Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
2
Question by macsimilian · Sep 02, 2021 at 01:42 PM · motioncardpathfollowingboardgamepaths

How to get object to follow a curved path to reach a moving destination?

I am making a board/card game where cards are moving around. For example, cards can go from the deck to the player's hand. However, because of how the player's hand adjusts itself for different kinds of cards, it's possible that the player's hand will change before the card from the deck reaches its destination.

How to make a card follow a somewhat curved path to its moving destination? I would also like for the card to smoothly speed up and slow down along this path, so preferably the method would maintain a consistent speed and wouldn't have to speed up/slow down to adapt to a different destination.

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
3
Best Answer

Answer by andrew-lukasik · Sep 02, 2021 at 07:52 PM

animation preview

(exaggerated rotations are intentional, you can disable them by editing rotation interpolation curve)_

animate_cards_answer.unitypackage right click / save link as file
 using System.Collections;
 using UnityEngine;
 
 public class CardsAnim : MonoBehaviour
 {
 
     [SerializeField] Transform _p0, _p1, _p2, _p3;
     [SerializeField] Transform _card;
     [SerializeField] float _duration = 2f;
     [SerializeField] AnimationCurve _translationInterpolation = new AnimationCurve(
         new Keyframe(0,0) , new Keyframe(0.03f,0.03f) , new Keyframe(0.3f,0.03f) , new Keyframe(1,1,4.3f,0)
     );
     [SerializeField] AnimationCurve _rotationInterpolation = new AnimationCurve(
         new Keyframe(0,0) , new Keyframe(0.82f,5.7f,26f,26f) , new Keyframe(1,1)
     );
 
     void OnEnable ()
     {
         StartCoroutine( AnimateCard() );
     }
 
     #if UNITY_EDITOR
     void OnDrawGizmos ()
     {
         if( _p1 ) { Gizmos.color = Color.red; Gizmos.DrawCube( _p1.position , Vector3.one*0.5f ); }
         if( _p2 ) { Gizmos.color = Color.blue; Gizmos.DrawCube( _p2.position , Vector3.one*0.5f ); }
         if( _p0 && _p1 && _p2 && _p3 )
         {
             Vector3 a = _p0.position, b = _p1.position, c = _p2.position, d = _p3.position;
             const int numSteps = 20;
             float dt = 1f / (float) numSteps;
             Gizmos.color = Color.white;
             for( int i=0 ; i<numSteps ; i++ )
                 Gizmos.DrawLine( SampleBezierCurve( i*dt , a , b , c , d ) , SampleBezierCurve( (i+1)*dt , a , b , c , d ) );
             Gizmos.color = Color.red;     Gizmos.DrawLine(a,b);
             Gizmos.color = Color.green; Gizmos.DrawLine(b,c);
             Gizmos.color = Color.blue;    Gizmos.DrawLine(c,d);
         }
     }
     #endif
 
     IEnumerator AnimateCard ()
     {
         float time = 0;
         yield return null;
         while( time<_duration )
         {
             float t = time/_duration;
             _card.position = SampleBezierCurve( _translationInterpolation.Evaluate(t) , _p0.position , _p1.position , _p2.position , _p3.position );
             _card.rotation = Quaternion.SlerpUnclamped( _p0.rotation , _p3.rotation , _rotationInterpolation.Evaluate(t) );
             
             yield return null;
             time += Time.deltaTime;
         }
         _card.position = _p3.position;
         _card.rotation = _p3.rotation;
 
         StartCoroutine( AnimateCard() );
     }
 
     static Vector3 SampleBezierCurve ( float t , Vector3 a , Vector3 b , Vector3 c , Vector3 d )
         => a*Mathf.Pow((1f-t),3) + b*(3f*t*Mathf.Pow((1f-t),2f)) + c*(3f*Mathf.Pow(t,2f)*(1f-t)) + d*Mathf.Pow(t,3f);
 
 }


animate-cards-answer.zip (6.1 kB)
9ohiu54wnihb0yp3io4jpwhrs-b9hijp4hrn0g-odkvzoiaejg.gif (405.3 kB)
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

125 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

Related Questions

Is this the right program 0 Answers

Curve Path Definition 0 Answers

Linking strings and integers? 1 Answer

Unity equivalent of GeekGameBoard? 3 Answers

How should I go about this? Creating Path with mouse/touch, upon release, follow the movement 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