Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by tmalhassan · Nov 13, 2017 at 11:45 AM · animationunity 5movementvariablerandom

Creating an animation with variables?

So I have been looking for a way to play an animation with different values. For example, I want to move my player from position 0 to position 100, and when I play the animation again, I want it to move from 25 to 75. I looked online and all I could find is suggestions to use a script for the movement instead of an animation. But I would like to know if there is a possible way to achieve this with an animation? Any help is very much appreciated. alt text

screenshot-7.png (35.0 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Hellium · Nov 15, 2017 at 11:14 AM

The AnimationCurve API provides function to set curves. I've not tested the script myself, but it's a starting point:

 public AnimationClip clip ; // Drag & Drop the clip to change

 void Start()
 {
         float startTime = 0 ;
         float startValue = 0 ;
         float endTime = 60 ;
         float endValue = 100 ;
         string objectName = "" ; // Leave empty if the target object is the one with the animator component
         AnimationCurve curve = AnimationCurve.Linear( startTime , startValue , endTime , endValue );
         string relativeObjectName = string.Empty; // Means the object holding the animator component
         clip.SetCurve( relativeObjectName, typeof(Transform), "localPosition.x", curve );
     }



Source :

  1. https://docs.unity3d.com/ScriptReference/AnimationClip.html

  2. https://docs.unity3d.com/ScriptReference/AnimationClip.SetCurve.html

Comment
Add comment · Show 7 · 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 tmalhassan · Nov 22, 2017 at 09:40 AM 0
Share

Hello @Hellium and thank you very much for the answer. First of all, I would like to deeply apologize for the late reply as I was building the environment where I can test the code. Second of all, I tested the code and I tried to get it to work, but there are some things that I couldn't actually understand. for instance, how do we assign the game object using the string? so I have a parent object (the parent contains the animator) that contains the 2 children that should rotate randomly. So I changed the string objectName to the same name of my child object. After I start the game nothing happens with the animation. But when I check the animation window I find a new missing property with the name that I created. So the question now is how do I assign that gameObject? alt text

Also, this is the warning that I get:

Could not register property modification for animation binding rotation.z of object Pointer, modifications will not be automatically reverted

screenshot-10.png (331.6 kB)
avatar image Hellium tmalhassan · Nov 22, 2017 at 10:18 AM 0
Share

Can you indicate the property you animate when calling clip.SetCurve ? It seems you can't animate world rotation (nor world position, nor world scale). You have to call :

     clip.SetCurve( "Pointer", typeof( Transform ), "localRotation.x", curve );

Be careful when dealing with rotation,

avatar image tmalhassan Hellium · Nov 22, 2017 at 11:27 AM 0
Share

The property that I am trying to animate is rotation.z. And it's not (Transform), it is a (RectTransform) since I'm working on a canvas. After making the changes that you suggested, the error is gone. But weirdly, it immediately snaps to -180 in rotation, neglecting the curve's startValue and endValue.

Show more comments
avatar image
0

Answer by Max_Bol · Nov 13, 2017 at 11:55 AM

It's possible to do it with an animation, but in the end you'l always have to work with a script that will manage the animation regardless. (Unless you create an insane system that includes hundreds of animations covering all the distances. Total waste of time.)

To "move" a player based on the animation, you got to use the Root Motion: https://docs.unity3d.com/Manual/RootMotion.html

This obviously requires you to have the animation with Root Motion (meaning that the animation includes the forward movement of each steps.) Then you can handle the animation while keeping track of the distance between the player and the target position through the script. The player than move according to the animation while orient itself and determines if it has to play the animation or not based on the script which check the distance and orientation of the player. This doesn't requires any "movement" of the player. Only Orientation & Distance calculation to be used as condition. (You can even handle the orientation through the animation, but you still need to script the detection of when the player is looking toward the right place.)

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 tmalhassan · Nov 13, 2017 at 12:19 PM 0
Share

It's not really about the Root $$anonymous$$otion (Since my player is a just an object: ball, box, .... etc.), it's the part about randomizing the value that I'm trying to achieve. And of course, I will be using the script to deter$$anonymous$$e the random value. It's just how to you make to object move from and to these random values? I updated the Question with a picture to explain more.

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

267 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 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

root motion on in place animation 0 Answers

Horizontal axis not working after animation 1 Answer

How to combine anim states with movement in a random movement game. 1 Answer

How to prevent Homanoid Mocap Animation Z-axis movement? 0 Answers

How to make a model move during timeline in Cinemachine. 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