Hi, I’m currently learning C# and Unity with a 2D tank game I’m building for experience. I was able to write a script for my tank that allows the turret to rotate over the z axis 2f and -2 if you hold R or T. The problem is I want the nose to stop rotating at a certain degree (36 and 348) so that the angle of the shot being fired is limited and the nose doesn’t come off of the tank and look strange. Below is what I have so far to make the turret move, but I don’t know where to go from there. Any advice would be great. Thank you
[SerializeField]
private float noseAngleP;
[SerializeField]
private float noseAngleN;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
tankRotate ();
tankStop ();
}
private void tankRotate()
{
if (Input.GetKey (KeyCode.R)) {
transform.Rotate(0,0,2f);
}
if (Input.GetKey (KeyCode.T)) {
transform.Rotate (0, 0, -2f);
}
}
private void tankStop()
{
if (noseAngleP > Quaternion.eulerAngles.z) {
transform.Rotate (0, 0, 0);
}
if (noseAngleN < Quaternion.eulerAngles.z) {
transform.Rotate (0, 0, 0);
}
}
}
Awesome, thanks for the comment. First bullet. I set the number for P and N from the [SerializeField] which does the same thing as public but is instead private (afaik). So I can set the numbers in the Inspector. Second bullet. I realize now that 348 wouldn't really work since it's greater than 40. I think I could get it to work if the angle was negative, but once I go passed 0 it just goes to 358 and freezes. I changed the less than sign to greater than and it allows me to go to the set value (40) and move back down to 358 but once I get there I can't go back up to 40. Any thoughts?
– schladogNo problem, glad it works for you. :) Happy learning.
– ricke44654void onTriggerEnter() { } should be void OnTriggerEnter(Collider other) { } The o needs to be capitalized. Everything needs to be exact. https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html
– voncarp