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

Animator and Script Rotation

Discussion in 'Scripting' started by MathewHI, Jun 4, 2021.

  1. MathewHI

    MathewHI

    Joined:
    Mar 29, 2016
    Posts:
    501
    The Animator blocks any rotations I try to add in a script. How can I have both?
     
  2. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Well giving more info would be a good start, post your script, seeing what you already have would be great!
     
  3. Ian_1337

    Ian_1337

    Joined:
    May 13, 2021
    Posts:
    49
    Hi, Don't know if this helps as an example, I'm fairly new to Unity.

    I used a Unity "Standard asset" character controller free from the asset store, an animator and Cinemachine for different camera states. I started with the blank FPS character, but it comes with a 3rd person one setup as an example (similar to this);

    upload_2021-6-4_11-43-23.png

    I downloaded a Ninja character from Mixamo to play around with (ch24_nonPBR) and it has an animator;

    upload_2021-6-4_11-44-56.png

    I then made a Ninja controller which has states;
    upload_2021-6-4_11-47-38.png

    Then I have Cameras > Animator (with a different animator)

    upload_2021-6-4_11-49-55.png

    This allows me to have animations running according to the input of the character.. but also the camera state is seperate (so if you want to rotate the camera around the player you can do that, for instance under my 'WalkCamera' settings I could change the camera to like rotate around him).

    Hope it helps
     

    Attached Files:

    Johan_Liebert123 likes this.
  4. Ian_1337

    Ian_1337

    Joined:
    May 13, 2021
    Posts:
    49
    Further to this (if it helps) in case I didn't make it clear, the animator for the camera states is not the ninja animator, the way it works is like this;
    upload_2021-6-4_12-1-41.png
    if my character walks it's on walk camera (it tells the ninja animator it's moving so it animates)
    if my character crouches or sneaks it's on crouch camera (it tells the ninja animator to crouch or sneak forwards)
    If my character sprints it changes the camera animator to sprint camera (animation=sprint)
    3rd person - I'm working on improving this, it works but it also changes the state of the character (so sprint state is quicker than walk state), so at the moment 3rd person is limited to walking speed only and I had to limit the animation to match the speed/state it's in (sprint animation disabled), I wasn't sure how to change just the camera and keep the original state it changed from lol.
     
    Johan_Liebert123 likes this.
  5. MathewHI

    MathewHI

    Joined:
    Mar 29, 2016
    Posts:
    501
    I just found out you can put it in LateUpdate to run after the Animator
     
  6. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Really good!! Like that's soo Informative. If I was you I'd just create a new thread as an example for everyone else! Create a tutorial on this, It'll be great (you can assure subs from me lol)
     
    Ian_1337 likes this.
  7. Ian_1337

    Ian_1337

    Joined:
    May 13, 2021
    Posts:
    49
    Thanks I'm still fairly new myself so still learning. I've dedicated the last 2-3 weeks or so to Unity in my spare time and so far I've created a working menu system with mp3 player from the asset store, 3d terrain that is infinite, a player character with animations all working on the new input system. Then separately I've started a project to make a hotbar and inventory but currently the examples I've been learning from use the old input system, so I'll be making one with the new input system soon I hope, I managed to improve one I downloaded and add more features but I stopped working on it for that reason. Today I've managed to get the 'Hunger and thirst' asset and I've changed the health text into a health bar in the same fashion which works with the hunger and thirst, you can turn it on or off and change the text to a number with percentage or just a number lol. Again this comes with examples of items for inventory but they use the old input system for picking them up or using them, converting to the new input system requires a bit of work! xD
     
  8. MathewHI

    MathewHI

    Joined:
    Mar 29, 2016
    Posts:
    501
    Thank you sir but I'm not sure if this will work for me I'll give it a try. I need to rotate a child gameobject that has an animator with new different rotations via script. I think it would be like trying to animate your Ninja character with new rotations in a script although you already have an animator on it. Someone told me use LateUpdate but thats not working for me either.
     
  9. Ian_1337

    Ian_1337

    Joined:
    May 13, 2021
    Posts:
    49
    hmm yeah my example is specifically for the player only, and changing the camera depending on the state the character is in (the animation state is based on the player input, the camera state changes if you are walking, sprinting or crouched - the camera state has many options for things like Lock to Target or others for rotating around the character for example).
    upload_2021-6-6_1-33-19.png

    If you need to rotate a different object with it's own animator (like an AI) i'm not sure exactly how, if you provide script or an example of what you're trying to achieve someone will surely help!
     
    Last edited: Jun 6, 2021
  10. Ian_1337

    Ian_1337

    Joined:
    May 13, 2021
    Posts:
    49
    Also did you check the Standard Assets 3rd Person player example? (it has a character model with animations and different camera looks I think, I didn't use it much I started making my own with the first person controller scripts)
     
  11. MathewHI

    MathewHI

    Joined:
    Mar 29, 2016
    Posts:
    501
    My player is set on a first-person pre-determined path I'm not using a controller. Actually I have something similar to your system, I have the body transform that has the animator then I have the camera/head transform that I can move with script rotations so far it seems to be working.
     
    Ian_1337 likes this.