Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Angle flip issue

Discussion in 'Scripting' started by smitchell, Jun 14, 2014.

  1. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Hey,

    So basically I'm having an issue when I'm trying to constrain an angle.

    I have an object that rotates towards the mouse, then it stops rotating within a angle restriction. The issue I'm having is when the mouse crosses over my object, works fine everywhere else except when that specific event happens.. It then flips the angle restriction.. Here's an image to demonstrate whats happening:



    So the red area is my restriction, on the left is where it should always be, the grey area is my object, the yellow line is the direction my mouse is moving (over the player) and on the right is what happens when the mouse has moved over the player.

    Does anyone know why this is happening?

    Here's the code I'm using to detect the angle

    Code (CSharp):
    1. if (rotation.eulerAngles.z >= 155f && rotation.eulerAngles.z <= 215f) {
    2.     //Dont rotate
    3. }
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    I forgot to mention rotation is a Quaternion
     
  3. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Is this to do with gimble lock?
     
  4. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Anyone?
     
  5. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    Is it possible to post a stripped down version of the code that calculates the rotation you're checking in your code snippet above?
     
  6. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Hi,

    Thanks for the reply. Yeah sure, attached the stripped down rotation code

    Code (CSharp):
    1. void RotateGun () {
    2.         mousePosition = Input.mousePosition;
    3.         mousePosition.z = -16;
    4.         mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
    5.      
    6.         //Rotate Gun shoot position
    7.         Quaternion rotation = Quaternion.LookRotation(mousePosition - gunSettings.shootPosition.transform.position, gunSettings.shootPosition.transform.TransformDirection(Vector3.up));
    8.         rotation.x = 0;
    9.         rotation.y = 0;
    10.      
    11.         if(!outOfBounds)
    12.             gunSettings.shootPosition.transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
    13.      
    14.         if(rotation.w < 0.2f && rotation.w > -0.2f)
    15.             outOfBounds = true;
    16.         else
    17.             outOfBounds = false;  
    18.      
    19.     }
     
  7. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    you can try something like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LookAtMouse : MonoBehaviour {
    5.  
    6.     public Vector3 target;
    7.     public GameObject player;
    8.  
    9.     void Start()
    10.     {
    11.         player = GameObject.FindGameObjectWithTag("Player");
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         float z = player.transform.position.z-Camera.main.gameObject.transform.position.z;
    17.         target = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, z));
    18.         float angle = Mathf.Atan2(target.x, target.y) * Mathf.Rad2Deg;
    19.        //Mathf.Clamp (angle, min, max); here to your restrictions before passing to player
    20.         player.transform.eulerAngles = new Vector3(0, 0, -angle);
    21.     }
    22. }
     
  8. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Thanks dude, I'll try it out when I get back to my PC

    So is my issue not to do with gimble lock? Do you know why it was happening?
     
  9. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    don't know just see some strange things, since i have the look at mouse script laying around Just show you how i would go about it...
    this i.e. doesn't make much sense...:
    Code (CSharp):
    1.   mousePosition = Input.mousePosition;
    2.         mousePosition.z = -16;
    3.         mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
    of these three lines the only one that counts is the last one... so no point it doing the previous two.
    as for
    Code (CSharp):
    1. Quaternion rotation = Quaternion.LookRotation(mousePosition - gunSettings.shootPosition.transform.position, gunSettings.shootPosition.transform.TransformDirection(Vector3.up));
    no clue what the other things are (gunsettings...) or if they are part of the problem/behaviour.

    I'm no quaternion expert so i listen to the ref ;-) : "W component of the Quaternion. Don't modify this directly unless you know quaternions inside out."
     
  10. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    I tried your code out and well the gun doesn't even rotate towards the mouse, it rotates just not towards the mouse.. The code I had before I managed to get rotating towards the mouse.

    you're correct that I don't need to do this:
    Code (CSharp):
    1. mousePosition = Input.mousePosition;
    2. mousePosition.z = -16;
    3. mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
    That's just something I've always done, Force of habit.

    I've changed it to this:
    Code (CSharp):
    1. mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -Camera.main.transform.position.z));
    "no clue what the other things are (gunsettings...) or if they are part of the problem/behaviour."
    - Nothing in gun settings would cause this issue, the only thing in gun settings thats used for the rotation is the transform of the guns shooting position

    I've simplified my code even more now

    Code (CSharp):
    1. void RotateGun () {
    2.         mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -Camera.main.transform.position.z));
    3.        
    4.         //Rotate Gun shoot position
    5.         Quaternion rotation = Quaternion.LookRotation(viewfinder.transform.position - gunSettings.shootPosition.transform.position, gunSettings.shootPosition.transform.TransformDirection(Vector3.up));
    6.        
    7.         if(!outOfBounds)
    8.             gunSettings.shootPosition.transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
    9.  
    10.         outOfBounds = gunSettings.shootPosition.transform.localEulerAngles.z > 155f && gunSettings.shootPosition.transform.localEulerAngles.z < 215f;
    11.     }
    I really need to find out why it's flipping
     
  11. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    here a package with an example scene of the LokAtMouse script working...
    maybe your setup is different...?

    edit: i'll change it so you can set bounds and repost the package later...
     

    Attached Files:

  12. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    package with bounds
     

    Attached Files:

  13. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    I figured out why your script wasn't working in my scenes it's because my gun is a child of an object, and your script doesn't account for local position, but when I changed it to localPosition it didn't work properly :(
     
  14. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    change it to localEulerAngles
    Code (CSharp):
    1. player.transform.localEulerAngles = new Vector3(0, 0, Mathf.Clamp(-angle, minBound, maxBound));
     
  15. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    I already tried that, It doesn't work :(
     
  16. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    well i don't know your setup, but if i drag my player into a empty parent object, keep the script on the player as a child and run it, it acts as suspected...?
     

    Attached Files:

  17. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Ok so I figured out that the reason this isn't working for me is to do with the way my scene is setup and the position of my object, I've attached a package to show you. The studio is really weird tho they insist it has to be setup like this, this is a really stripped down version, I assure you it gets even weirder..
     

    Attached Files:

  18. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    hmm yes something strange is happening ... this works thou:
    like in you drawing set the bounds min to -140 and max to -40 (i know bit strange... will sort out later why that is)

    Code (CSharp):
    1.  
    2.     void Update() {
    3.         float z = player.transform.position.z - Camera.main.gameObject.transform.position.z;
    4.         //Vector3 target = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, z));
    5.         Vector3 target = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -16f));
    6.         Vector3 direction = target-transform.position;
    7.    
    8.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    9.         player.transform.localEulerAngles = new Vector3(0, 0, Mathf.Clamp(angle-90, minBound, maxBound));
    10.         //player.transform.localEulerAngles = new Vector3(0, 0, angle-90);
    11.     }
     
  19. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Yeah it's really weird! I have no idea why it's happening, if you figure it out man then awesome :D
     
  20. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    did the code snippet above worked for you? it worked here if i made that change to your issue package.
     
  21. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Yeah it works, the bound values aren't correct but I'll figure that out