- Home /
Head Look Controller with camera
I have integrated the Head Look Controller script in my character and it works like a charme. Now in stead of a GameObject I want it to look at the Main Camera which can be controlled by the user. It all works but at a certain point the camera goes all over the place. I think it has something to do with the code in the "Cursor Hit" c#.
using UnityEngine;
using System.Collections;
public class CursorHit : MonoBehaviour {
public HeadLookController headLook;
private float offset = 1.5f;
// Update is called once per frame
void LateUpdate () {
if (Input.GetKey(KeyCode.UpArrow))
offset += Time.deltaTime;
if (Input.GetKey(KeyCode.DownArrow))
offset -= Time.deltaTime;
Ray cursorRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(cursorRay, out hit)) {
transform.position = hit.point + offset * Vector3.up;
}
headLook.target = transform.position;
}
}
What I want is that the camera stays under control of the user. I've tried to alter the code but I don't know what to delete to just simplify it as the lookat target.
Thanks
I'm afraid I don't quite understand what you're trying to do here. Is the camera controlled at all by the headlook function? Does the headlook look at the camera, or at a point in the world?
It's quite simple. I want just the target to be the camera. So the characer is always trying to look at you as the viewer. So the whole setup for controlling the target needs to be chopped out.
Right, that makes sense. In which case it's really easy!
Assets/Head Camera.cs(14,26): error CS0029: Cannot implicitly convert type UnityEngine.Transform' to
UnityEngine.Vector3'
This is the error
Oh whoops. Forgot that the headlook thing tracks onto locations, not transforms. In any case, I've fixed it now.
Answer by syclamoth · Oct 30, 2011 at 12:28 PM
Get rid of that CursorHit thing entirely, and just set
headLook.target = Camera.main.transform.position;
wherever you have a reference to your HeadLook script!
The camera is a transform like any other object, and as such can be tracked by the head-look script.