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 Ritt · Nov 04, 2015 at 06:41 AM · 2dplayer movementvectors

Movement Position Being Equal to Int

I wasn't exactly sure how to title this, however I will explain what I mean. I am trying to make my game object move using:

 transform.translate(Vector2.up * moveSpeed * time.deltaTime);

Now what I want to do next is what is causing some trouble. I would like the object to continue moving until it hits the Y value of 1, and then turn left. I almost have that working. See it WILL work if I use >= 1, because it skips over the value of 1 (not moving by choppy ints). Even if I try changing it to == 1.0f it still won't work as I guess the object is moving too quickly and skips over some values. We've tried re-positioning it in the >= block of code but that bugs out and doesn't work very well.

If you have any thoughts or possible solutions that would be great! Thanks! :-D

Code ex:

 if (transform.position.x <= -2 && transform.position.y < -1) {
     transform.Translate(Vector2.up * playerSpeed * Time.deltaTime);
 }
 else if (transform.position.y <= -4)
 {
     transform.Translate(Vector2.left * playerSpeed * Time.deltaTime);
 }


Comment
Add comment · Show 3
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 gjf · Nov 03, 2015 at 11:08 PM 0
Share

look at $$anonymous$$athf.Approximately() ... and please, in future, format your code...

avatar image Ritt gjf · Nov 03, 2015 at 11:12 PM 0
Share

I'll look into it, and yes sorry just noticed now.. :-/ If there is a way to edit it I'll do so now.

avatar image allenallenallen Ritt · Nov 04, 2015 at 06:42 AM 0
Share

I went ahead and just fixed it for you. Remember to use the 010101 button to format your code.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by allenallenallen · Nov 04, 2015 at 06:43 AM

The solution you have is fine. It's almost impossible for the value to land perfectly as an integer.

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
avatar image
0

Answer by Garazbolg · Nov 04, 2015 at 09:54 AM

It is normal behaviour but what you can do to correct that is : when you know your transform is above -1 just clamp it to that value like this :

 if (transform.position.x <= -2 && transform.position.y < -1) {
          transform.Translate(Vector2.up * playerSpeed * Time.deltaTime);
          if(transform.position.y > -1)                      //Check if it has passed -1
                tranform.position.y = -1;                     //Clamp it
      }
      else if (transform.position.y <= -4)
      {
          transform.Translate(Vector2.left * playerSpeed * Time.deltaTime);
      }

This should fix your problem.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Vector-based Pong-ball bounce calculations 1 Answer

2D Sprite looks at the wrong side 1 Answer

2D Animation does not start 1 Answer

How Vectors work and how I should use them? 1 Answer

Prevent Player from sliding over edges 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