Someone pelase help im new but i want to develoep a decent game.
What i'm trying to do is find a chracter script whereas my character is moving forward on Z constantly without anything the user has to do. but also, i want my character to have the ability to move left and right. Any help?
An example of a game would be this game here : www.cubefield2.com
if you see the way you move i want the script for that....help?
If you are using a CharacterController...which you should be if you want collision detection, then you could do something like this:
[RequireComponent(typeof(CharacterController))];
//Make sure there is a Character Controller attached to this object.
public class CharacterMotor : MonoBehaviour {
const float constantMotion = 10;
//If it really is a constant speed you can declare it as such and it will prevent you from changing it.
//Otherwise just remove the modifier.
public int moveSpeed;
public int jumpSpeed;
//how fast do we move side to side and jump.
public float gravity = 9.8f;
private CharacterController controller;
public void Start () {
controller = GetComponent<CharacterController>();
//cache a link to the Character Controller.
}
public void Update () {
Vector3 moveDirection = new Vector3( 0 , 0 , constantMotion );
//Create a new Direction vector
if( controller.isGrounded() ) { //Are we touching the ground?
moveDirection.x = Input.GetAxis("Horizontal") * moveSpeed;
if( Input.GetButtonDown("Jump") ) {
moveDirection.y = jumpSpeed;
//Set the vertical direction to the jump speed.
//moveDirection = transform.TransformDirection(moveDirection);
//You don't need this line since you will only be moving in one direction,
//But you will if your character rotates any.
}
}
moveDirection.y -= gravity * Time.deltaTime;
//This will pull the character back down.
controller.Move(moveDirection * Time.deltaTime);
//Move the character in the direction of moveDirection at a FPS independent rate.
}
}