So I’ve got some code for a simple rigidbody character, I’ll go ahead and post it:
using UnityEngine;
using System.Collections;
public class Controller3 : MonoBehaviour {
public float runAccel = 5f;
public float jumpForce = 5f;
public float maxRunSpeed = 20f;
public CameraController camController;
public GameObject cameraObject;
public float slopeLimit = 45f;
[HideInInspector]
public bool isGrounded = false;
[HideInInspector]
public Vector2 horizontalVel;
[HideInInspector]
public Vector3 camRot;
// Use this for initialization
void Start () {
cameraObject = GameObject.Find("Main Camera");
camController = cameraObject.GetComponent<CameraController>();
}
void Update () {
//limit horizontal velocity
horizontalVel = new Vector2(rigidbody.velocity.x,rigidbody.velocity.z);
if(horizontalVel.magnitude > maxRunSpeed){
rigidbody.AddRelativeForce(-Input.GetAxis("Horizontal") * runAccel, 0, -Input.GetAxis("Vertical") * runAccel);
}
//make player face same direction as camera from cam controller script
transform.rotation = Quaternion.Euler(0,camController.curVertRot,0);
//movement control
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal")*runAccel,0,Input.GetAxis("Vertical")*runAccel);
if(Input.GetKeyDown(KeyCode.Space) && isGrounded){
rigidbody.AddRelativeForce(0,jumpForce,0);
}
}
void OnCollisionStay(Collision collision){
//if we are on a surface whos slope is less than slope limit, we are grounded
foreach(ContactPoint contact in collision.contacts){
if(Vector3.Angle(contact.normal,Vector3.up) < slopeLimit){
isGrounded = true;
}
}
}
void OnCollisionExit(){
//once we leave any collision surface, we are not grounded
isGrounded = false;
}
}
The problem occurs on the line:
//make player face same direction as camera from cam controller script
transform.rotation = Quaternion.Euler(0,camController.curVertRot,0);
And while the camera is childed to the player. When I run this script it makes my character face the same way as my camera on the y axis, just like I want. However, my camera get’s really jittery and shaky every frame. Like it’s trying to readjust its rotation and snapping back every frame. After I narrowed the problem to that piece of code (by commenting out until the problem went away), I tried replacing it with various other snippets to emulate the rotation.
//make player face same direction as camera from cam controller script
transform.rotation = Quaternion.Euler(0,Camera.main.transform.rotation.y,0);
Or this one:
//make player face same direction as camera from cam controller script
transform.rotation = Quaternion.Euler(transform.rotation.x,Camera.main.transform.rotation.y,transform.rotation.z);
And this one:
[HideInInspector]
public Vector3 camRot;
void Update() {
//make player face same direction as camera from cam controller script
camRot = camController.transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler(transform.rotation.x,camRot.y,transform.rotation.z);
}
Along with a couple other variants. Nothing seems to work, so I tried detaching the main camera from the player, the shaking went away and my camera started acting normally. However, now my camera obviously wont follow the player so it’s useless. Anybody know a way to fix this? Thanks.