Rotating object with limit

Updated on January 1, 2017 in  [R] Scripts
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
2 on January 1, 2017

I cant write a proper script to rotate 2D object using W and S keys with limit from 360 degree to 270 degree. I tried this: 

using UnityEngine;

using System.Collections;
public class Rotate_cannon : MonoBehaviour {

// Use this for initialization

void Start () {
}
// Update is called once per frame

void Update ()

{

if(Input.GetKey(KeyCode.W))

{

transform.Rotate(-Vector3.back * 100 * Time.deltaTime);

}

if(Input.GetKey(KeyCode.S))

{

transform.Rotate(Vector3.back * 100 * Time.deltaTime);

}

Vector3 clampedRotation = transform.eulerAngles;

// Clamp the X value

clampedRotation.z = Mathf.Clamp(clampedRotation.z, 360, 270);

// assign the clamped rotation

transform.rotation = Quaternion.Euler(clampedRotation);

}
}

Can somebody please tell me, where is the problem? Thank you.

  • Liked by
Reply
1 on January 1, 2017

36 | clampedRotation.z = Mathf.Clamp(clampedRotation.z, 270, 360);

Mathf.Clamp goes from lowest to highest.

on January 1, 2017

Thank you.

Show more replies
  • Liked by
Reply
Cancel