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 srafciger · Jul 05, 2011 at 10:51 PM · animationinputdelaywait

Animation delay until certain distance

I have two objects in scene/level, one ball and a man who is supposed to hit that ball.

I already made the animation and script to play it in Unity after key press.

But almost every time man's hand misses the ball.

My question : How can I delay animation (after I press the key) until the ball is on certain distace to the man ?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by save · Jul 05, 2011 at 10:57 PM

For precise distances there's this handy tool.

 var theDistance = Vector3.Distance(man.position, ball.position);

For not as precise but quicker calculated try this.

 var sqrLen = (man.position - ball.position).sqrMagnitude;

Then you could for instance say:

 private var ballDistance : float = 0.0;
 var man : Transform;
 var ball : Transform;
 
 function Update () {
     ballDistance = Vector3.Distance(man.position, ball.position); //Calculate distance
     if (Input.GetKeyDown(KeyCode.S)) {
         swing(); //Drop to a coroutine
     }
 }
 function swing () {
     while ( ballDistance>0.5 ) yield; //Wait for the right moment
     animation.Play(); //Call your animation from here
 }

(Not tested)

You could also, instead of calculating distance (which does take some CPU-cycles), work with a collider set as a trigger. When the key is pressed set a boolean to true and when the ball enters the collider check that boolean, then fire the animation.

Comment
Add comment · Show 2 · 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 srafciger · Jul 06, 2011 at 08:47 AM 0
Share

Good answer, needed a few changes but generaly good.Thank you!

avatar image save · Jul 06, 2011 at 08:53 AM 1
Share

Glad it worked out, next time post a little bit of code so you wont have to restructure any code samples you get from us - makes it easier for both parts. ;-)

avatar image
0

Answer by Daemon0x0 · Dec 12, 2012 at 06:46 PM

Hi, maybe just try to use a collider at bone and handle direct this bone. I made somethink similar and I direct rotated the knee and hip bones when the ball touched the collider at toeBone. It worked fine.

Comment
Add comment · 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
0

Answer by Daemon0x0 · Dec 12, 2012 at 06:46 PM

Hi, maybe just try to use a collider at bone and handle direct this bone. I made somethink similar and I direct rotated the knee and hip bones when the ball touched the collider at toeBone. It worked fine.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

animation delay or wait 4 Answers

How to select an animation clip by index number? 7 Answers

Walking Animation Problem - Delaying 0 Answers

How to make realistic 2D balloon physics 0 Answers

How do I make it so that it wont edit a variable again for a certain ammount of time? C# 3 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