- Home /
Making my Game Object face according to direction of mobile joystick
Hey guys I am making a footstep system for a game but i am facing a really weird issue with rotation. i want to make my GameObjects rotation change to the direction where mobile joystick points but i cant really get it work
private void Update()
{
Vector3 touchPosition = joy.transform.position;
footSteps.transform.eulerAngles = new Vector3(footSteps.transform.eulerAngles.x, Mathf.Atan2(touchPosition.y, touchPosition.x) * Mathf.Rad2Deg, footSteps.transform.eulerAngles.z);
}
this is the code i am currently using. i searched all the threads regarding this issue but none of the solutions worked
look at the video to know the result of this code. https://vimeo.com/251559839
Answer by look001 · Jan 17, 2018 at 10:32 PM
just use the unity LookAt function. Something like this:
Vector3 touchPos = joy.transform.position;
footSteps.transform.LookAt(footSteps.transform.position + touchPos);
EDIT:
Since your up Vector is not pointing in the looking direction this might not work. Also i have to admit that this isn't the best solution because you probably only want to rotate the footsteps in one axis. However its the fastest to do. Here is another solution where you can specify the forward direction:
Vector3 stickPos = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0f);
Vector3 lookingDir = Vector3.right;
if (stickPos != Vector3.zero) {
float angle = Vector3.Angle (lookingDir, stickPos);
if (Vector3.Cross (lookingDir, stickPos).z < 0f) {
angle = -angle;
}
transform.rotation = Quaternion.Euler (new Vector3 (0f, angle, 0f));
}
you will have to change the stickPos to your joy position. If the player points in the wrong direction after that you might need to change the < 0f to >0f. I highly recommend to check the code instead of copying it and hope it works. Good luck ;)
I have to admit this might not work instantly. It rotates weirdly on the x axis becase your up vector of yout footsteps isn't pointing forward (its a bit confusing). The LookAt function points the up vector of an object to a point and the up vector of your footsteps is not pointing the direction. To fix this you can change your models axis in a modelling program or use the other solution i have added to the reply. I think its the better solution anyway :)
Answer by bhavinbhai2707 · Jan 17, 2018 at 10:40 PM
@look001 Didn't work either All what happened was the model got weirdly rotated on x axis
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Flip over an object (smooth transition) 3 Answers
How to control a 2d character in an top-down android game with the standard joystick? 1 Answer
Distribute terrain in zones 3 Answers
Why doesn't my game update on Android Tablet unless I'm clicking the screen? 1 Answer