Home > OS >  Unity Character/Camera Movement gone haywire
Unity Character/Camera Movement gone haywire

Time:11-22

Im working on a game in unity and I followed a dapper dino tutorial for character movement and character camera control. Everything was working with a few minor issues, most of which I solved, but the one issue I couldnt solve, was when I move the camera to face more the 90 degrees left or right, the character just spins out of control, and I spent a long time scrolling through comments and watching the other videos and stuff, but nothing seemed to work. Here is my code:

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

public class PlayerMovementController : MonoBehaviour
{
    [SerializeField] private float speed;
    [SerializeField] private float jumpForce;
    [SerializeField] private float JumpraycastDistance;

    private Rigidbody rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        Jump();
    }

    private void FixedUpdate()
    {
        Move();
    }

    private void Move()
    {
        float hAxis = Input.GetAxisRaw("Horizontal");
        float vAxis = Input.GetAxisRaw("Vertical");

        Vector3 movement = new Vector3(hAxis, 0, vAxis) * speed * Time.fixedDeltaTime;

        Vector3 newPosition = rb.position   rb.transform.TransformDirection(movement);

        rb.MovePosition(newPosition);
    }
    private void Jump()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            if (IsGrounded())
            {
                rb.AddForce(0, jumpForce, 0, ForceMode.Impulse);
            }
        }

    }

    private bool IsGrounded()
    {
        return Physics.Raycast(transform.position, Vector3.down, JumpraycastDistance);
    }
}

Video of broken character

ANY AND ALL HELP GREATLY APPRECIATED

I tried a bunch of stuff from the youtube comments of the video I was watching and it didnt solve anything

Camera code:

    [SerializeField] private float lookSensitivity;
[SerializeField] private float smoothing;

private GameObject player;
private Vector2 smoothedVelocity;
private Vector2 currentLookingPos;

private void Start()
{
    player = transform.parent.gameObject;
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
}

private void Update()
{
    RotateCamera();
    CheckForShooting();
}

private void RotateCamera()
{
    Vector2 inputeValues = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
    inputeValues = Vector2.Scale(inputeValues, new Vector2(lookSensitivity * smoothing, lookSensitivity * smoothing));


    smoothedVelocity.x = Mathf.Lerp(smoothedVelocity.x, inputeValues.x, 1f / smoothing);
    smoothedVelocity.y = Mathf.Lerp(smoothedVelocity.y, inputeValues.y, 1f / smoothing);

    currentLookingPos  = smoothedVelocity;

    currentLookingPos.y = Mathf.Clamp(currentLookingPos.y, -80f, 80f);

    transform.localRotation = Quaternion.AngleAxis(-currentLookingPos.y, Vector3.right);
    player.transform.localRotation = Quaternion.AngleAxis(currentLookingPos.x, player.transform.up);
}

CodePudding user response:

I was playing around and researching and I found an answer on the Unity forums it basically explained that my issue is known as "Gimbal Lock" and to fix it you add a minimum and maximum y value. Link to post here

CodePudding user response:

I FIXED IT we love Quaternions and Vector3s, Basically I changed

transform.localRotation = Quaternion.AngleAxis(-currentLookingPos.y, Vector3.right);
player.transform.localRotation = Quaternion.AngleAxis(currentLookingPos.x, player.transform.up);

to

        player.transform.localRotation = Quaternion.AngleAxis(currentLookingPos.x, Vector3.up);
  • Related