How to don't face player to camera direction?

Hello!

I’m using that script to rotate my player in a 2.5D game:

using UnityEngine;
using System.Collections;
 
public class MoveLook : MonoBehaviour {
 
    public float lookSpeed = 10;
    private Vector3 curLoc;
    private Vector3 prevLoc;
 
    void Update () 
    {
       InputListen();
       transform.rotation = Quaternion.Lerp (transform.rotation,  Quaternion.LookRotation(transform.position - prevLoc), Time.fixedDeltaTime * lookSpeed);
    }
 
    private void InputListen()
    {
       prevLoc = curLoc;
       curLoc = transform.position;
 
       if(Input.GetKey(KeyCode.A))
         curLoc.x -= 1 * Time.fixedDeltaTime;
       if(Input.GetKey(KeyCode.D))
         curLoc.x += 1 * Time.fixedDeltaTime;
       if(Input.GetKey(KeyCode.W))
         curLoc.z += 1 * Time.fixedDeltaTime;
       if(Input.GetKey(KeyCode.S))
         curLoc.z -= 1 * Time.fixedDeltaTime;
 
       transform.position = curLoc;
 
    }
}

It works perfectly but when the player is in idle state it faces to Z axis, not to the current position.

My game is like a 2D Game but you can move to Z axis too. I have an static camera that’s only moves to x and y axis but alway is looking to Z axis.

Im trying to solve this but im stuck.

1 Answer

1

If I understand what you are asking you want the last rotation left alone if the player is not moving.

void Update () {
    if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.S)) {
       InputListen();
       transform.rotation = Quaternion.Lerp (transform.rotation,  Quaternion.LookRotation(transform.position - prevLoc), Time.fixedDeltaTime * lookSpeed);
    }
}

That's work. Now i'm trying to modify that script to a Joystick input like EasyJoystick. I think the easy way it's changing the "if(Input.GetKe..." for my own joysticks axis inputs, right?

You'll have two axes, one for horizontal and one for vertical. This means that you code will allow your character to rotate arbitrarily rather than just the four directions.

Yep, my character rotates well when input Axis its 1 or -1, if X and Y axis are active at same time it rotate arbitrarily. How to fix that?