1
\$\begingroup\$

I have a character controller and after trying to implement moving independently of the camera it starts spinning uncontrollably. I have no idea why. Here is the code:

CharacterController:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Controller : MonoBehaviour {

public float moveSpeed;
public float jumpForce;
public float gravityScale;
public Animator anim;
public float rotateSpeed;
public Transform pivot;
public GameObject playerModel;

private CharacterController controller;
private Vector3 moveDirection;
//private bool isGrounded;
//private Rigidbody rigidbody;

// Use this for initialization
void Start () {

    //rigidbody = GetComponent<Rigidbody>();
    controller = GetComponent<CharacterController>();
    //isGrounded = true;
}

// Update is called once per frame
void Update () {

    //rigidbody.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, rigidbody.velocity.y, Input.GetAxis("Vertical") * moveSpeed);

    //moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, 0, Input.GetAxis("Vertical") * moveSpeed);
    float ystore = moveDirection.y;
    moveDirection = (transform.forward * Input.GetAxis("Vertical") * moveSpeed) + (transform.right * Input.GetAxis("Horizontal") * moveSpeed);
    moveDirection = moveDirection.normalized * moveSpeed;
    moveDirection.y = ystore;

    if (controller.isGrounded)
    {

        moveDirection.y = 0f;
        if (Input.GetButtonDown("Jump"))
        {

            moveDirection.y = jumpForce;
        }
    }

    moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
    controller.Move(moveDirection * Time.deltaTime);

    if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
    {
        transform.rotation = Quaternion.Euler(0f, pivot.rotation.eulerAngles.y, 0f);
        Quaternion newRotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0f, moveDirection.z));
        playerModel.transform.rotation = Quaternion.Slerp(playerModel.transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
    }

    anim.SetBool("isGrounded", controller.isGrounded);
    anim.SetFloat("Speed", (Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal"))));
}

/*
void OnTriggerEnter(Collider other)
{
    if (other.name == "Terrain")
    {
        isGrounded = true;
    }
}

void OnTriggerStay(Collider other)
{
    if (other.name == "Terrain")
    {
        isGrounded = true;
    }
}

void OnTriggerExit(Collider other)
{
    if (other.name == "Terrain")
    {
        isGrounded = false;
    }
}
*/
}

CameraController:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour {

public Transform target;
public bool useOffsetValues;
public float rotateSpeed;
public Transform pivot;
public float maxViewAngle;
public float minViewAngle;

private Vector3 offset;

// Use this for initialization
void Start () {

    if (!useOffsetValues)
    {

        offset = target.position - transform.position;
    }

    pivot.position = target.position;
    //pivot.parent = target;
    pivot.parent = null;

    Cursor.lockState = CursorLockMode.Locked;
}

// Update is called once per frame
void LateUpdate () {

    pivot.parent = target;

    //Get the x position of the mouse and rotate the target
    float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
    pivot.Rotate(0, horizontal, 0);

    //Get the y position of the mouse and rotate the pivot
    float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
    pivot.Rotate(-vertical, 0, 0);

    //Limit up camera rotation
    if (pivot.rotation.eulerAngles.x > maxViewAngle && pivot.rotation.eulerAngles.x < 180f)
    {
        pivot.rotation = Quaternion.Euler(maxViewAngle, 0, 0);
    }

    //Limit down camera rotation
    if (pivot.rotation.eulerAngles.x > 180f && pivot.rotation.eulerAngles.x < 360f + minViewAngle)
    {
        pivot.rotation = Quaternion.Euler(360f + minViewAngle, 0, 0);
    }

    //Move the camera based on the current rotation of the target and the original offset
    float desiredYAngle = pivot.eulerAngles.y;
    float desiredXAngle = pivot.eulerAngles.x;
    Quaternion rotation = Quaternion.Euler(desiredXAngle, desiredYAngle, 0);
    transform.position = target.position - (rotation * offset);

    if(transform.position.y < target.position.y + 1.5f)
    {
        transform.position = new Vector3(transform.position.x, target.position.y + 1.5f, transform.position.z);
    }

    //transform.position = target.position - offset;
    transform.LookAt(target.position + new Vector3(0, 1, 0));
}
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Have you used a debugger before? You should look up how to set a watchpoint and see where the rotation is being set and what it's being set to while running. That should help you narrow down why this is happening. \$\endgroup\$ – user1118321 Dec 14 '18 at 3:05
  • 2
    \$\begingroup\$ Welcome to a stack exchange site! In the future, please use the edit feature below the question tags to modify your question; avoid creating a duplicate. \$\endgroup\$ – Vaillancourt Dec 14 '18 at 3:26
  • 1
    \$\begingroup\$ Well, i start spinning when I turn my camera, and the more it has been turned in a direction the faster I spin when I press forward \$\endgroup\$ – user123054 Dec 15 '18 at 0:44
1
\$\begingroup\$

Here's one reason why: Quaternion.Slerp needs a 0..1 value as the third parameter, and you're using something that's definitely not between 0 and 1. You can try to use a constant value as your third parameter, let's say 0.05 for slow rotation or 0.2 for fast rotation and see if that fixes it. But if you want to limit the rotation speed to some degrees/second value, you'll need to look at the accepted solution here: https://stackoverflow.com/questions/20329460/unity3d-slerp-rotation-speed

\$\endgroup\$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.