-3
\$\begingroup\$

Edit: I want to find the distance between the player another object. How do I do that?

this is what I tried:

public GameObject other; void Update() { float dist = Vector3.Distance(other.transform.position, transform.position); print("distance " + dist) } 

which didn't seem to work

\$\endgroup\$
8
  • \$\begingroup\$ what is other? \$\endgroup\$ Commented Jul 9, 2016 at 8:53
  • \$\begingroup\$ Do not edit your question in a manner that changes it into something else. If you have another question, ask it as another one. \$\endgroup\$ Commented Jul 9, 2016 at 9:03
  • \$\begingroup\$ @S. Tarık Çetin I am sorry but last time I did that they '-1' it for duplication even though it was different. \$\endgroup\$ Commented Jul 9, 2016 at 9:10
  • \$\begingroup\$ @SP. it's a GameObject (just edited) \$\endgroup\$ Commented Jul 9, 2016 at 9:13
  • 1
    \$\begingroup\$ @AJ123 i see it's working. value changes when the other object moves. have you assigned the other object correctly in the script holding object's in inspector. you may have assigned another not moving object. \$\endgroup\$ Commented Jul 9, 2016 at 9:50

2 Answers 2

2
\$\begingroup\$

To achieve that task, you can use BoxCollider2D or PolygonCollider2D. Attach it to both objects, but make it 5 pixels larger for the first object.

Then write code, that kills the object, in the OnTriggerEnter2D / OnCollisionEnter2D event handlers.

To destroy the object you can use method Object.Destroy, just pass the desired gameObject reference to it.

Here's the reference materials: BoxCollider2D

Also, you can cast rays of limited length, how to do so described in the following link:

How to cast ray2d in Unity

Also, I strongly advice you to learn through this section and watch all the materials, because what you've asked is really basic question, you should at least watch the tutorials in the learn section on Unity site, so you won't ask the questions like this in the future.

Here it is, please watch it

\$\endgroup\$
2
  • \$\begingroup\$ But I want the first object to be able to overlap the second object, and a second later destroy it. Sorry I wasn't clear before \$\endgroup\$ Commented Jul 9, 2016 at 0:42
  • \$\begingroup\$ I don't want collision, I want position. Sorry if it was unclear \$\endgroup\$ Commented Jul 9, 2016 at 1:19
0
\$\begingroup\$

You can measure squared distances:

var sqrMagn = (target.position - transform.position).sqrMagnitude; if(sqrMagn <= squaredTriggerDistance) { Destroy(gameObject); } 

Change squaredTriggerDistance to a value you want.

\$\endgroup\$
7
  • \$\begingroup\$ should I add rigidbody2d instead of target? \$\endgroup\$ Commented Jul 9, 2016 at 0:46
  • \$\begingroup\$ @AJ123 No, that's a transform referance. But if you are asking this question, I suggest you to abort what you are doing now and return to tutorials. When I started Unity I was spending %80 of my time reading manual and watching tutorials. \$\endgroup\$ Commented Jul 9, 2016 at 5:41
  • \$\begingroup\$ Target is not a thing in the c# script. I think you gave me something from a js script \$\endgroup\$ Commented Jul 9, 2016 at 6:31
  • 2
    \$\begingroup\$ It's your job to adopt the techniques given to you to your own code. It's not nice to ask people to do your work for you. \$\endgroup\$ Commented Jul 9, 2016 at 8:44
  • 1
    \$\begingroup\$ @AJ123 Well... Target is supposed to be a variable holding the target gameobject's transform. I thought you could figure it out yourself. \$\endgroup\$ Commented Jul 9, 2016 at 8:59

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.