0
\$\begingroup\$

I finished up my movement script which is finally working fantastically. The only issue I am having is getting the character to face the direction when I press either left or right on the keyboard. Something along the lines of say like a mario game. It is a platformer game. When I move right, it works perfectly, however when I move left, it moves left but walks backwards. I would like to get it set up to face the direction that I am walking.

        Vector3 move = new Vector3(0, 0, Input.GetAxisRaw("Horizontal") * Time.deltaTime);
    move = this.transform.TransformDirection(move);

    if (move == Vector3.zero)
    {
        animator.SetFloat("Speed", 0);
    }

    if (move != Vector3.zero)
    {
    _controller.Move(move * _speed);
    animator.SetFloat("Speed", _speed);        
    }
\$\endgroup\$
6
  • 1
    \$\begingroup\$ You have not shared enough information for us to help you with this. To know how to turn your character, we need to know how your character's visuals are set up. For a mesh, we can rotate it. For a sprite, we can toggle the flipX property. For a complex arrangement of sprites, we can set a negative scale on a parent transform. The C# code you've shown gives no hints which one is appropriate for this character. \$\endgroup\$
    – DMGregory
    Commented Jul 15, 2023 at 19:05
  • \$\begingroup\$ As an aside, please read tag descriptions when tagging your question. Do not tag them with UnityScript, an old JavaScript-like language that Unity has not supported for most of a decade, if your code is actually in C# like this. \$\endgroup\$
    – DMGregory
    Commented Jul 15, 2023 at 19:06
  • \$\begingroup\$ try : private Vector3 direction float eulerY = Camera.GlobalTransform.basis.GetEuler().y; direction = Vector3.Back.Rotated(Vector3.Up, eulerY) \$\endgroup\$
    – Cei
    Commented Jul 15, 2023 at 20:03
  • \$\begingroup\$ and then for the movement, just assign the inpuct vectors to the variable direction, in godot i do it this way, which is pretty easy to translate for C#: if Input.is_action_pressed("forward") or Input.is_action_pressed("backward") or Input.is_action_pressed("left") or Input.is_action_pressed("right"): direction = Vector3(Input.get_action_strength("left") - Input.get_action_strength("right"), \$\endgroup\$
    – Cei
    Commented Jul 15, 2023 at 20:05
  • \$\begingroup\$ and after you assigned the input vector3 to direction, assign the rotation : direction = direction.rotated(Vector3.UP, "your camera goes here").normalized() \$\endgroup\$
    – Cei
    Commented Jul 15, 2023 at 20:06

0

You must log in to answer this question.

Browse other questions tagged .