- Home /
How do i make the game start so that the character is already running?
This is my code so far, i was wondering if there is a way to make the character start off the game running?
using UnityEngine; using System.Collections;
[RequireComponent (typeof(CharacterController))] public class AdvancedMovement : MonoBehaviour { public float walkSpeed = 5; public float runMultiplier = 2; public float strafeSpeed = 2.5f; public float rotateSpeed = 250; public float gravity = 20;
public CollisionFlags _collisionFlags;
private Vector3 _moveDirection;
private Transform _myTransform;
private CharacterController _controller;
public void Awake() {
_myTransform = transform;
_controller = GetComponent<CharacterController>();
}
// Use this for initialization
void Start () {
_moveDirection = Vector3.zero;
}
// Update is called once per frame
void Update () {
if(_controller.isGrounded) {
Debug.Log("On the ground.");
_moveDirection = new Vector3(0,0, Input.GetAxis("Move Forward"));
_moveDirection = _myTransform.TransformDirection(_moveDirection).normalized;
_moveDirection *= walkSpeed;
}
else{
Debug.Log("Not on the gorund.");
if((_collisionFlags & CollisionFlags.CollidedBelow) == 0) {
}
}
_moveDirection.y -= gravity * Time.deltaTime;
_collisionFlags = _controller.Move(_moveDirection * Time.deltaTime);
}
}
Answer by senad · Jan 31, 2012 at 08:28 AM
In your first Update-method call you will first check the user-input and then update the movement. It is very likely that there is no user input in the first update frame, so your character will not move.
So as I see it, if you want your character to move from the start, you need to set a moveDirection in the Start()-method and ignore the user input while it is zero.
This way your character would be moving from the beginning until the player changes the direction. Is it what you want to happen?
In a way yes, I am trying to make it so that throughout the whole game the user has no control over the forward movement of the character but only has the option to move from side to side and to jump. Does that make any sense?
Yes, now I get your meaning. :)
How you can implement this actually depends on your camera. If your camera is rotating then your forward moving direction will change with it. In this case it will always be the direction of the cam but with y = 0. The left vector will be the cross product of the forward with the up vector.
If your camera rotation is fixed, you will always have constant direction vectors for forward and left. You can use vector addition to calculate your movement like this: movement = forward + Input.GetAxis("leftright") * left;
An example for a constant setup: forward = (0,0,1) left = (-1,0,0) up = (0,1,0)