Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by kott7123 · Sep 17, 2017 at 06:03 AM · scripting problemanimatorrotateanimationsthird person shooter

How to rotate spine by the script, while an animation from the animator is playing?

Hi! I want to create third person shooter, but I have got one problem. I want to rotate shooter's spine by script, during an animation from the animator. And while the animation is playing, I can't rotate spine by the script. I don't know how can I do it. I'll be very grateful if someone helps me.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by Arocide · Sep 17, 2017 at 11:42 AM

When you begin using Mecanim for Animations you must play by its rules. Mecanim updates transforms later then Update and FixedUpdate messages, so modifying transforms in there that are now under the control of the Animator will just be overridden. So in order to tell Mecanim we want to look at a certain point or procedurally change the rotations of bones we must use Inverse Kinematics (IK). The first step to using IK in Unity is to enable an 'IK Pass' on your Animation layer the documentation here shows you where the tick box is to enable that.

Once you have done that the Animation system will now perform Inverse Kinematics calculations and provides you with a call back to use in your scripts with the MonoBehaviour.OnAnimatorIK message. You may be able to simply use the inbuilt LookAt functionality to get what you are looking for. If so it could be as simple as the following to get what you are looking for:

 using UnityEngine;
 
 public class LookAt : MonoBehaviour
 {
 
     public Transform lookTarget;
     public float lookWeight;
     public float bodyWeight;
     public float headWeight;
     public float eyesWeight;
     public float clampWeight;
 
     Animator animator;
 
     void Start()
     {
         animator = GetComponent<Animator>();
     }
 
     private void OnAnimatorIK(int layerIndex)
     {
         // Set our Look Weights for this pass
         animator.SetLookAtWeight(lookWeight, bodyWeight, headWeight, eyesWeight, clampWeight);
  //Now set our position to look at for this pass
         animator.SetLookAtPosition(lookTarget.position);
 
     }
 }
 

Where lookTarget is the GameObject your looking at (The Editor will still allow you to drag and drop GameObjects into a Transform type variable, unless you need the actual game object for a reason why not save a variable). You can find out what the parameters mean by looking at the documentation Animator.SetLookAtWeight and Animator.SetLookAtPosition

However if this does not work for you, you can also look at other possibilities such as Animator.SetBoneLocalRotation (The documentation actually suggests that it can be used in a similar situation to what you are looking for).

A nuclear option does exist if you do not wish to use IK, you can use the LateUpdate message in your script to override the animation systems transforms for bones, but that is not the best practice I find.

Hopefully this will get you on the right track to getting that character of yours pointing in the right direction! Best of Luck!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ishidres · Mar 08, 2020 at 10:59 PM 0
Share

Hello! Thanks for the awesome explanation! I didn't know you could simply use .SetLookAtPosition() to make the character look at a certain position.

I have the following problem: $$anonymous$$y character holds a weapon. When using the above method, it does look at the specific position, however, the weapon doesn't aim at that position, too. How would you affect the weapon to "look" (or more like aim) at the specific position, too?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

172 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[HELP PLEASE] How can i check if an animator transition from animation to animation has finished? 3 Answers

Attack Script 2 Answers

NEED HELP ANIMATING PLAYER MOVEMENT 0 Answers

Confused about animations 1 Answer

Multiple Enemies Animator Problem 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges