- Home /
getting out of LookAt messes up first person view
I'm having problems with getting out of transform.LookAt.
Imagine having a sphere that attracts a player and also locks the player's gaze at it until it is within a certain radius. When it is within that radius, the player can look normally again. The problem I have is that the view becomes slanted when going from right to left with the mouse as shown here.
This is the script I'm using.
using UnityEngine;
using System.Collections;
public class MoveToObject : MonoBehaviour
{
public float speed = 1.0f;
public GameObject player;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Vector3 objectPosition = transform.position;
Vector3 playerPosition = player.transform.position;
if (Vector3.Distance (objectPosition, playerPosition) > 5.0f) {
playerPosition += speed * (objectPosition - playerPosition).normalized * Time.deltaTime;
player.transform.position = playerPosition;
player.transform.LookAt (objectPosition);
}
}
}
One thing I tried was saving the original player.transform.rotation in a variable before getting attracted by the sphere and then restoring it later. This made the player face the same direction as he did originally and that's not what I want.
Answer by DESTRUKTORR · Sep 01, 2013 at 03:55 PM
The whole player is rotated. Are you using a character controller? You should attach the camera to a child of the controller, and have the camera's transform look at the object. If you switch to the scene view after the mouse gets screwed up, you'll note that the collider for the player is pointed in the same direction as the camera, I'd wager.
Thank you, that was the problem. Unfortunately I wasn't able to apply an elegant fix by letting the player camera look at the object. It seems to be somewhat disconnected from the player when inside the radius. I was using the standard First Person Controller by the way.
What I did do is do away with LookAt / LookRotation entirely and manually rotated the player. This was based on code found here
using UnityEngine;
using System.Collections;
public class $$anonymous$$oveToObject : $$anonymous$$onoBehaviour
{
public float speed = 1.0f;
public GameObject player;
private GameObject target;
private Vector3 targetPoint;
private Quaternion targetRotation;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Vector3 objectPosition = transform.position;
Vector3 playerPosition = player.transform.position;
if (Vector3.Distance (objectPosition, playerPosition) > 5.0f) {
// movement player to object
playerPosition += speed * (objectPosition - playerPosition).normalized * Time.deltaTime;
player.transform.position = playerPosition;
//replacement for LookAt / LookRotation, forces gaze of player onto object
targetPoint = new Vector3 (objectPosition.x, playerPosition.y, objectPosition.z) - playerPosition;
targetRotation = Quaternion.LookRotation (targetPoint, Vector3.up);
player.transform.rotation = Quaternion.Slerp (player.transform.rotation, targetRotation, Time.deltaTime * 10f);
}
}
}