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 sir_shanks_a_lot · Jan 18, 2018 at 05:49 PM · c#velocityplayer movement

Checking when the player is stopped, without velocity?

My game is an endless runner, so velocity is a constant. But I need to find out when the player has stopped due to colliding with a platform. I cannot check for when velocity is very small, because velocity is constant, even when the player is halted by a platform. But I also cannot check for collision with a platform, because then what I want to happen will be triggered every time the player collides with a platform.

It has to be something with the player's position, right? Can someone point me in the right direction please? I've been at this for hours.

Comment
Add comment · Show 5
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 KittenSnipes · Jan 20, 2018 at 03:23 AM 0
Share

@sir_shanks_a_lot

Does your object automatically reach that constant velocity at start? If so then you can check to see if the velocity has changed due to the collision because if an object collides with an object it does lose velocity.

avatar image sir_shanks_a_lot KittenSnipes · Jan 20, 2018 at 04:40 AM 0
Share

I put the velocity in update (the speed is changing as time progresses), so the velocity is constantly set to speed.

avatar image KittenSnipes · Jan 20, 2018 at 04:46 AM 0
Share

@sir_shanks_a_lot

Well then all you need is to check the velocity and have a variable that holds what your current velocity is. After that check to see if the velocity goes below it. That would mean that you collided with something because when you collide with something it accelerates negatively

avatar image sparkzbarca KittenSnipes · Jan 20, 2018 at 05:33 AM 0
Share

i think he's constantly adding velocity which I think is where he's saying he's running into the issue, basically he's got the car against the wall and is flooring the gas and it's making it hard to use a force measurement, because he is pushing against the wall. he's faililng to move, but he's trying.

avatar image ADiSiN · Jan 20, 2018 at 05:50 AM 0
Share

You can try to create constantly repeating function, so you won't execute this piece of code at each frame, but only for example each 0.1f. And each time when you invoke function it'll take current position of your player and compare it with previous position of your player when this function was called. If it's the same or lower than some number, then, probably, player stopped. Here you even can check only for the same position on X or Y or Z axes.

2 Replies

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

Answer by ransomink · Jan 20, 2018 at 09:29 AM

No problem, you'll want to check the player's previous position against their current position. If the distance is the same or minimal (using a tolerance), they're not moving. When the player moves, set their position to lastPos. Then, check the difference of the x value between the lastPos and curPos variables...


Here is a sample of how it could be done:

     public bool     moving;
     public float tolerance;
     public Vector2 prevPos;
     public Vector2 curPos;
 
     private Transform t;
 
     void Awake()
     {
         t = transform;
     }
 
     void Update()
     {
         // Move the game object however you set it up
         // This line is where you set the velocity to move your game object.
 
         // Moving? (along the horizontal axis)
         if ( t.position.x - tolerance > prevPos.x )
         {
             // Set last position
             prevPos = t.position;
         }
         else
         {
             // We're not moving
             moving = false;
         }
     }


Alternatively, you can get the distance between both positions but since your game is an endless runner, you only need to check against one axis...

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 sir_shanks_a_lot · Jan 20, 2018 at 03:03 PM 0
Share

Works great. Thanks!!

avatar image
0

Answer by sparkzbarca · Jan 20, 2018 at 05:44 AM

Endless runners normally use you leaving the screenspace to determine death if that's what your trying to do.

To do that a simple bounds check will work.

if your just wanting to know if someone is stuck on a wall as it were for whatever reason there are a few ways.

firstly oncollsionstay()

not on collision enter or on collision exit but on collision stay.

you can simply run a timer -= time.fixedtime or whatever and every physics update they stay in contact the timer will count down, if timer

as for using position, you could use position to determine if your starting to go backwards basically and you could even do something where you store the current frames position in global space as lastpos, then next frame you compare lastXpos - screen movement per frame and now if someone isn't moving, there position will be just that. if your "standing still" on a moving screen, your position will be in one frame, the position you were in the last frame but negative at least on the X axis the value at which the screen is moving right.

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

434 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 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 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Inconsistent Top Speeds On Player 1 Answer

Why my character moves left when i press right key ? Controls are upside down? 2 Answers

How to make the player direction change with the camera rotation? 1 Answer


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