# Its been a long time

**URL:** <https://talk.code3dgames.com/t/its-been-a-long-time/504>\
**Category:** Uncategorized\
**Created:** [June 13, 2022, 3:25am UTC](https://talk.code3dgames.com/t/its-been-a-long-time/504 "2022-06-13T03:25:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin](https://talk.code3dgames.com/user_avatar/talk.code3dgames.com/martin/32/41_2.png) [@Martin](https://talk.code3dgames.com/u/Martin)\
**Post date:** [June 13, 2022, 3:25am UTC](https://talk.code3dgames.com/t/its-been-a-long-time/504/1 "2022-06-13T03:25:15Z")

</div>

Hello Chris, how are you.  
Anyway,  
I was wondering if you can make if my avatar touches the key(The yellow sphere) the key dissapears.

Cheers,  
Martin:)

---

<div class="post-metadata">

**Author:** ![chris](https://talk.code3dgames.com/user_avatar/talk.code3dgames.com/chris/32/5_2.png) [@chris](https://talk.code3dgames.com/u/chris)\
**Post date:** [June 13, 2022, 3:27am UTC](https://talk.code3dgames.com/t/its-been-a-long-time/504/2 "2022-06-13T03:27:36Z")

</div>

Hi Martin!

Welcome back! I’m well, hope all is well with you too. I am not sure that I quite follow your question. Can you post your code? If you can do that, I’ll have a look as see if I can figure out what is going on.

Thanks!

-Chris

---

<div class="post-metadata">

**Author:** ![Martin](https://talk.code3dgames.com/user_avatar/talk.code3dgames.com/martin/32/41_2.png) [@Martin](https://talk.code3dgames.com/u/Martin)\
**Post date:** [June 17, 2022, 12:29am UTC](https://talk.code3dgames.com/t/its-been-a-long-time/504/4 "2022-06-17T00:29:31Z")

</div>

No, i mean that I need the collision script if the avatar is colliding with a key(the yellow sphere ) the key just dissapears and it looks like it was picked up.

---

<div class="post-metadata">

**Author:** ![chris](https://talk.code3dgames.com/user_avatar/talk.code3dgames.com/chris/32/5_2.png) [@chris](https://talk.code3dgames.com/u/chris)\
**Post date:** [June 17, 2022, 1:11am UTC](https://talk.code3dgames.com/t/its-been-a-long-time/504/5 "2022-06-17T01:11:35Z")

</div>

There are a couple of approaches here. Either way, you’ll want to add an `isCollidingWithKey()` inside the `sendKeyDown()` function. It will look something like:

```auto
function sendKeyDown(event) {
   // The rest of your code here...
  if (isCollidingWithKey()) pickUpKey();
}

```

The `pickUpKey()` function can be something simple like:

```auto
function pickUpKey() {
  scene.remove(key);
}

```

At some point, you can improve this by adding animation or scoring, but the above should get you started.

As for the `isCollidingWithKey()` function, you can start with something like:

```auto
function isCollidingWithKey() {
  var xDiff = marker.position.x - key.position.x;
  var zDiff = marker.position.z - key.position.z;
  var distance = Math.sqrt(xDiff **2 + zDiff** 2);
  return distance < 50;
}

```

This function uses the [Pythagorean theorem](https://en.wikipedia.org/wiki/Pythagorean_theorem) to determine the distance between the key and the avatar’s marker. If the distance is less than 50 (which is the size of the key), then this function will return `true`, causing `pickupKey()` to be called. If the distance is 50 or greater, then this function will return `false`.

The other approach that you can take with `isCollidingWithKey()` is similar to the raycaster approach that we use in this chapter for determining how close we are to the trees.

Hope that helps!

-Chris

---

<div class="post-metadata">

**Author:** ![Martin](https://talk.code3dgames.com/user_avatar/talk.code3dgames.com/martin/32/41_2.png) [@Martin](https://talk.code3dgames.com/u/Martin)\
**Post date:** [June 19, 2022, 1:15am UTC](https://talk.code3dgames.com/t/its-been-a-long-time/504/6 "2022-06-19T01:15:11Z")

</div>

Oh yes! It works but i get an Warning under Here:  
function isCollidingWithKey() {  
var xDiff = marker.position.x - key.position.x;  
var zDiff = marker.position.z - key.position.z;  
var distance = Math.sqrt(xDiff **2 + zDiff** 2);  
return distance \< 50;  
}

That Line of Code gets me a Warning idk what’s happening. var distance = Math.sqrt(xDiff **2 + zDiff** 2);

---

<div class="post-metadata">

**Author:** ![chris](https://talk.code3dgames.com/user_avatar/talk.code3dgames.com/chris/32/5_2.png) [@chris](https://talk.code3dgames.com/u/chris)\
**Post date:** [June 19, 2022, 1:19am UTC](https://talk.code3dgames.com/t/its-been-a-long-time/504/7 "2022-06-19T01:19:06Z")

</div>

That line is supposed to be x-squared plus z-squared:

```javascript
  var distance = Math.sqrt(xDiff **2 + zDiff** 2);

```

You could also write this as:

```javascript
  var distance = Math.sqrt(xDiff*xDiff + zDiff*zDiff);

```

-Chris
