Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 jpierre88 · Jul 23, 2014 at 10:34 AM · rotationlerpmathf

GameObject jumps and spins towards destination

Hi, game developers! I need your help again. I want to make a gameObject jump and rotate to a location. So far I have the basic idea down but don't know how to complete it. Here's my sketch to explain it clearer:

alt text

I broke it down to three parts:

  1. Lerp: I want to Lerp the cube from Vector3(8,1,0) to Vector3(1,6,0) in 3 seconds flat.

  2. Height: Depending on distance of the start and finish points, I want to calculate the peak height of the cube, to make it a believable jump.

  3. Rotate: As the cube jumps, it will rotate towards the destination, like doing a front flip. I just need a curve formula to simulate the force and gravity of the jump without the use of physics. Also, the cube MUST start and finish with Quaternion.identity.

Getting the cube to stop at Quaternion.identity I think is the hardest part. In a way it must look seamless, like landing your feet in the same position started of a front flip.

Here is a mock of the function so far. I just need help with it:

 // JavaScript
 var jumpTime : float;
 
 function jumpTo(destination : Vector3, sec : float, rotate : boolean) {
     // 1). Lerp X and Y axis:
     jumpTime += Time.deltaTime / sec; // Not sure if this is correct way to calculate seconds.
     var lerped = Vector3.Lerp(transform.position, destination, jumpTime);
     transform.position.x = lerped.x;
     transform.position.y = lerped.y;
     
     // 2). Calculate curve of height depending on distance... (no idea?)
     
     if (rotate) {
         // 3). Rotate cube to the direction of destination:
         //        Challenges: Don't know how to control speed of rotation and rotate to Quaternion.identity as it comes to its destination.
         transform.Rotate(destination);
     }
     
     // Clear variables when reached destination:
     if (transform.position == destination) {
         jumpTime = 0;
     }
 }


Any thoughts, comments or ideas would be helpful. I really appreciate the help of the Unity community!

jumpto.jpg (67.3 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

1 Reply

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

Answer by robertbu · Jul 23, 2014 at 03:11 PM

Here is a bit of demonstration code that implements your functionality. From watching it work, I would suggest varying the time based on the distance to get something that is a bit more natural. Note I use Mathf.Sin() to calculate a height and Quaternion.AngleAxis() to do the flip. Attach this script to a cube in a new scene. Hit the spacebar to do the jump/flip.

 #pragma strict
 
 var heightVsDistanceFactor = 0.5;
 var minDistForFlip = 3.0;
 
 private var jumping = false;
 
 function Update () {
 
     if (!jumping && Input.GetKeyDown(KeyCode.Space)) {
         var v : Vector3 = Random.insideUnitCircle * 7.0;
         v.x = Mathf.Round(v.x);
         v.y = Mathf.Round(v.y);
         v.z = v.y;
         v.y = 0.0;
         DoTheJump(v, 3.0);
     }
 }
 
 function DoTheJump(dest : Vector3, time : float) {
     jumping = true;
 
     var timer = 0.0;
     var start = transform.position;
     var dist = (start-dest).magnitude;
     var maxHeight = dist * heightVsDistanceFactor;
     
     var axis : Vector3;
     var doSpin = ( dist >= minDistForFlip);
     if (doSpin) {
         axis = Vector3.Cross(start - dest, Vector3.up);
     }
     
     while (timer <= time) {
     
         var vT = Vector3.Lerp(start, dest, timer/time);
         vT.y = Mathf.Sin(Mathf.PI * timer/time) * maxHeight;
         transform.position = vT;
         
         if (doSpin) {
             transform.rotation = Quaternion.AngleAxis(360.0 * timer/time, axis);
         }
         timer += Time.deltaTime;
         yield;
     }
     
     transform.rotation = Quaternion.identity;
     transform.position = dest;
 
     jumping = false;
 }
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 jpierre88 · Jul 23, 2014 at 07:31 PM 0
Share

@robertbu I would bow down and kiss your feet if you were here. THAN$$anonymous$$S!!

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

2 People are following this question.

avatar image avatar image

Related Questions

AngryBots like character rotation system 1 Answer

rotate object over time and oscillate 1 Answer

Mathf.Lerp not working 2 Answers

How to use the lerp function with rotation? (C#) 1 Answer

Lerp 180 degrees while holding B and lerp back 180 degrees when let go of B. 2 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