Search Unity

Rotation clamp

Discussion in 'Scripting' started by Lostwanderer1, Jul 29, 2019.

  1. Lostwanderer1

    Lostwanderer1

    Joined:
    Mar 11, 2019
    Posts:
    27
    How to clamp rotation of object like in the game wobble 3d?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    One thing to keep in mind about rotation is that Euler values should only ever be written to, never read from. This means that you can't just clamp the Euler values of the rotation and call it a day.

    What you should do instead is track the amount in both axes that the object is to be rotated outside of the rotation itself, and clamp those values. So, you'd have two floats xRotation and zRotation, make the user input change those values frame to frame, clamp them, and assign them to the rotation via Quaternion.Euler.
     
  3. Lostwanderer1

    Lostwanderer1

    Joined:
    Mar 11, 2019
    Posts:
    27
    Can you help me in the code ?
     
  4. ProtagonistKun

    ProtagonistKun

    Joined:
    Nov 26, 2015
    Posts:
    352
    If you post it, we can look at what you need and what the problem is. Asking "Can you look at my code" is a bit of a weird way of asking for help. Post the code in question and the problem you are having, be as clear as possible and we can probably figure out what needs to be changed.
     
  5. Lostwanderer1

    Lostwanderer1

    Joined:
    Mar 11, 2019
    Posts:
    27
    private Touch touch;

    private Vector2 touchposition;

    private Quaternion rotationx, rotationz;

    private float tiltspeedmodifier = 0.1f;

    void Update(){

    if(Input.touchCount>0)
    {

    touch = Input.GetTouch(0);

    switch(touch.phase){

    case TouchPhase.Moved:

    rotationx=Quaternion.Euler(
    -touch.deltaPosition.x * tiltspeedmodifier,0f,0f);

    transform.rotation= rotationx * transform.rotation;

    rotationz= Quaternion.Euler(
    0f,0f, -touch.deltaPosition.y * tiltspeedmodifier);

    transform.rotation= transform.rotation * rotationz;




    break;

    }


    }
    }
    }
     
  6. Lostwanderer1

    Lostwanderer1

    Joined:
    Mar 11, 2019
    Posts:
    27
    I need rotation clamp in this.
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    When you post code, use [code ]code tags[/code].

    Make rotationx and rotationz floats instead of Quaternions, and add the appropriate deltaPosition value to them each frame. It'll look a little bit like:
    Code (csharp):
    1. float rotationx = 0f;
    2. void Update() {
    3. ...
    4. rotationz += Input.deltaPosition.x;
    5. transform.rotation = Quaternion.Euler(rotationx, 0f, rotationz);
     
  8. Lostwanderer1

    Lostwanderer1

    Joined:
    Mar 11, 2019
    Posts:
    27
    Kind of confused here, can you elaborate the code?