- Home /
Camera and Character Rotate on Y axis
I apologize if this is a simple fix. Okay, right now I have a ball (Player) continuously rotating on x and y axis's when controlled. I have a free moving camera thats not a child nor parent and is locked to the rolling ball. That's all good. But, I want the camera to rotate the character on its Y axis, so when the Player looks around, he's able to move "forward" in the direction he is pointing. I'm been coding for a while now, but this one stumped me. Any clues? Seriously thank you in advance. This is the last piece to my game.
Camera Script:
public float distance = 5.0f;
public float bufferup = 1.5f;
public float bufferright = 0.75f;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
private float x = 0.0f;
private float y = 0.0f;
void Start () {
Cursor.lockState = CursorLockMode.Locked;
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
if(GetComponent<Rigidbody>())
GetComponent<Rigidbody>().freezeRotation = true;
}
void LateUpdate (){
if(target)
{
distance -= .5f * Input.mouseScrollDelta.y;
if(distance < 0)
{
distance = 0;
}
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y,x,0);
Vector3 position = rotation * new Vector3(bufferright, 0.0f, -distance) + target.position + new Vector3(0.0f, bufferup, 0.0f);
transform.rotation = rotation;
transform.position = position;
}
}
float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}
Your answer
Follow this Question
Related Questions
Rotating and Translating a GameObject in relation to an AR Camera in Unity/Vueforia 0 Answers
How do I get my forward movement to match with the direction of my camera? 1 Answer
make a target camera? 1 Answer
camera movments fixed.. character controller without using character controller -.-' 2 Answers