C# change of direction conflict

Hey guys,

Relatively new to Unity/C#. I am creating my own character controller class to handle moving the player. So far I have this:

void FixedUpdate ()
    {
        Vector3 direction = eyes.GetComponent<Camera>().transform.forward;
        direction.y = 0;
        if (Input.GetAxis("Vertical") > 0)
        {
            transform.position += direction * speed * Time.fixedDeltaTime;
        }
        if (Input.GetAxis("Vertical") < 0)
        {
            transform.position -= direction * speed * Time.fixedDeltaTime;
        }
    }

It works. However, the problem is that if I am moving forward with the “w” key pressed down and then press the “s” key to move backwards while still holding down the “w” key the character continues forward.

How do I go about fixing this?

Also, am I going about this right with the way I am programming the character’s movement? I know there are a lot of different ways to handle character movement but I do want to be able to do it with using GetAxis as opposed to KeyCodes and GetButtons.

First.

void FixedUpdate ()
    {
        Vector3 direction = eyes.GetComponent<Camera>().transform.forward;
        direction.y = 0;
        if (Input.GetAxis("Vertical") !=0)
        {
            transform.position += (direction*speed)*Input.GetAxis("Vertical");
    }
}

(Sorry about the formatting, writing this here on the forums…)

Second, consider chaching the transform of the “eyes” object so you don’t have to get the component each FixedUpdate. It’s a bad habit, so don’t start.

Third, the way you go about how you make the characters movement is all up to you. How you want your character to feel, what you want out of your character’s movement, and what you like. If you want to redo it at some point, nobody will stop you. If you’re new to this, then any time you put into this is worth it for the practice

Thank you your code example is a much more efficient and clean way to go about it.

I should have known that calling the get component in the update was a bad idea. I will try to find a way to store all that in another variable.

I understand making the character’s movement is all up to me but I’m still having trouble with my original question…
If I am moving with the forward key “w” and then press the back key “s” while still holding the forward key the player keeps moving forward instead of back. How would you go about setting it up where the player’s movement responds to whatever key was last pressed. If I’m moving forward and I press the key to move backwards I need that action to override the current one taking place.

Try using GetAxisRaw() instead of GetAxis() and play with the “Snap” checkbox in the Input settings for that axis. In my opinion, if you hold both buttons, you should stop movement entirely.

Thanks dude. Yeah I can’t quite figure it out. That said, I went and played similar exploration type games (which I’m making) such as The Stanley Parable and they didn’t even have their movements configured in the way I was trying to figure out. So I’ll let it go for now as I still have so much to learn.

However, the other guy suggested I find a way to make it so I don’t call GetComponent in the FixedUpdate which I figured wasn’t the best practice. Any suggestions on how to do this? I don’t really know how to do it without running it in some form of an Update function as I need it to be updated constantly to control my movement. Here is my code for the class so far:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    public float speed = 10f;
    public float strafeSpeed = 10f;
    public GameObject eyes; //Camera
    public Vector3 direction;
    public Vector3 sidestrafe;


    // Use this for initialization
    void Start ()
    {
        direction = eyes.GetComponent<Camera>().transform.forward;
        sidestrafe = eyes.GetComponent<Camera>().transform.right;
        direction.y = 0;
    }

    void Update()
    {

    }

    void FixedUpdate ()
    {
        direction = eyes.GetComponent<Camera>().transform.forward;
        direction.y = 0;
        if ( (Input.GetAxis("Vertical") != 0) || (Input.GetAxis("Horizontal") != 0) )
        {
            transform.position += (direction*speed)*Input.GetAxis("Vertical") * Time.fixedDeltaTime;
            transform.position += (sidestrafe*speed)*Input.GetAxis("Horizontal") * Time.fixedDeltaTime;
        }
    }
}

The only thing I can forsee which is something I did with my Flash games was to call a function from within the update to call that GetComponent but I don’t know if that is much better. For example:

    void Update()
    {
                GetCamera();
    }

         void GetCamera();
         {
                     direction=eyes.GetComponent<Camera().transform.forward;
        direction.y = 0;
          }
    void FixedUpdate ()
    {
        if ( (Input.GetAxis("Vertical") != 0) || (Input.GetAxis("Horizontal") != 0) )
        {
            transform.position += (direction*speed)*Input.GetAxis("Vertical") * Time.fixedDeltaTime;
            transform.position += (sidestrafe*speed)*Input.GetAxis("Horizontal") * Time.fixedDeltaTime;
        }
    }
}

^ Sorry that came out messy I was typing it here in editor :stuck_out_tongue:

Why is everything in FixedUpdate() when you’re not doing any physics stuff? Move it all to Update(). And never, EVER, use GetComponent() inside Update() or FixedUpdate(), at least not every time. Just do it once (in Awake() or Start() or OnEnable()) and cache the result.

You know…I don’t know why I have the character’s movement handled in the FixedUpdate as opposed to the Update haha I will fix that.

Someone else mentioned to not to do GetComponent inside an Update. The problem is that I constantly need the info from that thing. I don’t understand how to do this? And what do you mean by cache the result? The other guy mentioned cache as well but I don’t understand the concept as I’ve never heard of it before in the language I used to program in. If you could supply some pseudo code on how to achieve this it would really help me out.

Sure.

Camera myCamera;

private void Start() {
  myCamera = ...GetComponent<Camera>();
}

private void Update() {
  myCamera.DoStuff();
}

You would just be caching the result of the GetComponent() call in a variable that never changes again.

Make sure to add an a at the end instead of a y, by the way :stuck_out_tongue:

gah, fixed it :slight_smile: