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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Pauls · Jul 17, 2013 at 03:09 PM · transform.positionworldspacelocalscale

from world to local coordinates bouncing ball?

Hi,

I would like my character to bounce constantly, even if its parent moves up or down. If I try the bouncing effect on a constant y axis in local space, it works fine. But if the character goes down, the ball stays up in the air, because its "anchor point" has not changed. So I would like to use the character's y position for my effect. How could I then update the local coordinates with the parent's y position (which is in world space)? TransformPoint is to go from local to world, how to do the inverse? Or any other solution?

Here is the code that works in local space :

 coef += 0.1f;
 //Vector3 trPos = transform.position;
 //trPos.y = posY + (Mathf.Sin(coef) * multSin);//local space : goes up and down
 
 Vector3 trPos = transform.TransformPoint(transform.parent.position);//cannot use this to update local coordinates
 trPos.y = trPos.y + (Mathf.Sin(coef) * multSin);
 //transform.position = … new Vector3(trPos.x, trPos.y, trPos.z );

Thanks for your help

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 17, 2013 at 03:43 PM

If I understand what you want to do correctly, what you want to do is to Transform.localPosition:

 public class Bounce : MonoBehaviour {
 
     public float speed = 1.0f;
     public float magnitude = 2.0f;
  
     void Update() {
         Vector3 v3 = transform.localPosition;
         v3.y = Mathf.Sin(Time.time * speed) * magnitude;
         transform.localPosition = v3;
     }
 }

If you want more of a bounce like a ball hitting a surface, change the calculation to:

 v3.y = Mathf.Abs(Mathf.Sin(Time.time * speed) * magnitude);


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 Pauls · Jul 18, 2013 at 06:03 AM 0
Share

@robertbu thanks a lot robertbu, I misunderstood that transform.position was already in world space. Thanks for your help, it works fine!

avatar image robertbu · Jul 22, 2013 at 01:56 PM 1
Share

If you use Time.time or Time.deltaTime, then this code needs to be in Update() or if there was some need, LateUpdate(). FixedUpdate() is generally for Physics operations (which this is not). If for some reason you needed it in FixedUpdate(), you would use Time.fixedDeltaTime.

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

15 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

Related Questions

How do i move an object relative to another objects axis. 2 Answers

Keeping raycast position and rotation equal as gameObject 1 Answer

Trying to position a world space menu in front of the camera whilst ignoring the camera's tilt angle 1 Answer

World to local Inversion not working as I expect 0 Answers

How to get max and min points of a game object on the x and z axis? 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