Basic Player Movement. (I think)

Hey guys,

I’ve been playing about with Unity for a few months and made some ‘very’ simple projects.
Recently I wanted to focus on something a little more in-depth as opposed to moving cubes around a ground plane.

I want to create a movement script which is fairly simple I think, but for the life of me seem unable to do it.

Using the arrow keys I want to achieve the following… (this will be a top down 2D environment)

With the ‘up’ key I want move the character forwards and continue to move forward when the input key is released, although have the character begin slowly before reaching max movement speed. (forward being whatever is the current facing direction)

With the ‘left’ and ‘right’ keys I want to have the player rotate left or right accordingly and for an infinite time until the input key is released.

With the ‘down’ key I’d like to have the player face the opposite to the current moving direction.

Thank you guys for any help with this problem, so far all I have managed to do is get the player to move in the direction the input keys are pressed.

Post the code you have so far so we can understand the issue you face even better.

using UnityEngine;
using System.Collections;

public class CharacterController : MonoBehaviour {

    public float inputDelay = 0.1f;
    public float forwardVel = 12;
    public float rotateVel = 100;

    Quaternion targetRotation;
    Rigidbody rBody;
    float forwardInput, turnInput;

    public Quaternion TargetRotation
    {
        get { return targetRotation; }
    }

    void Start()
    {
        targetRotation = transform.rotation;
        if (GetComponent<Rigidbody>())
            rBody = GetComponent<Rigidbody>();
        else
            Debug.LogError("The character needs a rigidbody.");

        forwardInput = turnInput = 0;
    }

    void GetInput()
    {
        forwardInput = Input.GetAxis("Vertical");
        turnInput = Input.GetAxis("Horizontal");
    }

    void Update()
    {
        GetInput();
        Turn();
    }

    void FixedUpdate()
    {
        Run();
    }

    void Run()
    {
        if (Mathf.Abs (forwardInput) > inputDelay)
        {
            //move
            rBody.velocity = transform.forward * forwardInput * forwardVel;
        }
        else
            //zero velocity
            rBody.velocity = Vector3.zero;
        }

    void Turn()
    {
        if (Mathf.Abs(forwardInput) > inputDelay)
        {
            targetRotation *= Quaternion.AngleAxis(rotateVel * turnInput * Time.deltaTime, Vector3.up);
                }
                transform.rotation = targetRotation;
                }
                }

I found an error in the code on line 61. I had forwardInput instead of turnInput.

Now I need to make the player continue to move forward even when the forward key is no longer held down. And the ‘down’ key I need to make the player face the opposite to the current moving direction.

Okay, so just about got everything working. I cannot seem to get the player to face the opposite direction to which it is currently moving by pressing the ‘down’ key.

It is currently adding an opposing force.
Any tips?

using UnityEngine;
using System.Collections;

public class CharacterController : MonoBehaviour {

    public float inputDelay = 0.1f;
    public float forwardVel = 12;
    public float rotateVel = 100;

    Quaternion targetRotation;
    Rigidbody rBody;
    float forwardInput, turnInput;

    public Quaternion TargetRotation
    {
        get { return targetRotation; }
    }

    void Start()
    {
        targetRotation = transform.rotation;
        if (GetComponent<Rigidbody>())
            rBody = GetComponent<Rigidbody>();
        else
            Debug.LogError("The character needs a rigidbody.");

        forwardInput = turnInput = 0;
    }

    void GetInput()
    {
        forwardInput = Input.GetAxis("Vertical");
        turnInput = Input.GetAxis("Horizontal");
    }

    void Update()
    {
        GetInput();
        Turn();
    }

    void FixedUpdate()
    {
        Run();
    }

    void Run()
    {
        if (Mathf.Abs (forwardInput) > inputDelay)
        {
            //move
            //rBody.velocity = transform.forward * forwardInput * forwardVel;
            rBody.AddForce (transform.forward * forwardInput * forwardVel);
        }
        //else
            //zero velocity
            //rBody.velocity = Vector3.zero;
        }

    void Turn()
    {
        if (Mathf.Abs(turnInput) > inputDelay)
        {
            targetRotation *= Quaternion.AngleAxis(rotateVel * turnInput * Time.deltaTime, Vector3.up);
                }
                transform.rotation = targetRotation;
                }
                }

The easiest way to set the direction a transform is facing is to set the .forward property.

If you want to turn to the direct opposite direction of the current input, just do:

transform.forward = -forwardInput;

Hey Thank you for the reply,

I’m not exactly trying trying to turn the direct opposite of the current input. But rather the current trajectory of the player, regardless what direction they are facing.

eg; they could be facing to the left, but travelling down. I want to push the down key and have them then face up.

That was an example - you set your forward to what you want the character to face.

So if you want the opposite of the rigidbody’s velocity, you set forward to -rigidbody.velocity.