Search Unity

Suddenly the code does not react

Discussion in 'Scripting' started by Shayke, Mar 8, 2019.

  1. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    Hi, my code was working great untill a moment i did not notice it does'nt.
    I was making some animations and now i have no clue why the object is not rotating!
    Code (CSharp):
    1.     public void FollowMouse(Vector3 mouseXY)
    2.     {
    3.      
    4.         mouseXY = GetWorldPositionOnPlane(Input.mousePosition, 0);
    5.  
    6.         Vector3 vectorToTarget = mouseXY - transform.position;
    7.         float angle = Mathf.Atan2(vectorToTarget.y*-1, vectorToTarget.x*-1) * Mathf.Rad2Deg;
    8.         Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
    9.         transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * 15);
    10.      
    11.  
    12.     }
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    Use Debug.Log, or / and VS breakpoints, to track issues, of what changes what is not.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Did you insert some debug messages to see if the method is being called and if the values are what you expect them to be?

    @Antypodish beat me to it. lol.
     
    Antypodish likes this.
  4. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    I print the transform.rotation and this is what i get:
    What else could it be?
    I checked the animation and i did'nt touch the Z axis
    upload_2019-3-8_17-21-1.png
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    If your object don't rotate, then it means you override its rotation somewhere else.
    Or transformation.
     
  6. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    Yea i know, i have just 4 scripts and only 1 affect my player to rotate.
    So i am kinda certain that the problem is not in the script.
    It has any connection to the animator.
    if i put the animator on Animate Physics so it works but not as before.
    Any ideas? :confused:
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    Try on different object, to see, if you experience same issue.
     
  8. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    It works on other object.
     
  9. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    So you need now figure out, what is different, and keep narrowing down the issue.
     
  10. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    I solved the problem but i don't know the reason :\
    I just made a new Animator Controller with the same animations and it worked.
    Thanks for the help!
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Animations can drive and override rotations. That's probably what was happening.