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;
}
@Drksteel,
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