1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ROLL-A-BALL TUTORIAL #3

Last updated at Posted at 2019-10-11

#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

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

#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

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

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

#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

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

#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

At first delete a simple code.
9.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()”.

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

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

The next chapter explain how to operate a camera.

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?