Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Drone rotation clamp?

Discussion in 'Scripting' started by Jeepster, Dec 6, 2022.

  1. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Hi,

    I'm trying to make drone like movements for my drone game object and so far so good, the drone is moving and rotating based on horizontal and vertical input but now I need to clamp the rotation so that it doesn't rotate too much or even make a full 180.

    How do I clamp rotation based on my rotation code below so that it can only rotate 30 degrees in any direction?
    Code (CSharp):
    1.  
    2.  if(moving)
    3.             {
    4.                 transform.Rotate(Vector3.right, Input.GetAxis("Vertical") * 90 * Time.deltaTime, Space.Self);
    5.                 transform.Rotate(Vector3.left, -Input.GetAxis("Vertical") * 90 * Time.deltaTime, Space.Self);
    6.  
    7.                 transform.Rotate(Vector3.back, Input.GetAxis("Horizontal") * 90 * Time.deltaTime, Space.Self);
    8.                 transform.Rotate(Vector3.forward, -Input.GetAxis("Horizontal") * 90 * Time.deltaTime, Space.Self);
    9.  
    10.              
    11.              }
     
  2. TzuriTeshuba

    TzuriTeshuba

    Joined:
    Aug 6, 2019
    Posts:
    185
    Jeepster likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514