Character Controller Not Moving in the Direction its Facing

I have my script written but when I move the character two things happen that I dont like. First my character doesnt move in the direction its facing or the direction of the key I pressed and second when I click the right or left keys my character only rotates I want it to move left and right as well.

Thanks

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Character1AnimControl : MonoBehaviour {

static Animator anim;
public float speed = 2.0f;
public float rotationspeed = 75.0f;

// Use this for initialization
void Start () {

anim = GetComponent();

}

// Update is called once per frame
void Update () {

float translation = Input.GetAxis(ā€œVerticalā€)*speed;
float translation = Input.GetAxis(ā€œHorizontalā€)*rotationspeed;
translation *= Time.deltaTime;
translation *= Time.deltaTime;

transform.Translate(0, 0, translation);
transform.translate(0, translation, 0);

if (translation != 0)
{
anim.SetBool(ā€œIsWalkingā€, true);
} else {
anim.SetBool(ā€œIsWalkingā€, false);
}

}
}

Please take a moment to look at this page: Using code tags properly - Unity Engine - Unity Discussions
It will show you how to insert code nicely into the forums, so that it’s easier to read.

I don’t see any code for rotation at all.

One thing that stands out, is that you’ve named 2 variables ā€˜translation’. I’m pretty sure that’s not even allowed.
Do you have errors from this script?

Okay here is the code again srry and I fixed the double translation variable.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Character1AnimControl : MonoBehaviour {

    static Animator anim;
    public float speed = 2.0f;
    public float rotationspeed = 75.0f;

    // Use this for initialization
    void Start () {

        anim = GetComponent<Animator>();

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

        float translation = Input.GetAxis("Vertical")*speed;
        float rotation = Input.GetAxis("Horizontal")*rotationspeed;
        translation *= Time.deltaTime;
        rotation *= Time.deltaTime;

        transform.Translate(0, 0, -translation);
        transform.Rotate(0, rotation, 0);

        if (translation != 0)
        {
            anim.SetBool("IsWalking", true);
        }else{
            anim.SetBool("IsWalking", false);
        }

    }
}

So how do I go about fixing this, thanks.

Okay, well if you’re moving and rotating at the same time… it might be ā€˜odd’ to find ā€œforwardā€ , as it will be always changing.
You never applied the sideways movement to the transform.Translate portion. That’s why it never moved to the side.
However, I’m not sure if just doing that will fix it all for you because of the turning part.
Does that make sense?

This is a third person character controller?

Do you want to just create your own? As in you didn’t want to use the standard asset? It can be fun to make your own, for sure…

I dont really know Im very new to this. So what do I type to fix this issue?

well, as I said I don’t think it will fully fix it… but
see how you have transform.Translate(0,0,-translation); ?
change that to: transform.Translate(rotation, 0, -translation);
Before, you weren’t using the horizontal value to move (only rotate). That’s why it was like that.

No it didnt seem to work, now i just move really fast forwards and backwards and dont rotate at all.

Okay, so there’s an issue with turning (rotating) and moving. If I go left, and I’m also rotating left… do I just keep rotating & going left forever?
Plus, if I rotate and I want my ā€œcurrent leftā€ to alter with the rotation, then I’ll just be doing a circle.

So, do you want the character to move sideways while keeping its ā€œfacingā€ direction, or just turn to the side like 90 degrees and move? That’s how I am thinking about it, anyways.

This would move you in all directions (including diagonal) but doesn’t do any rotation:

 void Update()
    {
        float v = Input.GetAxis("Vertical") * speed * Time.deltaTime;
        float h = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
        Vector3 move = new Vector3(h, 0, v);
        move = Vector3.ClampMagnitude(move, speed * Time.deltaTime);
        transform.Translate(move);
    }

More info is required about how you want the rotation to go, I think…

Ok Im trying to get it so when I press W to go forward my character turns in the forward direction, starts walking then if I like turn left and hit A the character turns -90 degress left and continues walking.
Right now the character moves forward and back when I press A and D and moves left and right when I press W and S all the while facing the same direction which I dont like.

Hopefully thats enough info and heres the code again so you dont have to scroll back up.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Character1AnimControl : MonoBehaviour {

    static Animator anim;
    public float speed = 2.0f;
    public float rotationspeed = 75.0f;

    // Use this for initialization
    void Start () {

        anim = GetComponent<Animator>();

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

        float translation = Input.GetAxis("Vertical")*speed;
        float rotation = Input.GetAxis("Horizontal")*rotationspeed;
        translation *= Time.deltaTime;
        rotation *= Time.deltaTime;
   
        transform.Translate(rotation, 0, -translation);
        transform.Rotate(0, rotation, 0);

        if (translation != 0)
        {
            anim.SetBool("IsWalking", true);
        }else{
            anim.SetBool("IsWalking", false);
        }

    }
}

Sorry, I’ve been thinking about this for a while, but haven’t come up with a good solution.

It’s not the first time I’ve been stumped by this sort of ā€œissueā€ :slight_smile:

It seems simple, but whenever I try to implement it, I cannot. lol

One thing that comes to mind is having a ā€œwalk left/rightā€ animation. That is what I had always planned to use, had I ever gotten around to doing that. With that, my theory was that regular walking (forward or forward + any turning, for rotation) would be on thing. Then, walking left/right/backwards could have a seperate animation , all the while keeping my rotation unchanged. That seems simple in theory…

I was trying to use a separate Vector3 (empty game object or stored vector3) to use as my ā€œforward orientationā€ just now, but came up short. It sounds plausible, but…

Anyways, this is incomplete and may be similar to what you already have:

 void Update()
    {
        float v = Input.GetAxis("Vertical") * speed * Time.deltaTime;
        float h = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
        Vector3 move = transform.forward * v;// + transform.right * h;
      //  move = Vector3.ClampMagnitude(move, speed * Time.deltaTime);
        transform.rotation *= Quaternion.Euler(0, h*10, 0);
        transform.position += move;
    }

left/right will only ā€œmoveā€ the player if they’re already moving forward or backward.
Sorry, maybe someone else can help in a better way. I’d be curious/interested to learn a solution to this, too :wink:

I tried a bit more. :slight_smile:
I used an empty game object as a parent.
In my code, the variable ā€œcapsā€ is the main (child) object.

float v = Input.GetAxis("Vertical") * speed * Time.deltaTime;
float h = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
Vector3 move = transform.forward * v + transform.right * h;
move = Vector3.ClampMagnitude(move, speed * Time.deltaTime);
 
if (v != 0 && h != 0) 
   transform.rotation *= Quaternion.Euler(0, h*15, 0); // alter '15' to whatever feels comfortable for rotation speed.

else if (v > 0)
   caps.transform.localRotation = Quaternion.identity;
else if (v < 0)
   caps.transform.localRotation = Quaternion.Euler(0, -180, 0);
else if (h != 0)
{
   if (h < 0) caps.transform.localRotation =  Quaternion.Euler(0, -90, 0);
   else caps.transform.localRotation = Quaternion.Euler(0, 90, 0);
}
transform.position += move;

Could look a little nicer, maybe, not sure… However, it’s working decently well, as far as I can tell.
Let me know if it’s any good with your character :slight_smile:

a quick fix from your original code, take note of lines 26 and 27:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Character1AnimControl : MonoBehaviour {

    static Animator anim;
    public float speed = 2.0f;
    public float rotationspeed = 75.0f;

    // Use this for initialization
    void Start () {

        anim = GetComponent<Animator>();

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

        float translation = Input.GetAxis("Vertical")*speed;
        float rotation = Input.GetAxis("Horizontal")*rotationspeed;
        translation *= Time.deltaTime;
        rotation *= Time.deltaTime;

        transform.Rotate(0, rotation, 0);//moved this line to before movement
        transform.Translate(transform.TransformDirection(translation));//movement is now moving based on rotation

        if (translation != 0)
        {
            anim.SetBool("IsWalking", true);
        }else{
            anim.SetBool("IsWalking", false);
        }

    }
}

However, this is very car-like behavior and probably far from what you are intending to do. Just note that in games, rotation and movement is usually separated from each other for the sake of control, the linkage between them is an illusion.

1 Like

No it still doesnt seem to be working, why is this happening ughh.

That might come off as rude, but you are sure that you haven’t set any funny values for the Input Axis in the Editor, right? Because @Laperens script looks fine.
Could you explain what itsn’t working? :slight_smile:

Sorry, Im really new to this. I pasted his script in but erros came up that I cant fix.

What errors? You kinda need to give us some insight to get help!

Argument #1' cannot convert float’ expression to type `UnityEngine.Vector3’
It is on line 27 I think…
sorry again haha

Change line 27 to this:

transform.Translate(transform.TransformDirection(Vector3.forward * translation));
1 Like

Its way closer, but no instead of moving left and right when I press W and S, he move a little diagonal but its closer. Also why when I turn it seems to be rotating around a axis not the center of his body?