I’m making an RPG with point-and-click movement like the one in GameDev.TV’s RPG course on Udemy. The player movement code is mostly the same as it is in the course, but for some reason, one part is not working in my project. When the player clicks on an interactable object, like a crate that can be looted, they should run up to it until they are 2 distance units away then stop:
As you can see, this portion of the code is identical to that written in the course. However, my character just runs straight through the object and then executes the interaction animation inside. Has anyone run into this?
It might help to see the entire InteractWithObject script.
Rather than pasting a screenshot, try copying and pasting the script. You can then select the entire script and press the </> button to turn it into code, or you can type ``` (the backwards quote next to the 1 on the keyboard three times) on it’s own line… example
```
public class Example
{
}
```
becomes
Both of those settings have been in place. Inexplicably, I logged on and the error is suddenly different. Now, instead of running inside the object, the player lays down on the ground and spins around it. I thought it was some new combat and health scripts that I attached after your response, but I removed both and the bug persisted. The only animation I have that causes the player to lay on the ground is the death animation, but that is only triggered through the health script, so since I removed that script I don’t think that is being triggered.
What’s the size on that capsule… it looks to have a radius bigger than 2 compared to the size of the character…
Do you have a Rigidbody on your character? If so, make sure that it is Kinematic, and make sure to lock the axes. That’s the only reason I could see that the character would flop onto it’s side.
Yes, that would mean the capsule’s radius is 1.8, but… where is the capsule’s origin point?
Since the Renderer is in the root of the target, in order for it to be standing properly on the ground, it’s location would have to be aproximately 1.8 off of the ground. Remember that Vector3.Distance works off of the full distance between the two vectors, based on all three dimensions. This means that your character will need to be virtually in the center of the capsule before it’s distance is less than 2.
Try putting the capsule itself under an empty GameObject, and then making the empty GameObject the target. Adjust the capsule so that the base of the capsule rests on the ground (set it’s local position to 0,1.8,0).
That worked! Thank you for all your help on this, that was a tough one. He still runs through the obstacle if I click the terrain on the side opposite to where he’s standing, but I’ll use a NavMeshObstacle component for that.