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
2
Question by Hjalte · May 11, 2014 at 04:07 PM · c#movementvector3compare

Greater than or equal to Vector3

Okay so basically i want to check if one Vector3 is greater or equal to another vector3 Heres my script where I would like to check if transform.position is greater or equal to DesinationSpot. How would i go about doing this?? using UnityEngine; using System.Collections;

 public class RigidbodyMovePlatform : MonoBehaviour {
     
     public Vector3 DestinationSpot;
     public Vector3 OriginSpot;
     public Vector3 speed = new Vector3(3, 0, 0);
     public Vector3 speedBack = new Vector3(-3, 0, 0);
     public bool Switch = false;
 
     void FixedUpdate () {
 //Here I wanna check if transform.position is greater than or equal to DestinationSpot
         if(transform.position == DestinationSpot){
             Switch = true;
         }
 //Here I wanna check if transform.position is less than or equal to OriginSpot
         if(transform.position == OriginSpot){
             Switch = false;
         }
         if (Switch == true) {
             rigidbody.MovePosition(rigidbody.position + speedBack * Time.deltaTime);        
         }
         else{
             rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime);
         }
     }
 }

I know others have asked simular questions but i havent been able to get a good answer atleast not one in C#.

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 robertbu · May 11, 2014 at 04:10 PM 2
Share

What does greater or equal mean in a 3D environment? For example, an object with a smaller 'z' value is closer to the camera and therefore on top. $$anonymous$$aybe you want:

 bool GreaterEqual(Vector3 a, Vector3 b) {
     return (a.x >= b.x) && (a.y >= b.y) && (a.z >= b.z);
 }
avatar image Jeff-Kesselman · May 11, 2014 at 04:12 PM 0
Share

Are you talking about the magnitude? (length of the vector)? If so then you can use

 if (vec1.mangitude <= vec2.magnitude)
avatar image Hjalte · May 12, 2014 at 06:44 PM 0
Share

Thank you very much for the quick answers, it helped.

1 Reply

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

Answer by snarf · May 12, 2014 at 10:57 AM

Directly comparing two Vector3 is not the way to go.

instead you should compare the distance to some value - like this;

 public class RigidbodyMovePlatform : MonoBehaviour {
  
     public Vector3 DestinationSpot;
     public Vector3 OriginSpot;
     public Vector3 speed = new Vector3(3, 0, 0);
     public Vector3 speedBack = new Vector3(-3, 0, 0);
     public bool Switch = false;
  
     void FixedUpdate () {
 //Here I wanna check if transform.position is greater than or equal to DestinationSpot
        if(Vector3.Distance(transform.position, DestinationSpot) < 1.0f /* within 1 meter radius */){
          Switch = true;
        }
 //Here I wanna check if transform.position is less than or equal to OriginSpot
        if(Vector3.Distance(transform.position, OriginSpot) < 1.0f /* within 1 meter radius */){
          Switch = false;
        }
        if (Switch == true) {
          rigidbody.MovePosition(rigidbody.position + speedBack * Time.deltaTime);     
        }
        else{
          rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime);
        }
     }
 }
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 Hjalte · May 12, 2014 at 06:41 PM 0
Share

Thank you very much that helped alot.

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

22 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Multiple Cars not working 1 Answer

Move from one position to another in x seconds 2 Answers

Need helping getting a character controller to jump 1 Answer

How can I make this object relative to the players position? 2 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