Rotate object to joystick direction
What i want to do is take an object along the Y axis and rotate it to whatever direction the joystick is rotating towards. I have not been able to find a suitable answer. I got it to point forward, left and right, but only the absolute left and right, and i can't point the object backwards at all
Here's the code for reference.
public float speed;
private Vector3 Target;
private float target;
private void Update()
{
target = Input.GetAxis("Right Stick L/R") + Input.GetAxis("Right Stick U/D");
Target.Set(target, 90, 0);
Quaternion rotation = Quaternion.LookRotation(Target);
transform.rotation = rotation;
}
Answer by JustinC · Jan 20, 2018 at 05:46 PM
I know this is an older post, don't know if you have figured it out yet or not. If i understand your question, you want your object to rotate in the same orientation as your joy stick. I have a bit of code that I am using right now with the virtual joystick in the standard assets. works pretty well for me.
`void Twist ()
{
h1 = CrossPlatformInputManager.GetAxis("Horizontal"); // set as your inputs
v1 = CrossPlatformInputManager.GetAxis("Vertical");
if (h1 == 0f && v1 == 0f) { // this statement allows it to recenter once the inputs are at zero
Vector3 curRot = twistPoint.transform.localEulerAngles; // the object you are rotating
Vector3 homeRot;
if (curRot.y > 180f) { // this section determines the direction it returns home
Debug.Log (curRot.y);
homeRot = new Vector3 (0f,359.999f, 0f); //it doesnt return to perfect zero
} else { // otherwise it rotates wrong direction
homeRot = Vector3.zero;
}
twistPoint.transform.localEulerAngles = Vector3.Slerp (curRot, homeRot, Time.deltaTime*2);
} else {
twistPoint.transform.localEulerAngles = new Vector3 (0f, Mathf.Atan2 (h1, v1) * 180 / Mathf.PI, 0f); // this does the actual rotaion according to inputs
}
}`
Let me know if this is what you are looking for
Plz where I can put this code for rotation my joystick
If you don't understand how to use this code, I highly suggest you learn more about program$$anonymous$$g and Unity in general. You will be struggling to do basic things if you do not know how the above code works. Watch a lot of youtube videos or sign up for some classes at a local college.
To answer your question, this function can go anywhere you want it to. All you need is a public class level variable (of type GameObject) in the script named "twistPoint". Then either make sure you have the CrossPlatformInput$$anonymous$$anager or just change that to Unitys default input system. You can call the function from Update(). Once your script is ready, make sure you assign a GameObject to the "twistPoint" variable in the inspector.
If that didn't make sense to you, try looking up some of the things I mentioned. Not phrases but specifics. Search for things like: "GameObject", "Class Level Variable", "Unity C# Syntax", "Unity Input $$anonymous$$anager" just to name a few.
A$$anonymous$$AZINNNNNG. Just what I was looking for for so many days. Thanks! You've posted an answer months later the topics posting and helped a lot. We're in 2020 and it saved my project. ^^ thanks a lot.
Answer by areejyahya · Oct 07, 2018 at 04:06 PM
Plz where I can put this code for rotation my joystick