- Home /
Custom Character Controller Errors?
The current script I have does extremely well when it doesn't have a "character body" aka when it has a capsule and such. When the character is attached it begins to act strangely, such as, i can look up and down but looking left or right isn't working, also when moving backwards the character and camera bounce, there is also (sometimes it occurs but not always) times where the capsule will spin out of control. As of now the camera has the script, the character controller and has a capsule parented. No Rigid Body or any other abnormality, with rigid bodies it tends to make the controller go all over the place in random directions, which is fine i don't want a rigid body. I tried seeking help in the forums and got no such luck, so i tried explaining it a little better here in hopes of a solution. Here is the script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("Camera/Simple Smooth Mouse Look")]
public class CustomFP : MonoBehaviour {
Vector2 _mouseAbsolute;
Vector2 _smoothMouse;
public Vector2 clampInDegrees = new Vector2(360, 180);
public bool lockCursor;
public Vector2 sensitivity = new Vector2(2, 2);
public Vector2 smoothing = new Vector2(3, 3);
public Vector2 targetDirection;
public Vector2 targetCharacterDirection;
public GameObject characterBody;
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
// Use this for initialization
void Start () {
targetDirection = transform.localRotation.eulerAngles;
if (characterBody) targetCharacterDirection = characterBody.transform.localRotation.eulerAngles;
}
// Update is called once per frame
void Update () {
Screen.lockCursor = lockCursor;
var targetOrientation = Quaternion.Euler(targetDirection);
var targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);
var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
_smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
_smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
_mouseAbsolute += _smoothMouse;
if (clampInDegrees.x < 360)
_mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);
var xRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right);
transform.localRotation = xRotation;
if (clampInDegrees.y < 360)
_mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);
transform.localRotation *= targetOrientation;
if (characterBody)
{
var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, Vector3.up);
characterBody.transform.localRotation = yRotation;
characterBody.transform.localRotation *= targetCharacterOrientation;
}
else
{
var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
transform.localRotation *= yRotation;
}
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}