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.