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

Cannot rotate GameObject from C# with Animator attached?

Discussion in 'Scripting' started by Selzier, Mar 25, 2015.

  1. Selzier

    Selzier

    Joined:
    Sep 23, 2014
    Posts:
    652
    I have tried almost every api call that lets you rotate a gameobject, and this thing will NOT rotate via code, using C#.
    • It has an animator attached that animates rotation, but the rotation animation is not playing when I try to rotate from code (only a position animation is playing).

    • The gameobject & parents are not static.

    • Have tried rotation gameobject and parent, neither one will rotate.

    • Have tried euler angles rotations, quaternion rotations, transform.lookat, no errors but the thing will never rotate from it's original position while playing the position animation (up-down motion animating Y value of position.
    I even recorded a youtube video to help explain the problem:


    Why can't I rotate this gameobject from code? Any help much appreciated!!
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    if its the animator interfering, putting rotation code into LateUpdate should fix your problem
     
  3. Selzier

    Selzier

    Joined:
    Sep 23, 2014
    Posts:
    652
    So it looks like this fixes it, thanks James.

    Need some help still though, see I'm not doing anything on each frame only calling "transform.lookAt" one time, so update or LateUpdate not the best because I have to check if I should run that line of code every frame. For example (this works finally though!):

    void LateUpdate(){
    if (shouldLookAtPlayer == true) {
    transform.LookAt(transform.position);
    }
    }

    So now I have to set a bool, and then every frame it has to check if that's true or false, then run my line of code. Is there a way to run function "late" without LateUpdate?

    For example, something like:

    void MyFunctionThatRunsLate(){
    transform.LookAt(transform.position);
    }

    And then I can just call that without running a bool check every frame?
     
  4. Selzier

    Selzier

    Joined:
    Sep 23, 2014
    Posts:
    652
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    tbh, a bool check every frame is nothing performance wise..

    Im not too clear on why you are needing the bool.. you could null check the player transform instead I guess. There is no function later than lateupdate in the typical frame cycle
     
  6. Khuram021

    Khuram021

    Joined:
    Dec 15, 2015
    Posts:
    3
    we can try it by adding raycast on the bones in my case.
    i apply animation on my gameObject,animation works fine but the transform.rotation does not changes.so i can not detect which side is been touched by the boundry.i applied raycast to bones.because bones will rotate and raycast will move along with them.this works for me.