< Return to Video

Unity 5 - Roll a Ball game - 2 of 8: Moving the Player - Unity Official Tutorials

  • 0:01 - 0:04
    Now we are going to move the player game object.
  • 0:04 - 0:06
    First let's think through how we want this player
  • 0:06 - 0:08
    game object to behave.
  • 0:08 - 0:10
    We want to have the sphere roll around on the
  • 0:10 - 0:12
    game area, bump in to walls, stay on the ground
  • 0:12 - 0:14
    and not fly off in to space.
  • 0:15 - 0:17
    We want to be able to collide with our collectible
  • 0:17 - 0:19
    game objects and pick them up when we do.
  • 0:19 - 0:21
    This requires physics.
  • 0:21 - 0:23
    To use physics the game object needs
  • 0:23 - 0:26
    a rigidbody component attached.
  • 0:26 - 0:28
    To attach a new component we must first
  • 0:28 - 0:30
    select the game object we want to attach
  • 0:30 - 0:32
    the component to, in this case we will select
  • 0:32 - 0:34
    our player game object.
  • 0:34 - 0:37
    Then we can either choose the Component menu
  • 0:37 - 0:39
    and select Physics - Rigid Body
  • 0:39 - 0:41
    which would attach this component to the game object
  • 0:41 - 0:43
    we have selected.
  • 0:44 - 0:46
    Or use the Add Component button in the Inspector
  • 0:46 - 0:49
    choosing Physics - Rigid Body.
  • 0:50 - 0:52
    Either way this attached the rigidbody
  • 0:52 - 0:54
    component to the selected game object.
  • 0:55 - 0:57
    If we choose to we can rearrange the
  • 0:57 - 0:59
    order of the components on the game object
  • 0:59 - 1:02
    using the context sensitive gear menu
  • 1:02 - 1:04
    in the upper right of the component.
  • 1:04 - 1:07
    Doing this has no effect on the performance of our game.
  • 1:07 - 1:10
    However, having a consistent order to the components
  • 1:10 - 1:13
    on our game object may help us speed up our development
  • 1:13 - 1:15
    by keeping or maintaining an organised
  • 1:15 - 1:17
    project and hierarchy.
  • 1:17 - 1:19
    Don't forget, you can collapse or expand
  • 1:19 - 1:22
    the Component view by clicking on the component's title bar.
  • 1:22 - 1:24
    We should note that whenever we do this
  • 1:24 - 1:26
    this will also toggle the relevant
  • 1:26 - 1:29
    gizmos for that component in the Scene view.
  • 1:34 - 1:35
    Now we need to get the player object moving
  • 1:35 - 1:37
    under our control.
  • 1:37 - 1:39
    To do this we need to get input from our player
  • 1:39 - 1:41
    through the keyboard and we need to apply
  • 1:41 - 1:44
    that input to the player game object as forces
  • 1:44 - 1:46
    to move the sphere in the scene.
  • 1:47 - 1:49
    We will do this by using a script that
  • 1:49 - 1:51
    we attach to the player game object.
  • 1:52 - 1:54
    First let's create a folder in our project view
  • 1:54 - 1:56
    to hold our script assets.
  • 1:56 - 1:59
    In the Project view click on the Create menu
  • 1:59 - 2:01
    and choose Create Folder.
  • 2:02 - 2:04
    Rename this folder Scripts.
  • 2:05 - 2:08
    Next let's create a new C# script.
  • 2:09 - 2:11
    To create a new script we have some choices.
  • 2:11 - 2:13
    We can choose Assets - Create
  • 2:13 - 2:15
    to create our new C# script.
  • 2:17 - 2:20
    Or we can use the Create menu in the project view.
  • 2:22 - 2:24
    But what might be more efficient in this case
  • 2:24 - 2:26
    would be to select the player game object
  • 2:26 - 2:29
    and use the Add Component button in the Inspector.
  • 2:30 - 2:33
    The Add Component menu contains the selection
  • 2:33 - 2:34
    New Script.
  • 2:34 - 2:36
    This allows us to both create and attach
  • 2:36 - 2:38
    a script in one step.
  • 2:39 - 2:42
    First let's name this script PlayerController.
  • 2:43 - 2:47
    We can choose the language of the script, which will be C#,
  • 2:47 - 2:50
    and then click on Create and Add.
  • 2:50 - 2:52
    Or simply hit the return or enter key to
  • 2:52 - 2:54
    confirm our selection.
  • 2:55 - 2:58
    Unity will create, compile and attach this script
  • 2:58 - 3:00
    to our selected game object.
  • 3:00 - 3:03
    We should note, this way of creating a script
  • 3:03 - 3:06
    will create that script asset on the root
  • 3:06 - 3:08
    or top level of our Project view.
  • 3:09 - 3:12
    We will need to move the asset in to the Scripts directory
  • 3:12 - 3:14
    in order to maintain an organised Project view.
  • 3:15 - 3:18
    If we select the script in our Project view
  • 3:18 - 3:21
    we see a preview of the script asset in the Inspector,
  • 3:21 - 3:23
    but this text is not editable.
  • 3:24 - 3:26
    Let's open the script.
  • 3:26 - 3:28
    We can do this a number of ways.
  • 3:29 - 3:32
    When we are inspecting a game object using the script
  • 3:32 - 3:34
    we can use the context sensitive gear menu.
  • 3:36 - 3:39
    We can double click on the script asset in the Project view
  • 3:39 - 3:41
    or we can use the Open button in the Inspector
  • 3:41 - 3:44
    when we have the script selected in our Project view.
  • 3:45 - 3:48
    This will open our script in our preferred script editor.
  • 3:48 - 3:50
    First let's remove the sample code provided
  • 3:50 - 3:52
    in the base script.
  • 3:52 - 3:54
    Next let's think,
  • 3:54 - 3:56
    what do we want to do with this script?
  • 3:56 - 3:59
    We want to check every frame for player input
  • 3:59 - 4:02
    and then we want to apply that input to the
  • 4:02 - 4:05
    player game object every frame as movement.
  • 4:05 - 4:08
    Where will we check for and apply this input?
  • 4:08 - 4:10
    We have two choices.
  • 4:10 - 4:12
    Update and Fixed Update.
  • 4:13 - 4:16
    Update is called before rendering a frame
  • 4:16 - 4:18
    and this is where most of our game code will go.
  • 4:19 - 4:21
    Fixed Update on the other hand is called just
  • 4:21 - 4:24
    before performing any physics calculations
  • 4:24 - 4:27
    and this is where our physics code will go.
  • 4:28 - 4:30
    We will be moving our ball by applying forces
  • 4:30 - 4:32
    to the rigidbody, this is physics.
  • 4:32 - 4:35
    So we will put our code in Fixed Update.
  • 4:37 - 4:39
    What code to we need to write?
  • 4:40 - 4:41
    We know we need input,
  • 4:41 - 4:43
    but how do we find out more?
  • 4:44 - 4:46
    There is a shortcut in Monodevelop
  • 4:46 - 4:48
    that searches the Unity API.
  • 4:48 - 4:51
    On the mac it's command plus single quote,
  • 4:51 - 4:55
    and on the pc it's control plus single quote.
  • 4:55 - 4:57
    Select the item you want to research,
  • 4:57 - 4:59
    in our case input,
  • 4:59 - 5:01
    and hold down the command or control key and
  • 5:01 - 5:03
    type the single quote button.
  • 5:03 - 5:05
    This search brings up every reference
  • 5:05 - 5:08
    in our documentation related to input.
  • 5:09 - 5:11
    Select Input at the top,
  • 5:11 - 5:14
    interface in to the input system.
  • 5:16 - 5:18
    This brings up the page on Input.
  • 5:19 - 5:21
    This is the page on the input class.
  • 5:21 - 5:24
    We use this class to read the axis setup in the
  • 5:24 - 5:26
    input manager and to access multitouch
  • 5:26 - 5:28
    touch and accelerometer data
  • 5:28 - 5:30
    on mobile devices.
  • 5:30 - 5:32
    We read the text on the top of the page to
  • 5:32 - 5:34
    understand how to use the class.
  • 5:34 - 5:36
    In our case, to get input from the player
  • 5:36 - 5:39
    on all platforms, including mobile devices.
  • 5:40 - 5:42
    Under the description is a list of class
  • 5:42 - 5:45
    variables and class functions.
  • 5:45 - 5:47
    The class variables hold information
  • 5:47 - 5:50
    like the number of touches in touchCount
  • 5:50 - 5:53
    or a reference to the default gyroscope with gyro.
  • 5:54 - 5:56
    The class functions do something for us.
  • 5:57 - 6:00
    In our code we will be using Input.GetAxis.
  • 6:01 - 6:04
    When we find the item we want we click on the link
  • 6:04 - 6:07
    to bring up a page on the function or the variable.
  • 6:08 - 6:10
    Let's look at Input.GetAxis.
  • 6:10 - 6:13
    This page includes the signature of the function,
  • 6:13 - 6:15
    a description of the function
  • 6:15 - 6:18
    and code snippets showing how to use it
  • 6:18 - 6:20
    in Unity's Javascript and C#.
  • 6:20 - 6:22
    We will be using C#.
  • 6:23 - 6:25
    For more information on the input manager
  • 6:25 - 6:29
    and Input.GetAxis see the lessons linked below.
  • 6:30 - 6:32
    We will be using Input.GetAxis in a
  • 6:32 - 6:34
    very similar way to the code snippet.
  • 6:35 - 6:39
    Let's return to our script and write our code.
  • 6:39 - 6:45
    float moveHorizontal = Input.GetAxis ("horizontal")
  • 6:46 - 6:51
    float moveVertical = Input.GetAxis ("vertical")
  • 6:52 - 6:56
    This grabs the input from our player through the keyboard.
  • 6:56 - 7:00
    The float variables moveHorizontal and moveVertical
  • 7:00 - 7:02
    record the input from the horizontal
  • 7:02 - 7:06
    and vertical axis, which are controlled by the keys on a keyboard.
  • 7:06 - 7:10
    Our player game object uses a rigidbody
  • 7:10 - 7:12
    and interacts with a physics engine.
  • 7:12 - 7:16
    We will use this input to add forces to the rigidbody
  • 7:16 - 7:19
    and move the player game object in the scene.
  • 7:19 - 7:21
    To know more about how to apply forces to
  • 7:21 - 7:24
    the rigidbody let's check the documentation.
  • 7:25 - 7:28
    Type Rigidbody in to our script.
  • 7:28 - 7:31
    Now select rigidbody and hold down
  • 7:31 - 7:32
    the command key on the mac
  • 7:32 - 7:34
    or the control key on the pc
  • 7:34 - 7:36
    and type single quote.
  • 7:37 - 7:40
    Again, this brings a page with a search term "rigidbody".
  • 7:41 - 7:43
    We want to click on Rigidbody.
  • 7:44 - 7:46
    This brings up the rigidbody page.
  • 7:46 - 7:49
    If we want to apply force to our player game object
  • 7:49 - 7:51
    we need to do something.
  • 7:52 - 7:54
    So let's look at the functions available
  • 7:54 - 7:56
    to the rigidbody class.
  • 7:56 - 7:58
    We read the descriptions until we find
  • 7:58 - 8:00
    the one we want, in this case
  • 8:00 - 8:02
    let's choose AddForce.
  • 8:03 - 8:05
    This adds a force to the rigidbody
  • 8:05 - 8:08
    as a result the rigidbody will start moving.
  • 8:09 - 8:12
    Click the link and we go to the page on AddForce.
  • 8:12 - 8:14
    We can see the signature of the function
  • 8:14 - 8:16
    at the top of the page.
  • 8:16 - 8:19
    This signature tells us we need a vector3
  • 8:19 - 8:21
    and an optional ForceMode to add
  • 8:21 - 8:22
    force to our rigidbody.
  • 8:23 - 8:25
    What is a vector3?
  • 8:25 - 8:28
    For more detailed information on vector3
  • 8:28 - 8:30
    Please see the information linked below.
  • 8:31 - 8:33
    But in simple terms the vector3
  • 8:33 - 8:37
    holds 3 decimal values in one container.
  • 8:37 - 8:39
    This makes it easy for us to move around
  • 8:39 - 8:44
    and use values for things like a force in 3D space,
  • 8:44 - 8:46
    which requires a value for force on
  • 8:46 - 8:50
    each of the X, Y and Z axis.
  • 8:50 - 8:53
    Or to describe a rotation which would also require
  • 8:53 - 8:56
    a value for each of the X, Y and Z axis.
  • 8:58 - 9:01
    All of our documentation pages are linked together.
  • 9:02 - 9:05
    If we were to click on Vector3, we would be taken to the
  • 9:05 - 9:07
    documentation page for vector3.
  • 9:08 - 9:12
    The same is true for ForceMode, MonoBehaviour and RigidBody.
  • 9:14 - 9:16
    Below the description are snippets
  • 9:16 - 9:19
    that show us how this function could be used.
  • 9:19 - 9:22
    Note the second signature for AddForce below the first.
  • 9:23 - 9:25
    The descriptions are the same.
  • 9:26 - 9:30
    But in this case we can use AddForce with either a vector3
  • 9:31 - 9:35
    or 3 float values for X, Y and Z.
  • 9:36 - 9:39
    The next concept that we need to cover is
  • 9:39 - 9:42
    how to get a hold of, or how to reference
  • 9:42 - 9:44
    different components on our game object.
  • 9:45 - 9:49
    We are writing a script called PlayerController.
  • 9:49 - 9:51
    Which is attached as a script component
  • 9:51 - 9:53
    to our Player game object.
  • 9:53 - 9:56
    From this script we need to AddForce
  • 9:56 - 9:58
    using the rigidbody component.
  • 9:58 - 10:01
    We want to access that component from this script.
  • 10:02 - 10:05
    There are several ways that we can do this.
  • 10:05 - 10:08
    But in our case we will cover only one of the main ways
  • 10:08 - 10:12
    of accessing another component on the same game object.
  • 10:12 - 10:16
    We will create a variable to hold this reference in our script
  • 10:16 - 10:19
    and we will set this reference in the Start function.
  • 10:20 - 10:22
    We see this here in the code snippet.
  • 10:23 - 10:27
    public Rigidbody rb creates a public variable
  • 10:27 - 10:31
    with the type of rigidbody called rb
  • 10:31 - 10:34
    to hold the reference to the rigidbody we want to access.
  • 10:35 - 10:39
    In Start the reference is set by using the code
  • 10:39 - 10:41
    GetComponent
  • 10:42 - 10:45
    This will find and return a reference to the attached rigidbody,
  • 10:45 - 10:47
    if there is one.
  • 10:47 - 10:49
    All of the code in the start function is
  • 10:49 - 10:53
    called on the first frame that the script is active.
  • 10:53 - 10:56
    This is often the very first frame of the game.
  • 10:57 - 11:02
    Finally in FixedUpdate the attached rigidbody component
  • 11:02 - 11:05
    is accessed through the variable named rb
  • 11:05 - 11:08
    with rb.AddForce.
  • 11:09 - 11:12
    So in our script we need to write
  • 11:13 - 11:19
    private Rigidbody rb to create the variable to hold the reference.
  • 11:21 - 11:25
    And in a new start function we need to write
  • 11:25 - 11:29
    rb=GetComponent
  • 11:31 - 11:33
    In FixedUpdate
  • 11:33 - 11:37
    let's use the simplest version of Rigidbody.AddForce.
  • 11:37 - 11:39
    One that simply uses a vector3
  • 11:39 - 11:42
    and we will leave the ForceMode at default
  • 11:42 - 11:44
    by omitting it from our code.
  • 11:45 - 11:48
    So in our script we need to type
  • 11:48 - 11:53
    rb.AddForce and then some vector3 value.
  • 11:55 - 11:58
    No how do we get our two float values
  • 11:59 - 12:00
    in to a vector3 value?
  • 12:01 - 12:05
    Let's create a new vector3 variable called Movement.
  • 12:06 - 12:08
    This will be equal to a new vector3
  • 12:08 - 12:12
    that is made up of an X, a Y and a Z.
  • 12:13 - 12:17
    The X, Y, Z values will determine the direction of the force
  • 12:17 - 12:19
    we will add to our ball.
  • 12:20 - 12:21
    What is our X value?
  • 12:22 - 12:24
    That would be moveHorizontal.
  • 12:24 - 12:27
    With this our left and right keys will add force
  • 12:27 - 12:30
    moving the ball to the left or right.
  • 12:30 - 12:32
    What is our Y? 0.
  • 12:32 - 12:34
    We don't want to move up at all.
  • 12:35 - 12:38
    What is our Z value? That would be moveVertical.
  • 12:41 - 12:44
    Now we use Movement, a vector3 value,
  • 12:44 - 12:46
    in rb.AddForce
  • 12:46 - 12:50
    as rb.AddForce(movement).
  • 12:52 - 12:55
    Let's save this script and return to Unity.
  • 12:56 - 12:59
    We check for errors in our footer or the console
  • 12:59 - 13:01
    and there are none. Good.
  • 13:01 - 13:03
    Let's test what we've written.
  • 13:04 - 13:08
    Hit Play, and by using the keys setup on the input manager
  • 13:08 - 13:10
    the ball moves in the scene.
  • 13:11 - 13:13
    But it's very slow.
  • 13:13 - 13:16
    This is too slow to be playable, but the basic concept
  • 13:16 - 13:18
    works 100%.
  • 13:19 - 13:22
    To stop testing leave play mode.
  • 13:24 - 13:27
    Let's return to our code and create a tool
  • 13:27 - 13:30
    that will give us control over the speed of the ball.
  • 13:31 - 13:35
    We need to multiply our movement by some value.
  • 13:35 - 13:38
    We could simply enter that value here on our script
  • 13:38 - 13:41
    but if we ever needed to make any tweaks or changes
  • 13:41 - 13:44
    we would have to return to our scripting editor
  • 13:44 - 13:47
    and change the value in the script and then recompile.
  • 13:47 - 13:49
    This takes time.
  • 13:49 - 13:52
    The solution is to create a public variable in our script.
  • 13:53 - 13:55
    Let's create a public float
  • 13:56 - 13:58
    called Speed.
  • 13:58 - 14:01
    By creating a public variable in our script
  • 14:01 - 14:03
    this variable will show up in the Inspector
  • 14:03 - 14:05
    as an editable property.
  • 14:05 - 14:08
    When we use a public variable we can make
  • 14:08 - 14:10
    all of our changes in the editor.
  • 14:11 - 14:14
    We then multiply Movement by Speed.
  • 14:14 - 14:17
    We now have control over our movement value
  • 14:17 - 14:19
    from inside the editor.
  • 14:19 - 14:22
    Let's save these changes and return to Unity.
  • 14:23 - 14:26
    When we return to the editor we can see our
  • 14:26 - 14:29
    PlayerController script now has a speed property.
  • 14:29 - 14:32
    Let's set this property to 100.
  • 14:33 - 14:35
    Now enter play mode.
  • 14:36 - 14:38
    Whoa! Way too fast!
  • 14:39 - 14:42
    But changes are fast too. Exit play mode
  • 14:42 - 14:45
    and change the value to 10.
  • 14:46 - 14:48
    Hit Play.
  • 14:49 - 14:51
    That's a little better.
  • 14:52 - 14:54
    Congratulations, we can now move our character.
  • 14:55 - 14:59
    In the next assignment we'll talk about how to move the camera.
Title:
Unity 5 - Roll a Ball game - 2 of 8: Moving the Player - Unity Official Tutorials
Description:

more » « less
Video Language:
English
Duration:
15:00

English subtitles

Revisions