简体   繁体   中英

My player character won't stop snapping to the original Y axis using Unity's character controller

I have a really frustrating problem with what I think is Unity's character controller component. Whenever I move my player, my character is moving to it's original Y axis value when moving. The character doesn't fall from that Y position until I stop pressing a movement key. I used a tutorial from Brackey's for the momement, I'm not even sure if it is the code itself since I deleted it and used a different system and had the same result. Either way, here's the code:

 private void FixedUpdate()
    {
        //Movement control. Explanation found here: https://docs.unity3d.com/ScriptReference/Input.GetAxis.html
        
        //Grabs player input 
        float axisH = Input.GetAxisRaw("Horizontal");
        float axisV = Input.GetAxisRaw("Vertical");



        //Direction of player (linked to camera)

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

        float turnSmoothTime = 0.1f;
        float turnSmoothVelocity = 0;

        if (direction.magnitude >= 0.1f)
        {
            

            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            playerTransform.rotation = Quaternion.Euler(0f, angle, 0f);


            // playerTransform.position += new Vector3(axisH, 0, axisV).normalized * speed; //Moves player based on movement

            Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
            characterController.Move(moveDirection.normalized * speed);

          
           
        }

    }

As for the values of my character controller component, see below:

角色控制器

I've attempted to change all these values to various configurations with no luck. I can't find any posts on this forum or others solving my issue, those who have posed similar questions found resolutions eslewhere.

Any help would be greatly appreciated!

EDIT: I realised the character is not moving up a set amount, but instead what the Y value is originally set to in Unity.

Can you give some videos of the problem ? also the you can try adding rigidbody to the gameobject that character controller is on. The character controllers Move function can act strange , i recommend trying Move(your own values) to actually understand how character controller move its objects. Also try to change the Y axis with bruteforce , add some debugs in your code to understand where and how the Y value is changing.

https://docs.unity3d.com/ScriptReference/CharacterController.Move.html

also Simple Move may be better ;

https://docs.unity3d.com/ScriptReference/CharacterController.SimpleMove.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM