Hi, I need help with this script:
public GameObject itemObject;
public float distance = 3.0f;
public Transform cameraTransform = Camera.main.transform;
public void OnPointerDown(PointerEventData eventData)
{
Instantiate(itemObject, cameraTransform.position + cameraTransform.forward * distance, Quaternion.identity);
Debug.Log("Was Pointed");
}
This script place a object in front the camera with 3m of distance, but I want that the object always be in the coordinate 0 of the Y axis, even if I look up or down.
Thank you.
Hi!
Try this:
public GameObject itemObject;
public float distance = 3.0f;
public Transform cameraTransform = Camera.main.transform;
public void OnPointerDown(PointerEventData eventData)
{
var newObject = Instantiate(itemObject, cameraTransform.position + cameraTransform.forward * distance, Quaternion.identity);
newObject.transform.position = new Vector3(newObject.transform.position.x, 0, newObject.transform.position.z);
Debug.Log("Was Pointed");
}
Good day.
Best way is to make the object child of the camera object. So the coordinate of position is very easy to find, and the camera can be lloking anywhere.
So do somethig like this:
GameObject NewItem= Instantiate(itemObject, cameraTransform);
NewItem.transfomr.LocalPosition = *The Position relative to the cam*
Bye!