Hi all, I'm working on making a 2D platformer game that has a shooting element. What I have is a script that gets the touch input and rotates the gun towards the touch location.
What I need to do is have it always try to move back to 0 degrees gradually. I've tried a Lerp but I couldn't get it to work correctly.
Here is what I have:
for (var touch: Touch in Input.touches) {
mouse_pos = Input.mousePosition;
if (touch.position.x > Screen.width / 2){
mouse_pos = Input.touches[0].position;
mouse_pos.z = 5.23; //The distance between the camera and object
object_pos = Camera.main.WorldToScreenPoint(target.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
angle = Mathf.Clamp(angle, -22, 40);
transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
}
}
So if you know how I might achieve this I'd greatly appreciate all suggestions or solutions, thank you very much!
↧