Hello,
I was looking for a way to rotate my object, based on the touch screen.
If I touch the right side of the screen, my character has to turn to the right until I press, in contrast to the left.
I tried that but it does not seem to work:
void Update(){
ruota = this.transform.rotation;
if(Input.GetTouch(0).position.x > Screen.width-100){
ruota.y -= 0.2f;
this.transform.rotation = ruota;
}
if(Input.GetTouch(0).position.x < 100){
ruota.y += 0.2f;
this.transform.rotation = ruota;
}
}
ruota is a quaternion.
You don’t want to be assigning or manipulating individual components (x,y,z,w) of a Quaternion. Quaternions are 4D constructs that are not intuitive. Unity gives you a variety of ways of dealing with rotations (some with pitfalls), besides directly manipulating a Quaternion. For what your are doing here, I’d use Transform.Rotate():
public class RotateScript: MonoBehaviour {
public float speed = 45.0f;
void Update(){
if(Input.GetTouch(0).position.x > Screen.width-100){
transform.Rotate(0.0f, Time.deltaTime * speed, 0.0f);
}
if(Input.GetTouch(0).position.x < 100){
transform.Rotate(0.0f, -Time.deltaTime * speed, 0.0f);
}
}
}
‘speed’ will be the degrees per second the object will rotate. You want to scale your rotations by Time.deltaTime (as I’ve done here) to keep your rotation frame rate independent.