- Home /
How to cap this movement?
I know what is causing this, but in the vector3 near the bottom, just doing CameraDistance.y and CameraDistance.z doesnt work.
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public float Sensitivity;
public float MaxPos;
public float MaxAngle;
private float MouseY;
private float MouseYSet;
private float CameraAngle;
private Vector3 CameraDistance;
public GameObject CameraTarget;
void Start () {
}
void Update () {
// Look controls
MouseY -= Input.GetAxisRaw ("Mouse Y");
CameraAngle = Mathf.Clamp (CameraAngle + (MouseY - MouseYSet) * Sensitivity * 10, -MaxAngle, MaxAngle);
CameraDistance = new Vector3 (transform.localPosition.x, Mathf.Clamp (CameraDistance.y + (MouseY - MouseYSet) * Sensitivity * 10, CameraTarget.transform.localPosition.y + CameraTarget.transform.localScale.y / 2, CameraTarget.transform.localPosition.y + CameraTarget.transform.localScale.y / 2 + MaxPos), Mathf.Clamp (CameraDistance.z + (MouseY - MouseYSet) * Sensitivity * 10, CameraTarget.transform.localPosition.z - MaxPos, CameraTarget.transform.localPosition.z));
if (MouseY != 0)
{
transform.localRotation = Quaternion.Euler (CameraAngle, transform.localRotation.y, transform.localRotation.z);
transform.localPosition = new Vector3 (transform.localPosition.x, transform.localPosition.y + CameraDistance.y, transform.localPosition.z + CameraDistance.z);
MouseYSet = MouseY;
}
}
}
You should take your time and ask the question correctly explaining exactly what are you trying to achieve. People can't guess what is the real problem just from few lines of code.
The problem is its not moving up and down or back and forward right now
Answer by Scribe · Dec 29, 2014 at 10:07 PM
Though this is not directly related to the current problem you are asking about, you really need to learn from previous mistakes, the line:
transform.localRotation = Quaternion.Euler (CameraAngle, transform.localRotation.y, transform.localRotation.z);
is not going to work for the exact reason and answer you were given in this question previously. It honestly makes me ~personally~ far less willing to help if you don't actually learn from answers given.
But perhaps it was a genuine mistake, in which case you should refer to that answer linked!
Your answer
Follow this Question
Related Questions
2D Background Stop Moving on BoundingBox Exit? 2 Answers
Setting target position in Vector3.MoveTowards 2 Answers
Move object with finger swipe 0 Answers