I’m going mad with an script of mine, because I don’t understand it’s behaviour. I have two problems, one with the camera behaviour and other with the movement behaviour.
The script must be a simple 3rd player controller, the camera (moved with the mouse) rotates the player object in the Y axis, and moving the mouse in the Vertical moves the camera up and down, with some angle constraints. After rotate the player object, using the keyboard we can move the player forward/backwars or strafe left/right in the facing direction.
The first problem I have is when varying the camera distance, the angle constraints that I’ve set before doesn’t works anymore… I need to set new ones, and don’t understand why!
Let’s explain the situation with a picture (after trying 3 times I’m not able to attach this image):
I assume that a camera looking at the head of the player (the GameObject target) will maintain the same angle whatever the distance between camera and target is (in the picture, the angle in X axis must be the same in the “y” distance and the “z” distance).
So, after putting a Minimum vertical limit of -65 and a Maximum vertical limit of 65 for a Camera distance of 5, I get a coherent movement of the camera when rotates on the X axis over the player’s head. But after changing the Camera distance the values of -65 and 65 doesn’t works well anymore. I was convinced that the limits would be independent of the camera distance. Definitely I’m making lots of mistakes on the camera movement, but I don’t know where… someone can explain me what I’m doing wrong? Here is the code:
/* mouseSensitivity, minimumY, maximumY and maxDistance
are public float values that you can set from the editor. */
x += Input.GetAxis("Mouse X") * mouseSensitivity;
y += Input.GetAxis("Mouse Y") * -mouseSensitivity;
y = Mathf.Clamp(y, minimumY, maximumY);
Vector3 offset = new Vector3(0f, 2f, -maxDistance);
transform.position = Quaternion.Euler(y, x, 0f) * offset + target.transform.position;
transform.LookAt(target.transform);
parentTransform.rotation = Quaternion.Euler(0f, transform.rotation.eulerAngles.y, 0f);
As I said before, my second problem is with the player movement, I’m using the player’s transform.forward and transform.right vectors in order to build a direction vector multiplying with the Vertical and Horizontal Input values, after this I’m adding the direction vector to the position. I’m doing this because I think that transform.forward and transform.right vectors are affected by rotation:

But my code only works when the player object is facing to Z. Again, I’m doing something wrong but I don’t know what. Will anyone help me to understand where is my mistake? Here is the code:
/* parentTransform is the Transform of the player object.
movementSpeed is a public float value that you can set from the editor.
moveDirection is a private auxiliar Vector3. */
moveDirection =
(parentTransform.transform.right * Input.GetAxis("Horizontal")) +
(parentTransform.transform.forward * Input.GetAxis("Vertical"));
moveDirection *= (movementSpeed * Time.deltaTime);
parentTransform.transform.position += moveDirection;
If it will be helpful, here is the full code:
using UnityEngine;
using System.Collections;
public class cameraCollision : MonoBehaviour
{
public GameObject target; // target is player's head
public float maxDistance;
public float mouseSensitivity;
public float minimumY;
public float maximumY;
float x = 0f;
float y = 0f;
Transform parentTransform; // transform of the player's body
Vector3 moveDirection;
public float movementSpeed;
void Start ()
{
parentTransform = target.transform.parent;
}
void Update ()
{
MouseInput();
KeyboardInput();
}
void LateUpdate()
{
CameraCollision();
}
void CameraCollision()
{
RaycastHit hit;
if (Physics.Linecast(target.transform.position, transform.position, out hit, 0x200))
{
transform.position = hit.point;
}
}
void KeyboardInput()
{
moveDirection =
(parentTransform.transform.right * Input.GetAxis("Horizontal")) +
(parentTransform.transform.forward * Input.GetAxis("Vertical"));
moveDirection *= (movementSpeed * Time.deltaTime);
parentTransform.transform.position += moveDirection;
}
void MouseInput()
{
x += Input.GetAxis("Mouse X") * mouseSensitivity;
y += Input.GetAxis("Mouse Y") * -mouseSensitivity;
y = Mathf.Clamp(y, minimumY, maximumY);
Vector3 offset = new Vector3(0f, 2f, -maxDistance);
transform.position = Quaternion.Euler(y, x, 0f) * offset + target.transform.position;
transform.LookAt(target.transform);
parentTransform.rotation = Quaternion.Euler(0f, transform.rotation.eulerAngles.y, 0f);
}
}

1234680–52282–$Camera.bmp (217 KB)