- Home /
Soo wierd!!! Clamp doesnt work when moving and goes past clamp point!
Its the part with cameray and cameraz that clamp the positioning. its not clamping when the player its moving.
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public int Sensitivity;
public float MaxDistance;
private float MouseY;
private float MouseYSet;
private float CameraAngleX;
public float CameraY;
public float CameraZ;
public GameObject CameraTarget;
void Start () {
}
void Update () {
// Look controls
MouseY -= Input.GetAxisRaw ("Mouse Y");
CameraY = Mathf.Clamp (CameraY + (MouseY - MouseYSet) * Sensitivity * 10, CameraTarget.transform.localPosition.y + transform.localScale.y / 2, CameraTarget.transform.localPosition.y + transform.localScale.y / 2 + MaxDistance);
CameraZ = Mathf.Clamp (CameraZ + (MouseY - MouseYSet) * Sensitivity * 10, CameraTarget.transform.localPosition.z - MaxDistance, CameraTarget.transform.localPosition.z);
if (MouseYSet != MouseY)
{
transform.position = new Vector3 (transform.localPosition.x, CameraY, CameraZ);
MouseYSet = MouseY;
}
transform.LookAt (CameraTarget.transform.localPosition);
}
}
when mouseyset != mousey, the delta is used to move the position and then sets mouseyset to the new mousey
Oh no it works fine when staying still, just when he moves the constant transform.position change keeps it in place for some reason.
Answer by tanoshimi · Jan 03, 2015 at 11:08 PM
Use Debug.Log to print out some specific examples of CameraY, MouseY, MouseYSet, Sensitivity, CameraTarget.transform.localPosition.y, transform.localScale.y, and MaxDistance that you feel are not working.
Your answer
Follow this Question
Related Questions
Trying to make a third person camera, whats wrong with this transform.position? 1 Answer
Sometimes strange behaviour of my 'walk around' script 0 Answers
Problem with the "Play again" button. 1 Answer
Vertical camera movement script being EXTREMELY wierd 0 Answers
Count Variable Acting Strange 1 Answer