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 safak93 · Aug 23, 2015 at 05:01 PM · transform.positiontargetmagnetmagnetic

How to check if transform.position reaches target.position?

Hi everyone,

I have created a magnet script but there is only 1 problem left.

First of all here is the script:

 using UnityEngine;
 using System.Collections;
 
 public class Coin : MonoBehaviour {
 
     private GameObject target;
 
     void Update() {
         if(target) {
             transform.position = Vector3.MoveTowards(transform.position, target.transform.position, 3.0f * Time.deltaTime);
 
         }
         
         //I know it doesn't work. lol. It's only an example
         /*if (transform.position = target.transform.position) {
             target = null;
         }*/
     }
 
     void OnTriggerEnter2D(Collider2D col)
     {
         if (col.gameObject.tag == "Magnet") {
             target = col.gameObject;
         }
     }
 }

The problem is I want that if the "Coin" reaches the target position, target should be then = null. I have tried it with WaitForSeconds() but it's not good. I don't know how I should do that.

I hope someone can help me. Thanks :)

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 Positive7 · Aug 23, 2015 at 05:34 PM 0
Share

Destroy(target); ?

avatar image safak93 · Aug 23, 2015 at 06:01 PM 0
Share

No i don't think so. I want it after the coin reaches the target position.

avatar image Positive7 · Aug 23, 2015 at 06:19 PM 0
Share

if (Vector3.Distance(transform.position, target.transform.position >= 0) {

}

avatar image safak93 · Aug 23, 2015 at 08:06 PM 0
Share

I get this error:

 error CS0019: Operator `>=' cannot be applied to operands of type `UnityEngine.Vector3' and `int'
 
 error CS1502: The best overloaded method match for `UnityEngine.Vector3.Distance(UnityEngine.Vector3, UnityEngine.Vector3)' has some invalid arguments
 
 error CS1503: Argument `#2' cannot convert `object' expression to type `UnityEngine.Vector3'

avatar image Positive7 · Aug 23, 2015 at 09:43 PM 0
Share

$$anonymous$$y bad sorry

 if (Vector3.Distance(transform.position, target.transform.position) <= 0) {
             //Do something
         }

2 Replies

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

Answer by Positive7 · Aug 23, 2015 at 09:46 PM

Setup a new Layer in Layer manager Call it whatever you want it Mine looked like "User Layer 8 = Player" . Note the int value after User Layer " 8 " In the Ontrigger event set your int value replacing " x "

  Physics2D.IgnoreLayerCollision(0, x); 

Coin.cs:

     using UnityEngine;
     using System.Collections;
     
     public class Coin : MonoBehaviour {
         
         public GameObject target;
         
         void Update() {
             if (target != null) {
     
                 transform.position = Vector3.MoveTowards (transform.position, target.transform.position, 3.0f * Time.deltaTime);
             
                 if (Vector3.Distance (transform.position, target.transform.position) <= 0) {
                     Debug.Log ("Entered");
                     gameObject.SetActive (false);
                     target = null;
                 }
             }
         }
         
         void OnTriggerEnter2D(Collider2D col)
         {
             if (col.gameObject.tag == "Magnet") {
     
                 target = col.gameObject;
                 Physics2D.IgnoreLayerCollision(0,8);
             }
         }
     }
 
 
Comment
Add comment · Show 8 · 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 safak93 · Aug 24, 2015 at 08:23 AM 0
Share

Thanks. I will try it when I get home.

avatar image safak93 · Aug 26, 2015 at 05:54 PM 0
Share

It only works when I change <=0 to >=4. With "<=0" it didn't worked. Is that not weird?

 if (Vector3.Distance (transform.position, target.transform.position) >= 4) {
                 Debug.Log ("Entered");
                 target = null;
             }
avatar image Positive7 · Aug 26, 2015 at 08:07 PM 0
Share

Hmm... Does the BoxCollider bigger then the parent object?

avatar image safak93 · Aug 26, 2015 at 08:25 PM 0
Share

The magnet is a parent object from the player and so yeah the magnet collider is bigger.

avatar image Positive7 · Aug 26, 2015 at 09:06 PM 0
Share

And which gameObject the script is attached to, Because it still works fine in my test scene (after a bit of modification)

Show more comments
avatar image
0

Answer by pRoFlT · Aug 24, 2015 at 04:15 AM

I'm trying to figure out your expected outcome. Can we get a better understanding of the magnet script for the coin?

Do you want the coin to be attracted to the player like the million other runner phone games i.e. temple run?

If yes then the magnet script should have the coin Lerp or "MoveTowards" to the player until it is hit by the player then it should disappear??

Unless what you want (which now that i read it some more makes more sense) a magnet to attract coins and hold them? would you want to move the magnet and still have them hold on?

Basically you could use a distance measurement between two positions and stop once within a certain distance. dont use 0 or magnet.position = coin.position. thats almost impossible....well kind of. you could just make coin.position = magnet.position once it was close enough.

try this function, i use for moving platforms from one location to another, it should work for you.

 // transform.position is COIN, _currentPoint.position would be magnet.position
 var distanceSquard =  (transform.position - _currentPoint.position).sqrMagnitude;
 
 //MaxDistanceToGoal is how close you need to be to have reached your goal. you can call it something else. As this was for a moving platform.
 if(distanceSquard < MaxDistanceToGoal * MaxDistanceToGoal)
             //do what you want once its close enough

I give you this code because it appears that you are working in 2D mode and this is a basic distance calculation in 2D.

Comment
Add comment · Show 4 · 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 safak93 · Aug 24, 2015 at 08:51 AM 0
Share

So basicly it's a magnet-bonus for about 2-4 seconds. If the player collects the magnet-bonus the magnet gets activated. The magnet is a child from player-object. So when a coin is in a specific radius(e.g box collider2D) it moves to the magnet/player and then the coin will disabled(for the PoolScript) when it hits the player.

 using UnityEngine;
      using System.Collections;
      
      public class Coin : $$anonymous$$onoBehaviour {
      
          private GameObject target;
      
          void Update() {
              if(target) {
                  transform.position = Vector3.$$anonymous$$oveTowards(transform.position, target.transform.position, 3.0f * Time.deltaTime);
      
              }
          }
      
          void OnTriggerEnter2D(Collider2D col)
          {
              if (col.gameObject.tag == "$$anonymous$$agnet") {
                  target = col.gameObject;
              }
     
              if (col.gameObject.tag == "Player") {
                  //do something
                  gameObject.SetActive(false);
              }
          }
 }

The problem here is that I have a PoolScript and the coin will activated again. If the coin is reactivated it moves automatically to the magnet although the magnet is deactivated and therefore I need to call e.g target = null; when the coin reaches the target position.

I hope I could explain it to you. :)

avatar image pRoFlT · Aug 25, 2015 at 06:19 AM 0
Share

Seems like all you need to do is set the target=null when the coin is pooled? wouldn't this solve it. coin hits player coin is deactivated. when coin is pooled back in you simply set Target=NULL. Right?

I don't know what pooling system your using but when you grab an object form the pool you should have control of what/where it goes and access to the public vars like Target? or create a function called public void clearTarget() {Target=NULL";} and call it when you pool in the coin.

avatar image safak93 · Aug 26, 2015 at 05:51 PM 0
Share

Thanks but the problem here is then: "Object reference not set to an instance of an object" and nothing is pooled out because "Target" is not assigned at the begining of the pool.

avatar image pRoFlT · Aug 27, 2015 at 04:18 PM 0
Share

hmm next option is to check if magnet is activated. you have the target.

So something like

   if(target)
     {
     var magnet =  target.GetComponent<$$anonymous$$agnet>(); // or whatever the magnet script is.
     if(magnet.activated) // need to make a public variable to check on the magnet class
       {
       //move towards target
       }
     }

That way you dont care about target == null or not you only care if you have a target if the magnet is activated.

Although then if you do get a magnet then coins from everywhere will pull in. even ones far away.....

you could do

 if(target)
 {
  if(coin.activated)
   {
   //move towards magnet
  }
 }
 
 // And in your trigger event set activated to true. $$anonymous$$eaning $$anonymous$$agnet is activated and coin passed through magnet collider
 
 // Set false when hit player collider...

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

27 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

Related Questions

how is the Input.compass.rawVector affected by a magnet? 0 Answers

magnet power up in 3d endless runner game? 1 Answer

Help with pulling objects toward player 0 Answers

Magnetic Power Up 0 Answers

Problems with magnet in grid based puzzle game 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