ROLL-A-BALL TUTORIAL #3

#Movement of Player

In this chapter, we create the movement of the player. We have to think about the operation below to move the Player.
1.	Create a Player
2.	Add a movement function into Player
We add a function that adjust position into Player with physical operation.

# 1. Add a Player
At first, we create a Player.
1.	Click the “Create” button in Hierarchy view
2.	Select "3D Object" -> "Sphere" 
3.	Change a name into "Player" in Inspector view
![1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/82e1a8d7-ac20-62d6-9e9f-1cf95f449389.png)

 
Locate Position at central high in the stage
   Position(x:0, y:1.5, z:0)

#2. Reproduce a Game
Confirm current function before adding a component confirm a current function. In Unity, we can confirm the actions of the current game by clicking the “reproduce a game” button. Click on the button. While a game-playing  “reproduce a game” button be bright blue. After confirming actions click  “reproduce a game” button again to stop a game.
![2.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/975f8868-d491-55a8-3b0d-0f2511937b5c.png)

 
#3. Add a component
Next, add a component. Component constructs GameObject. GameObject be able to make its own operations and affect other objects in accordance with the actions of a component. We can confirm in the Inspector view by selecting Component Game Object. For example, the following picture shows “Sphere Collider”
![3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/903dd78d-b92b-0065-6bdf-fddd324ef745.png)
 
And then add a component into the Player with the “Add Component” button in the Inspector view.
1.	Select Player in Hierarchy view
2.	Click the “Add Component” button in the Inspector view.
3.	Select Physics > Rigidbody 
![4.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/ac4da4ee-6d08-b2d9-2d0b-068015741528.png)
 
And reproduce a game after adding a component. Because added a function of physical operation we can confirm movement in which Player object falls.
![5.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/8221fa62-2af0-98a4-95bf-d53196f6a100.png)

 
#4. Add a function that makes Player roll to the direction you want to roll
We need a function that makes Player roll as a result of input to add a rolling game. This function is not prepared in Unity so you need to make it yourself. We code this component with C# or UnityScript. 
At first, add a component into the Player object.
1.	Select Player object in Hierarchy view
2.	Click the “Add Component” button in Inspector view
3.	Click New Script
4.	Select “PlayerController” as Name and “CSharp” as Script
5.	Click “Create And Add”

A component that doesn’t have function anything has added a component in this process.
![6.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/7b402c37-8634-62c7-ad0f-8444772eb200.png)

As well as "PlayerController.cs" file has made in the Assets folder ->Project browser. This file became the main script. Because it is not good to be untidy in folder we create “Script Folder” and then make it move into this folder.
1.	Click the “Create” button in Project browser
2.	Edit folder name to” Script”
3.	Select “PlayerController” and drag&drop to Assets/Script folder
![7.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/dee6d95a-ba32-d06a-920e-d917aa4a1cc1.png)

 
#5.Alteration of the script in component
The next step is editing the script in the component. To edit script double click "PlayerController.cs" which created previously and then boot Monodevelop. 
![8.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/23338d2e-7565-bb80-e613-9d05440c7f59.png)

At first delete a simple code. 
![9.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/3fe28662-4d4d-5061-7cae-02c49f38ef31.png)

```PlayerController.cs
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
}
```
 

A script that creates in this time has a function that “when clicking a button, it rolls”. If analyze steps as function it becomes likes below.
1.	Every flame, accept inputs
2.	Before move by physical operation add power to Player’s rigid-body based on input

#6. Add an input function
At first, we must resolve ‘Every flame, accept inputs’. Unity script call method automatically when defining a specific method. For example when coding “ void Update(){} ” in class Unity script call processing written in ‘ {} ‘ at every time when the flame is renewed. If we code “ void FixedUpdate(){} ” we can set processing which is called whenever the character moves with the physical operation. Because use physical operation, we code likes below in FixedUpdate.

```PlayerController.cs
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
    void FixedUpdate ()
    {
    }
}
```

Next, we acquire ‘input’ in FixedUpdate. If we code likes below, it can substitution ‘input’ into x,z

```PlayerController.cs
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
    void FixedUpdate ()
    {
        //  substitution ‘input’ into x,z
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
    }
}
```
 
#7. Add a physical operation function 
Next, we add power into a rigid body component based on acquired input. We use “GetComponent” to acquire the same object component. We need ‘Rigidbody’ so code “GetComponent<Rigidbody>()”.

```PlayerController.cs
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
    void FixedUpdate ()
    {
        float x =  Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        // acquire Rigidbody component which same Game object owned
        Rigidbody rigidbody = GetComponent<Rigidbody>();
    }
}
```
 

At last we add an input function into a rigid body that acquired previously. We select ”AddForce”.

```PlayerController.cs
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
    void FixedUpdate ()
    {
        float x =  Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Rigidbody rigidbody = GetComponent<Rigidbody>();

        // add a power to x(side),z(depth) in rigidbody
        rigidbody.AddForce(x, 0, z);
    }

``` 

This chapter has done. Let’s reproduce a game. And Players move slowly because we set weak power right now. 
![10.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/73f2a0c2-d58f-6b45-285b-3933898a0ce7.png)

So we strengthen power in rigid-body. Add Force

```PlayerController.cs
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
    void FixedUpdate ()
    {
        float x =  Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Rigidbody rigidbody = GetComponent<Rigidbody>();

       //strengthen power of x,z to 10times
        rigidbody.AddForce(x * 10, 0, z * 10);
    }
}
``` 
 

If we code, we can adjust ball speed easily. First add a ‘speed variable’ to the PlayerController class in Inspector.And edit likes below.

 ```PlayerController.cs
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
    // Control speed
    public float speed = 10;

    void FixedUpdate ()
    {
        float x =  Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Rigidbody rigidbody = GetComponent<Rigidbody>();

        // Multiply  x,y by speed
        rigidbody.AddForce(x * speed, 0, z * speed);
    }
}

``` 

And then added control speed item in Inspector view. We can adjust speed anytime.

 ![12.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/33d2c0a4-79d2-7630-d78c-ad83a59a4ed8.png)

The next chapter explain how to operate a camera.