- Home /
How do I keep functionality of LookAt script whilst using it in a 3dGUI camera layer?
I have a simple lookat script that takes in the vector3 of the touch on an ios device, and makes an object look at it. I am trying to use this in a layer which then put onto my main camera. The only problem is, it ceases to work. It appears to be shooting a ray through the right camera, but offsetting the position based on where my main camera is positioned. Is there any way around this?
We need to see your code. It sounds like you are using the wrong camera when you do the screen to world conversion.
using UnityEngine;
using System.Collections;
public class test : $$anonymous$$onoBehaviour
{
public Ray ray;
RaycastHit hit;
Vector3 hitpoint;
public Quaternion currentRotation;
Quaternion targetRotation;
public float rotationSpeed = 4;
public Camera cam;
void Update ()
{
ray = cam.ScreenPointToRay(Input.GetTouch(0).position);
currentRotation = this.transform.rotation;
if(Physics.Raycast(ray, out hit))
{
hitpoint = hit.point;
hitpoint.y = 0;
}
targetRotation = Quaternion.LookRotation(hitpoint);
this.transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationSpeed * Time.deltaTime);
}
}
Where cam is the GUI camera.
I've included an image for a better demonstration of what's happening. GUI is the camera with the gui, main is the camera that the gui is rendering onto. F1 is where I'm touching through the gui cam, but it seems to think that my touch is at F2 which causes the object to look at my main cam, ins$$anonymous$$d of rotate based on where my touch is through the gui cam.