character gets stuck when walking into walls (solved, kind of)

6190185--678735--main camera.png (NOTE: someone on twitch told me how they did it so here’s a fix though this might not work for everyone what you can do is make a rigidbody character controller that moves with velocity) hello sorry for the inconvenience I’m fairly new to programming so this is why i come for help, my player character gets stuck a little ways away when walking into walls any advice would be nice as long it can remain 2.5d (entities that are 2d yet a world that is 3d)

character controller code

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

public class ThridPersonMovement : MonoBehaviour
{
     public CharacterController controller;

    public Transform cam;

    public float speed = 6f;

    public float gravity = -10.00f;

    public float turnSmoothTime = 0.1f;

    public float turnSmoothVelocity = 1f;

    public float jumpHeight = 2f;

    public Transform GroundCheck;

    public float groundDistance = 0.4f;

    public LayerMask groundMask;

    Vector3 velocity;

    bool isGrounded;

    // Update is called once per frame
    void Update()
    {
           isGrounded = Physics.CheckSphere(GroundCheck.position, groundDistance, groundMask);

       if(isGrounded && velocity.y < 0)
       {
           velocity.y = -2f;
       }
       float Vertical = Input.GetAxisRaw("Vertical");

       Vector3 direction = new Vector3(0f, 0f, Vertical).normalized;

       if(Input.GetButtonDown("Jump") && isGrounded)
       {
           velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
       }
       velocity.y += gravity * Time.deltaTime;controller.Move(velocity * Time.deltaTime);

       if (direction.magnitude >= 0.1f)
       {
            float targetAngle = Mathf.Atan(direction.x) * Mathf.Rad2Deg + cam.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);
            Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
            controller.Move(moveDir.normalized * speed * Time.deltaTime);
       }
    }
}

camera controller

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

public class CamController : MonoBehaviour
{
    private Transform _xForm_Camera;
    private Transform _xForm_Parent;

    private Vector3 _LocalRotation;

    public float MouseSensitivity = 4f;
    public float OrbitDampening = 10f;
    public float turnSmoothTime = 0.1f;
    float turnSmoothVelocity = 1f;

    public bool CameraDisabled = false;

    // Start is called before the first frame update
    void Start()
    {
        if (_xForm_Parent)
        {
            float dist = Vector3.Distance(this._xForm_Parent.position, transform.position);
            print("Distance to _xForm_Parent: " + dist);
        }

        Cursor.lockState = CursorLockMode.Locked;
        this._xForm_Camera = this.transform;
        this._xForm_Parent = this.transform.parent;

    }


    // Update is called once per frame, ater Update() on every game object in the scene
    void LateUpdate()
    {
        if (Input.GetKeyDown("c"))
        {
            CameraDisabled = !CameraDisabled;
        }


        if (!CameraDisabled)
        {
            //Rotation of the Camera based one Mouse Coordinates
            if (Input.GetAxis("Mouse X") != 0)
            {
                _LocalRotation.x += Input.GetAxis("Mouse X") * MouseSensitivity;
            }
        }
        Quaternion QT = Quaternion.Euler(0, _LocalRotation.x, 0);
        this._xForm_Parent.rotation = Quaternion.Lerp(this._xForm_Parent.rotation, QT, Time.deltaTime * OrbitDampening);
    }
}

Can you please fix the formatting on your code? It’s all on one line!

1 Like

fixed it 28 minutes ago, realized just after I posted, sorry.