Section 3- Basic Physics and Scripting



17) Look at physics components of floor and ball
Planes, spheres, cubes, capsules, cylinders, and quads are all created with appropriate collider components.

This allows other physics objects to collide with it. (Delete the collider component to make it "pass-through.")


18) Right Bodies
Select the Player, then select "Add Component -> Physics -> Rigidbody" in the Inspector.

Any physics object that will be moving should have a Rigid Body component in addition to the collider component.
Unmoving objects like walls should only have a collider.

If an object will be frequently moved via directly accessing it's transform but you don't want it to respond to forces like a full physics object, give it a RigidBody and select "kinematic" in the RigidBody component. (A moving wall, for example.)
Directly moving an object with just a collider will still "work," but will incur a needless performance penalty.


19) Make PlayerMovement script on Player object
Select the Player, then select "Add Component -> New Script" in Inspector.
Select C#, name it "PlayerMovement," then "Create and Add."
Double Click on the new script in the Assets window to open it in MonoDevelop.

The new script will have a template for a class derived from MonoBehavior, which means it can be attached to a GameObject.
Empty Start and Update overrides are provided.
Start is called when the GameObject is enabled and Update is called once every frame.

Example complete C# PlayerMovement script is here.


20) Write Player movement code in Update function
Put the following code into the Update function:

float fMoveHorizontal = Input.GetAxis("Horizontal");
float fMoveVertical = Input.GetAxis("Vertical");

rigidbody.AddForce(new Vector3(fMoveHorizontal, 0.0f, fMoveVertical) * fSpeed * Time.deltaTime);


21) Look around the script-editing environment
You can directly access components of the object a script will be attached to. For example, accessing rigidbody in the above code.

Any class variables declared public can be set in the Inspector.

The script is automatically compiled when Unity window gets focus and any errors are shown.

Highlight a word and press ctrl + ' to bring up Unity docs on it.


22) Improve the PlayerMovement script
Declare the public variable speed and give it a default value of 100.

Move the player movement code to FixedUpdate since it interacts with physics.
(FixedUpdate is called once each physics frame.)

Change Time.deltaTime to fixedDeltaTime to get the physics update time.


20) Script console debugging
Put the following code into the Update function:

print("Debug text");

Run


23) Experiment with PlayerMovement's public speed variable in the Inspector
Set speed to around 500 and test.


24) Add a physics material to (attempt to) slow down the Player.
Try moving the player in-game. It keeps rolling because of no friction.

Select "Create -> Physics Material" in the Project view. Increase the static and dynamic friction.

Apply the new material to the colliders on both the Player and ground by dragging them onto the Material property or clicking the circle to the right and selecting it from the menu.

The friction does nothing! Why not? Physics engine sees 0 surface area contact.

Remove and delete the physics material.


25) Increase Player linear drag
Instead of using a PhysicsMaterial, increase drag property of the Player's RigidBody component. (~2.0)

This works, but you might want to write a script to temporarily remove the drag when falling...

But this seems hacky! Welcome to game physics.





Prev: Section 2- Basic Lighting and Project Organization
Next: Section 4- Level Building, More Scripting, and Publishing

Back to Main