GameObject keeps moving towards the camera when it follows the transform.position of another object

This is a weird one, I’m having it so that a hand can pick up gameobjects within the world and I figured one way I could do this is have the gameobject being picked up simple follow the transform.position of the hand. It works really nicely but for whatever reason the gameobject along with the hand is actually moving towards the camera.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class PickUpVillager : MonoBehaviour

{
    public bool VillagerPickedUp = false;

    private void OnMouseDown()

    {

            GetComponent<NavMeshAgent>().enabled = false;
            GetComponent<VillagerScript>().enabled = false;
            VillagerPickedUp = true;
    }

    private void Update()

    {
        if (VillagerPickedUp == true)

        {
            GameObject godHandEmpty = GameObject.Find("GodHandEmpty");
            gameObject.transform.position = godHandEmpty.transform.position;
        }
    }


}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GodHandMousePosition : MonoBehaviour
{

    void Update()

    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit))

        {
            gameObject.transform.position = new Vector3 (hit.point.x, hit.point.y, hit.point.z);
        }

    }

}

Now I included the raycast code that the hand had because I’ve run into problems like this before and it sometimes turned out to be the collider, I don’t know what’s going on with it right now but I included the raycast code just in case because that could be causing problems as well.

Oh it’s worth pointing out that it’s not just the capsule that I’ve picked up which moves to the camera the hand does as well.

I have in no position to test my hypothesis but I have an idea, what maybe is happening. My guess is that your RayCast is hitting either the surface of the object or the hand itself and this point is always a little bit closer to the camera since the Ray starts there. And then you just set the position to the hit point you found. You probably want to check if the hit point is an intersection with the ground(?) I guess. Or something like that.

But again, this is only a guess, sorry if I’m wrong.

Ahhh thanks, I’ll give this a shot and see if it works, you’ve given me some ideas now, I thought that nothing would happen with the hand if there was no collider so it looks like I’m going to have to do some experimenting.

1 Like

There could be a collider on the hand or more likely on the object you’re picking up. You can Debug.Log (hit.collider.gameObject.name) to know which object the raycast is hitting.

On a note unrelated to your current problem, you absolutely should not use GameObject.Find every frame, and in fact you honestly shouldn’t use it at all. There is, invariably, always a better way to find a reference to an object than GameObject.Find.

1 Like

In theory it shouldn’t but I cannot know that since the screenshot you uploaded does not show anything useful in the inspector :wink:
So from my point of view you could have collider on either the hand or on the object.

OH! I just realised what it could be, if a collider exists and it’s a trigger will the raycast still pick it up?

Edit: Yep, just worked it out, I had colliders that were children of the object being picked up and I needed to disable them.