- Home /
Lock rotation of tank barrel
I have created a working prototype for a tank game. The final thing that I cannot get right is stoping the angle of the barrel at a set angle to stop it going stright up looking unrealistic or digging into the ground. My current script is
using UnityEngine;
using System.Collections;
public class TurrentMouseVert : MonoBehaviour {
public float horizontalSpeed = 2.0F;
public float verticalSpeed = 2.0F;
void Update()
{
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis ("Mouse Y");
transform.Rotate (v, 0, 0);
//Debug.Log(transform.eulerAngles.x);
Debug.Log(transform.eulerAngles.y);
//Debug.Log(transform.eulerAngles.z);
}
}
I have removed my test part that included mathf.clamp to avoid confusion, please help
In the comments to my answer at the following link, you will find a script that handles a turret with arbitrary rotation of the base tank and has a limit on the barrel. You must open the comments to find the script:
http://answers.unity3d.com/questions/562443/turret-slerp-rotation-and-clamping.html
Answer by 432qwerty · Jul 28, 2014 at 04:56 PM
You need to use transform.eulerAngles.y as you have in the debug in an if statement to check that the rotation of the tank barrel is within certain bounds, and if not set the rotation of the barrel to be within the bounds using transform.rotation.
Hope this helps!
Answer by tommyblock · Jul 29, 2014 at 08:18 AM
Thanks for the responses guys but im still none the wiser. Is it possible to respond with part of the script written, thanks very much
Your answer
![](https://koobas.hobune.stream/wayback/20220613155219im_/https://answers.unity.com/themes/thub/images/avi.jpg)