Simple help with Simple Script
Hi, I have a third person character controller which moves to all direction but always facing to the camera direction (like an aiming camera/like Skyrim, for example). I need to know what do I need to change in the last two rows to change that so the character faces the direction he is moving towards (but stays relative to the camera, like Uncharted/GTA).
Here's the script:
public class Movement : MonoBehaviour { Animator anim; Transform cam;
public float MaxSpeed = 1;
public VirtualJoystick joystick;
void Start ()
{
anim = GetComponent<Animator>();
cam = Camera.main.transform;
}
void Update()
{
if (anim == null) return;
var x = joystick.Horizontal();
var y = joystick.Vertical();
Move(x, y);
}
private void Move(float x, float y)
{
anim.SetFloat("Speed", x);
anim.SetFloat("Speed", y);
if (x != 0 || y != 0)
{
transform.rotation = Quaternion.Euler(0, cam.transform.eulerAngles.y, 0);
if (Input.GetKeyDown(KeyCode.LeftShift))
{
anim.SetBool("Sprint", true);
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
anim.SetBool("Sprint", false);
}
}
transform.position += (Vector3.ProjectOnPlane(cam.transform.forward, Vector3.up).normalized * MaxSpeed) * y * Time.deltaTime;
transform.position += (Vector3.ProjectOnPlane(cam.transform.right, Vector3.up).normalized * MaxSpeed) * x * Time.deltaTime;
}
}
Thanks.
Comment
Your answer
Follow this Question
Related Questions
Help a tired man with #C..... (Record Player in Horror Game) 1 Answer
Crafting script Help 0 Answers
Adding scripts to Unity 1 Answer
Particles moving past gun. Please help. 1 Answer
script help 1 Answer