I have been in the process of making my own custom character controller so it can move at different speeds that vary depending on the direction movement is being applied. I added a SmoothMouseLook script I found and have been trying to get the capsule’s rotation on the y-axis to match up with the camera the SmoothMouseLook was applied to. This is so that the character moves forward relative to the camera and not to the world. The camera is parented to the capsule gameObject (player). Here is the PlayerController script:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
private CharacterController player;
public float forwardMult = 1;
public float backwardMult = 1;
public float sidewaysMult = 1;
public float gravity = -2;
public float jumpHeight = 10;
private float jump = 0;
private Rigidbody rb_player;
private Camera firstCam;
void Start () {
player = gameObject.GetComponent<CharacterController> ();
rb_player = gameObject.GetComponent<Rigidbody> ();
firstCam = gameObject.GetComponentInChildren<Camera> ();
}
void FixedUpdate () {
float moveVertical = Input.GetAxis ("Vertical");
float moveHorizontal = Input.GetAxis ("Horizontal");
//Applies forward/backward multipliers
if (moveVertical > 0) {
moveVertical *= forwardMult;
}else if (moveVertical < 0){
moveVertical *= backwardMult;
}
//Applies Rotation Defined by Camera Look
float yRotation = firstCam.transform.rotation.y;
gameObject.transform.Rotate (new Vector3 (0.0f, yRotation, 0.0f));
yRotation = 0;
//Defines player movement
Vector3 verticalMovement = transform.forward * moveVertical * Time.deltaTime;
Vector3 horizontalMovement = transform.right * moveHorizontal * Time.deltaTime * sidewaysMult;
//Moves Player
player.Move (verticalMovement + horizontalMovement);
player.Move (new Vector3(0.0f, gravity, 0.0f));
}
}
I have tried many things to get the rotation to match up such as gameObject.transform.rotation = firstCam.transform.rotation (Unity says something about me trying to convert Vector3s into quaternions) and trying to rotate the capsule as is in the script currently(the player keeps on spinning indefinately). Here is the SmoothMouseLook script which I don't understand well:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[AddComponentMenu("Camera-Control/Smooth Mouse Look")]
public class SmoothMouseLook : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationX = 0F;
float rotationY = 0F;
private List<float> rotArrayX = new List<float>();
float rotAverageX = 0F;
private List<float> rotArrayY = new List<float>();
float rotAverageY = 0F;
public float frameCounter = 20;
Quaternion originalRotation;
void Update ()
{
if (axes == RotationAxes.MouseXAndY)
{
rotAverageY = 0f;
rotAverageX = 0f;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotArrayY.Add(rotationY);
rotArrayX.Add(rotationX);
if (rotArrayY.Count >= frameCounter) {
rotArrayY.RemoveAt(0);
}
if (rotArrayX.Count >= frameCounter) {
rotArrayX.RemoveAt(0);
}
for(int j = 0; j < rotArrayY.Count; j++) {
rotAverageY += rotArrayY[j];
}
for(int i = 0; i < rotArrayX.Count; i++) {
rotAverageX += rotArrayX*;*
-
}*
-
rotAverageY /= rotArrayY.Count;*
-
rotAverageX /= rotArrayX.Count;*
-
rotAverageY = ClampAngle (rotAverageY, minimumY, maximumY);*
-
rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX);*
-
Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);*
-
Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);*
_ transform.localRotation = originalRotation * xQuaternion * yQuaternion;_
-
}*
-
else if (axes == RotationAxes.MouseX)*
-
{ *
-
rotAverageX = 0f;*
_ rotationX += Input.GetAxis(“Mouse X”) * sensitivityX;_
-
rotArrayX.Add(rotationX);*
-
if (rotArrayX.Count >= frameCounter) {*
-
rotArrayX.RemoveAt(0);*
-
}*
-
for(int i = 0; i < rotArrayX.Count; i++) {*
_ rotAverageX += rotArrayX*;_
_ }_
_ rotAverageX /= rotArrayX.Count;*_
* rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX);*
* Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);*
_ transform.localRotation = originalRotation * xQuaternion; _
* }*
* else*
* { *
* rotAverageY = 0f;*
_ rotationY += Input.GetAxis(“Mouse Y”) * sensitivityY;_
* rotArrayY.Add(rotationY);*
* if (rotArrayY.Count >= frameCounter) {*
* rotArrayY.RemoveAt(0);*
* }*
* for(int j = 0; j < rotArrayY.Count; j++) {*
* rotAverageY += rotArrayY[j];*
* }*
* rotAverageY /= rotArrayY.Count;*
* rotAverageY = ClampAngle (rotAverageY, minimumY, maximumY);*
* Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);*
_ transform.localRotation = originalRotation * yQuaternion;_
* }*
* }*
* void Start ()*
* { *
* if (GetComponent())*
* GetComponent().freezeRotation = true;*
* originalRotation = transform.localRotation;*
* }*
* public static float ClampAngle (float angle, float min, float max)*
* {*
* angle = angle % 360;*
* if ((angle >= -360F) && (angle <= 360F)) {*
* if (angle < -360F) {*
* angle += 360F;*
* }*
* if (angle > 360F) {*
* angle -= 360F;*
* } *
* }*
* return Mathf.Clamp (angle, min, max);*
* }*
}
Any help is appreciated and maybe even a quick summary of how the SmoothMouseLook works.