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 /
  • Help Room /
avatar image
0
Question by SSJ-Panda · Jul 21, 2016 at 01:30 PM · c#movement scripttransform.positionmove an objectif statement

SImple, but how do I check a game object's position in an If statement?

In my game I have a number of cubes that are invisible placed around the map, I can access their public transforms and I use them to move another placeholder cube to their locations. I want to check, using an if statement, when I reach "Waypoint 01", for example, and if the game object is at "Waypoint 01"'s transform move to "Waypoint 02". Below I shall post the code I have made so far to make the visible placeholder cube move to a single waypoint and my attempt to type out an if statement to make the placeholder cube move to the next waypoint.

Any assistance is much appreciated.

 {
     public Transform enemyStart;
     public Transform waypoint01;
     public Transform waypoint01plus;
     public Transform waypoint02;
     public Transform waypoint03;
     public Transform waypoint04;
     public Transform waypoint05;
     public float speed;
 // Use this for initialization
 void Start () 
 {
 
 }
 
 // Update is called once per frame
 void Update () 
 {
     float step = speed * Time.deltaTime;

     transform.position = Vector3.MoveTowards(transform.position, waypoint01.position, step);

     if (transform.position = waypoint01)
     {
         transform.position = Vector3.MoveTowards(transform.position, waypoint01plus.position, step);
     }
 }
 }


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
1

Answer by UnrealSoftware · Jul 21, 2016 at 02:00 PM

There are some problems with your code:

  • Equality is checked with == in most programming languages, including C#. A single = is used for assignments only.

  • You compare a Vector3 (transform.position) with a Transform (waypoint01) but you actually want to compare the positions of both

  • Moreover that check would only kick in when the position is EXACTLY equal to waypoint01's position. This is probably a bad idea due to floating point precision. You should check the distance instead and continue when a certain threshold is reached: if (Vector3.Distance(transform.position, waypoint01.position) < 0.1) { ... }

  • The stuff in the if condition would only be executed once (or very few times when using my distance change) because the transform would quickly move outside the position so the condition is not fulfilled anymore.

  • The stuff above the if condition would still be executed always which is also not what you want to happen. You don't want to move towards waypoint01's position if you already have been there.

You should re-think your general approach because your solution doesn't scale. You would have to write a lot of code if you have many waypoints which is a bad thing. Are you familiar with arrays/lists? It would be more clever to have an array or list of waypoints. You would then save the index to the next waypoint you want to reach and always move towards its position. As soon as it is reached you increase the index which makes you move towards the next waypoint. And of course you have to stop (or do something else) when the last waypoint has been reached.

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 TheRedGuy90 · May 04, 2017 at 03:41 AM 0
Share

If someone were to use an "if" statement to detect if one object's position is the same as anothers, is that any more or less complicated

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

201 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

Related Questions

Moving moving Object with left/right arrow keys in a circular direction 3 Answers

lerp works on (-6.5/y/z) but not on (+6.5/y/z) 0 Answers

Code is acting different depending on call 2 Answers

Add changeable conditions in the editor, similar to unityEvent? 0 Answers

2D AI movement 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