- Home /
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.
Ok. That's the correct script for EasyJoystick, works perfectly.
using UnityEngine;
using System.Collections;
public class $$anonymous$$oveLook : $$anonymous$$onoBehaviour {
public float lookSpeed = 10;
public EasyJoystick joystick;
private Vector3 curLoc;
private Vector3 prevLoc;
void Update ()
{
if(joystick.JoystickAxis.x > 0.1 || joystick.JoystickAxis.y > 0.1 || joystick.JoystickAxis.x < -0.1 || joystick.JoystickAxis.y < -0.1 ){
InputListen();
transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.LookRotation(transform.position - prevLoc), Time.fixedDeltaTime * lookSpeed);
}
}
private void InputListen()
{
prevLoc = curLoc;
curLoc = transform.position;
if(joystick.JoystickAxis.x < 0)
curLoc.x -= 1 * Time.fixedDeltaTime;
if(joystick.JoystickAxis.x > 0)
curLoc.x += 1 * Time.fixedDeltaTime;
if(joystick.JoystickAxis.y > 0)
curLoc.z += 1 * Time.fixedDeltaTime;
if(joystick.JoystickAxis.y < 0)
curLoc.z -= 1 * Time.fixedDeltaTime;
transform.position = curLoc;
}
}
Answer by robertbu · Sep 21, 2013 at 02:11 PM
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.Get$$anonymous$$e..." 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?
Your answer
Follow this Question
Related Questions
Camera not rotate when following player 1 Answer
How to get camera to follow player 2d 11 Answers
Why does the camera smoothly follow in one direction but not the other? 0 Answers
Player look in camera direction problem 1 Answer
How can I respawn my RigidBodyFPSController facing new direction? 0 Answers