I will show you today how to make a simple steerage mean steerage on right, on left, directly and backword and jump, it all comes down to one script.
Assign that script to for example cube this is the simpler solution which may be, this is only example, go to discuss the script.
using UnityEngine;
using System.Collections;
public class steerage : MonoBehaviour {
float go = 40f;
float jump = 20f;
Rigidbody physics;
bool IfGround = true;
void Start () {
physics = GetComponent();
}
void Update () {
if (Input.GetKey (KeyCode.LeftArrow)) {
transform.position += Vector3.left * go * Time.deltaTime;
}
if (Input.GetKey (KeyCode.RightArrow)) {
transform.position += Vector3.right * go * Time.deltaTime;
}
if (Input.GetKey (KeyCode.DownArrow)) {
transform.position += Vector3.back * go * Time.deltaTime;
}
if (Input.GetKey (KeyCode.UpArrow)) {
transform.position += Vector3.forward * go * Time.deltaTime;
}
if (Input.GetKey (KeyCode.Space) && IfGround==true) {
physics.velocity = physics.velocity + Vector3.up * jump;
}
}
void OnCollisionStay()
{
IfGround = true;
}
void OnCollisionExit()
{
IfGround = false;
}
}
On the top the script are variables type float, variable go is defining how fast object is moving, variable jump is defining how high object can jump, next is variable physics object, which it is adding physics to jumping, that will look like as a jump, now object won’t show up in one second in air and variable IfGround is needed to detecting position object.
In function “Start” We are downloading physics component and in function “Update” is a heart of the program, inside of condition if is instruction which will execute when player will enter left arrow, then object will move in left. In next line is adding position object and direction wherein object have to move and accumulate it throw variable go to define speed object. We have to accumulate all throw Time.deltatime, because if we won’t do that, object would have to agitate in different speed. Rest of the code do 26 line look like similar, is only different arrows and directions.
From 27 to 29 line code define when player will enter space and if object is on ground, then object have to jumping, and jumping object will to keep as physics object.
On the bottom the script there are two functions which are checking that object is in air or on ground, if weren’t those functions we would jump in air.
That’s all, we should compile and drag and drop script to object which you would to move.
If you would to like move character in 3d game making your own script so that’s no sense because you can download ready scripts control character, on the top in bookmark assets in Import package enter on Characters download all and find object „First person controller” and drag and drop it on scene and here we go, you have ready script control.
If you recognise it as useful, share it with others so that others can also use it and leave upvote and follow if you wait for next articles :)
That’s all, thanks, see you later!