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 ayygareth · Mar 05, 2020 at 12:05 AM · positionobject

Checking for object in position

Hi, i'm trying to make it so when an object moves to a specific position it triggers an event (Printing a message currently) I've been looking for a while and can't figure it out.

So far my code is

if (Physics.Raycast(transform.position,Vector3.Equals(0,0,0))) { Debug.Log("In Position"); }

This doesn't work though, i'm not really sure what i'm doing so would appreciate any help!

Thanks in advance!

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 Visulth · Mar 05, 2020 at 12:14 AM 0
Share

Could you expand on what you mean by "when an object moves to a specific position"?

Typically when you want functionality like that you'd create a trigger collider (e.g., an invisible sphere) and then have your code run within OnTriggerEnter. If you wanted to know when that object is touched (as opposed to being inside), you could use OnCollisionEnter. (Both of these calls would require a rigidbody on either the collider or the object).

A raycast on the other hand fires an invisible ray from a position along a direction and is typically better suited for answering questions like "what is my character looking at" or "is there an object of type x in front of my character".

avatar image ayygareth Visulth · Mar 05, 2020 at 12:22 AM 0
Share

I'll do my best to explain it so apologies if I don't explain well.

I have an object that spawns in and starts moving. I want it to stop at a specific point then trigger the next event. Would I need to use OnTriggerEnter for this to happen? Thanks for helping!

avatar image Visulth ayygareth · Mar 05, 2020 at 12:54 AM 0
Share

I would think so. If you wanted to test if an object was at a very specific spot (like pushing a cube onto a pressure plate in a Zelda game), then I would probably create a trigger there. But if you wanted something more dynamic, like "the object walks 10 meters and then turns around" then I would do it by testing position directly.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tecses1 · Mar 05, 2020 at 12:28 AM

There are multiple ways to handle this. Firstly,

if (Physics.Raycast(transform.position,Vector3.Equals(0,0,0))) { Debug.Log("In Position"); }

This is casting a ray from your transform.position in the direction of (0,0,0), so in other words, nowhere. A raycast is used to send a ray out from an origin and see what it hits, and I'm not sure this is what you want. You could try

transform.position.Equals(desiredPosition);

But keep in mind if you're using anything but whole numbers, checking floats is extremely difficult because of the precision. So I would recommend two things:

1) Tag your player something like "Player", and then create an object with a collider trigger, and use OnTriggerEnter to find if the player hit it, like:

 private void OnTriggerEnter(Collider other) {
         if (other.transform.tag == "Player") {
             Debug.Log("In position");
         }
     } 

2) Or, check the distance between the desired position and your position so you can have a range, Like:

     if (transform.position.Distance(desiredPosition) < 0.5f){
         Debug.Log("Within 0.5 meters of position!");   
    }
Comment
Add comment · Show 3 · 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 ayygareth · Mar 05, 2020 at 12:41 AM 0
Share

Thanks for the reply! I keep getting an error on the transform.positon.Equals which is saying 'no overload for method 'Distance/Equals' takes 3 arguments

    void Update()
  {
     transform.position += transform.forward * -1 * Time.deltaTime;

     if (transform.position.Equals(0,2,5))
     {
         Debug.Log("In Position");
     }
 }

is my full code for the script

avatar image Visulth ayygareth · Mar 05, 2020 at 01:05 AM 0
Share

Just a heads up, Vector3.Equals is not a great tool for equality unless you're specifically certain that is what you want to use. == is a better fit because it compensates for floating point inaccuracies (whereas transform.position.Equals doesn't).

That said, in your code, Equals() is expecting a single object and you're technically giving it 3 objects. You'd want to give it an actual vector3 object and use that, like transform.position.Equals(destination).

 protected vector3 destination = new Vector3(0f, 2f, 5f); 
 //if you wanted to specify the value in-code. 'f' is used after numbers to indicate that they are floats, ins$$anonymous$$d of converting from int to float. A vector3 is a float data-type.

 public vector3 destination; //if you wanted to specify the value in the inspector
 
 void Update() 
 {
      transform.position += transform.forward * -1f * Time.deltaTime;
      if (transform.position == destination) 
      {
           Debug.Log("In position");
      }
 }

avatar image tecses1 ayygareth · Mar 05, 2020 at 01:11 AM 0
Share

That's because Transform.position.Equals needs a vector3 object for comparison. You are passing 3 integers. (0,2,5) is very different from (new Vector3(0,2,5))! I would of course recommend using the distance solution ins$$anonymous$$d!

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

153 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

Related Questions

Why Does My Object Change Position During Update? 0 Answers

Remove object based on its position alone 1 Answer

Collide edge of object with the edge of the screen 0 Answers

Camera rotation around player while following. 6 Answers

How to segment a number line of game objects between two points dynamically? 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