Joystick Orientation Wrong After Camera Rotation
I am using a virtual joystick to move & rotate the player and a touch swipe system for the camera rotation, The problem is that when I rotate the camera (Let's say I rotated the camera to 45 degrees on the left) the joystick orientation doesn't change with the camera & I have to move joystick to the left to move my character forward. What I want is that if I rotate the camera the joystick should also change its orientation so where the camera is facing the character should also move in that direction by moving the joystick up.
Joystick Script that I'm using
private Rigidbody Char;
private Joystick joystick;
void Start()
{
joystick = FindObjectOfType<Joystick>();
Char = GetComponent<Rigidbody>();
}
void Update()
{
Vector3 moveVector = (Vector3.right * joystick.Horizontal + Vector3.forward * joystick.Vertical);
// This line is for player's movement.
Char.velocity = new Vector3(joystick.Horizontal * 3f, Char.velocity.y, joystick.Vertical * 3f);
// This line is for player's rotation.
if (moveVector != Vector3.zero)
{
transform.rotation = Quaternion.LookRotation(moveVector);
}
}
}
Answer by bhanubirani · Dec 28, 2020 at 01:42 PM
@Fury1995 - Were you able to find the solution for this problem? I'm also facing the similar issue. In my ARKit project, when I move around, the joystick doesn't work as expected.
Here is the code I'm using:
private Joystick joystick;
void Start()
{
hits = new List<ARRaycastHit>();
joystick = FindObjectOfType<Joystick>();
}
void Update()
{
// rotate character as we move joystick
float angle = Mathf.Atan2(joystick.Horizontal, joystick.Vertical) * Mathf.Rad2Deg;
playerRigidBody.transform.rotation = Quaternion.Euler(new Vector3(0, angle, 0));
}
Here is the solution to this problem. This is exactly what will solve the problem.
https://www.youtube.com/watch?v=N34eNLzzfvQ
Works like charm!
I saw this video, and my joystick still with wrong orientation. Can you help me please?
Your answer
Follow this Question
Related Questions
Cinemachine Freelook Camera Movement with Joystick 3 Answers
How to move mouse cursor with joystick? 1 Answer
Input.AnyKey for gamepads? 0 Answers