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
1
Question by Joshua · Feb 23, 2011 at 04:08 PM · movementdirectionglobaldetermine

Determine global movement of gameObject

I have a ball bouncing around in a ponglike game. The ball only bounces in the y, x plane.The ball is supposed to bounce from left to right to left, etc, so to avoid it from getting stuck bouncing only up and down I applied a constant force.

This constant force either pushes it to the left or the the right.

Now I need a way to determine is the ball's current movement in the x-plane is positive or negative. When it's negative (moving left) the constant force needs to be negative (push ball more to the left) and if it's positive vise versa. I have no idea how to determine the movement of an object in the global x axis though. Many thanks in advance.

Comment
Add comment · Show 1
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 Joshua · Feb 23, 2011 at 04:58 PM 0
Share

It would also work if I could somehow get the direction of the current speed. then I could take the x component and evaluate if it's positive.

2 Replies

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

Answer by The_r0nin · Feb 23, 2011 at 05:16 PM

Record the X position as the very last thing in your movement loop. Then compare your X position in the next frame with the position in the last frame. If the difference between them is positive, you are moving in a positive direction. If negative, the same.

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 cregox · Feb 24, 2011 at 03:33 PM 0
Share

That's a more optimal solution, quite clever. :)

avatar image
2
Best Answer

Answer by cregox · Feb 23, 2011 at 05:30 PM

Depends on how you're making it bounce. If you're using rigidbodies / physics, just grab its velocity vector x. If you're implementing your own, you have to grab the delta of previous position and current so you know its current velocity. Then you grab the X component from that as well. Here's an example:

var oldPos : Vector3;
function Update () {
    var delta = transform.position - oldPos;
    print(delta.x); // it's moving one side if it's negative or positive to the other.
    oldPos = transform.position;
}
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 Joshua · Feb 24, 2011 at 02:39 AM 0
Share

Thanks a lot, great and thorough explanation!

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

No one has followed this question yet.

Related Questions

Grid-based movement: How to face forward 2 Answers

Why does my character only dash forward and not in the moving direction? 1 Answer

How to make a movement system that changes with your viewpoint but also lets you hit multiple keys at once to go diagonally? 0 Answers

how to make my character walk automatically and when pressing a button change the direction 2 Answers

Character facing wrong direction when moving vertically 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