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 :)
No i don't think so. I want it after the coin reaches the target position.
if (Vector3.Distance(transform.position, target.transform.position >= 0) {
}
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'
$$anonymous$$y bad sorry
if (Vector3.Distance(transform.position, target.transform.position) <= 0) {
//Do something
}
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);
}
}
}
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;
}
Hmm... Does the BoxCollider bigger then the parent object?
The magnet is a parent object from the player and so yeah the magnet collider is bigger.
And which gameObject the script is attached to, Because it still works fine in my test scene (after a bit of modification)
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.
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. :)
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.
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.
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
Follow this Question
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