Player Movement (lateral movement and flight won't function together)

Hey guys,

I am trying to write a script for player movement and watched this youtube tutorial to learn how.

I tried to extend the functionality to move the player up and down on the y-axis to start simulating some form of flight. However, if I have the forward movement first in fixed update then it won’t register the flight variables and if I have the flight variables first then it doesn’t register forward and backward movement. Can anyone point me in the right direction or just tell me why this is happening

Here is the code I am working with right now

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour {
// initial speed for all movement
public float speed = 12;
// initial speed for all turning
public float turnSpeed = 100;
// replacing the jump function with this to achieve dashing
public float dashDist = 5.0f;
// delay input to make sure player wanted to make that action
public float inputDelay = 0.1f;

// rotation for the player
Quaternion targetRotation;

// cache the rigidbody that will be used to move object
public Rigidbody rb;
public float forwardInput, turnInput, fly;

// Use this for initialization
void Start () {
targetRotation = transform.rotation;
if (GetComponent ())
rb = GetComponent ();
else
Debug.LogError (“Need rigidbody”);

forwardInput = turnInput = 0;
}

void GetInput(){
forwardInput = Input.GetAxis (“Vertical”);
turnInput = Input.GetAxis (“Horizontal”);
fly = Input.GetAxis (“Fly”);
}

// Update is called once per frame
void Update () {
GetInput ();
Turn ();
}

// FixedUpdate called for physics based updates
void FixedUpdate(){
Move ();
Fly ();

}

void Move(){
Debug.Log (“in move method”);
if (Mathf.Abs (forwardInput) > inputDelay) {
//can move
rb.velocity = transform.forward * forwardInput * speed;
} else {
// no movement velocity = 0
rb.velocity = Vector3.zero;
}
}

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

void Fly(){
if (Mathf.Abs (fly) > inputDelay) {
//can move
rb.velocity = transform.up * fly * speed;
} else {
// no movement velocity = 0
rb.velocity = Vector3.zero;
}
}
}

Thanks

Use code tags!

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour {
    // initial speed for all movement
    public float speed = 12;
    // initial speed for all turning
    public float turnSpeed = 100;
    // replacing the jump function with this to achieve dashing
    public float dashDist = 5.0f;
    // delay input to make sure player wanted to make that action
    public float inputDelay = 0.1f;

    // rotation for the player
    Quaternion targetRotation;

    // cache the rigidbody that will be used to move object
    public Rigidbody rb;
    public float forwardInput, turnInput, fly;

    // Use this for initialization
    void Start () {
        targetRotation = transform.rotation;
        if (GetComponent<Rigidbody> ())
            rb = GetComponent<Rigidbody> ();
        else
            Debug.LogError ("Need rigidbody");

        forwardInput = turnInput = fly = 0;
    }

    void GetInput(){
        forwardInput = Input.GetAxis ("Vertical");
        turnInput = Input.GetAxis ("Horizontal");
        fly = Input.GetAxis ("Fly");
    }
   
    // Update is called once per frame
    void Update () {
        GetInput ();
        Turn ();
    }

    // FixedUpdate called for ohysics based updates
    void FixedUpdate(){
        Move ();
        Fly ();

    }

    void Move(){
        Debug.Log ("in move method");
        if (Mathf.Abs (forwardInput) > inputDelay) {
            //can move
            rb.velocity = transform.forward * forwardInput * speed;
        } else {
            // no movement velocity = 0
            rb.velocity = Vector3.zero;
        }
    }

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

    void Fly(){
        if (Mathf.Abs (fly) > inputDelay) {
            //can move
            rb.velocity = transform.up * fly * speed;
        } else {
            // no movement velocity = 0
            rb.velocity = Vector3.zero;
        }
    }
}

Thank you that was my first post on this forum

Just an idea but rather than running everything through the update function simultaneously take input from the user on specific key-bindings. For example use “Q” and “E” to increase/decrease height and “W” and “S” to move forward/backwards.

1 Like
using UnityEngine;
using System.Collections;

public class MovementScript : MonoBehaviour {

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {

        if(Input.GetKeyDown(KeyCode.W))
        {
            transform.Translate(Vector3.forward * Time.deltaTime);
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            transform.Translate(Vector3.back * Time.deltaTime);
        }

        if (Input.GetKeyDown(KeyCode.Q))
        {
            transform.Translate(Vector3.up * Time.deltaTime);
        }

        if (Input.GetKeyDown(KeyCode.E))
        {
            transform.Translate(Vector3.down * Time.deltaTime);
        }

    }
}

The only problem with this is that its for movement so it needs to be continuous.

You should tailor your code to the button presses, the “transform.Translate” sections of code are there just as an example.

This forum post could be of use to you:

Thanks I got it now. How do I mark it once it is solved?

Not actually sure, never had to do it :slight_smile: Good job though.