Search Unity

Animation causing collider glitches

Discussion in 'Animation' started by ajx33, Nov 23, 2021.

  1. ajx33

    ajx33

    Joined:
    Jan 15, 2019
    Posts:
    2
    My animation is making my character teleport into the wall then the collider is teleporting him back out. How can I fix this? I believe it's because the blending slider is going from 0 instantly to 1 instead of gradually blending back to the idle animation. I've attached a video showing the problem, my animation script, and my player controller script.
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class ThirdPersonController : MonoBehaviour
    8. {
    9.     private MoveControls moveControls;
    10.     private InputAction move;
    11.  
    12.     private Rigidbody rb;
    13.  
    14.     [SerializeField] private float speed, jumpForce, maxSpeed;
    15.  
    16.     private Vector3 forceDir = Vector3.zero;
    17.  
    18.     [SerializeField] private Camera playerCamera;
    19.  
    20.     private Animator animator;
    21.  
    22.     private void Awake()
    23.     {
    24.         rb = this.GetComponent<Rigidbody>();
    25.         moveControls = new MoveControls();
    26.         animator = this.GetComponent<Animator>();
    27.     }
    28.  
    29.  
    30.     private void OnEnable()
    31.     {
    32.         moveControls.Player.Jump.started += Jump;
    33.         moveControls.Player.BasicAttack.started += BasicAttack;
    34.         move = moveControls.Player.Move;
    35.         moveControls.Player.Enable();
    36.     }
    37.  
    38.  
    39.     private void OnDisable()
    40.     {
    41.         moveControls.Player.Jump.started -= Jump;
    42.         moveControls.Player.BasicAttack.started -= BasicAttack;
    43.         moveControls.Player.Disable();
    44.     }
    45.  
    46.     private void FixedUpdate()
    47.     {
    48.         forceDir += move.ReadValue<Vector2>().x * GetCameraRight(playerCamera) * speed; // adjusts direction based off input and camera
    49.         forceDir += move.ReadValue<Vector2>().y * GetCameraForward(playerCamera) * speed;
    50.  
    51.         rb.AddForce(forceDir, ForceMode.Impulse);
    52.         forceDir = Vector3.zero;
    53.  
    54.         if (rb.velocity.y < 0f)
    55.         {
    56.             rb.velocity -= Vector3.down * Physics.gravity.y * Time.fixedDeltaTime;
    57.         }
    58.  
    59.         Vector3 horizontalVelocity = rb.velocity;
    60.         horizontalVelocity.y = 0;
    61.  
    62.         if (horizontalVelocity.sqrMagnitude > maxSpeed * maxSpeed)
    63.         {
    64.             rb.velocity = horizontalVelocity.normalized * maxSpeed + Vector3.up * rb.velocity.y;
    65.         }
    66.  
    67.         LookAt();
    68.     }
    69.  
    70.     private void LookAt()
    71.     {
    72.         Vector3 direction = rb.velocity;
    73.         direction.y = 0f;
    74.  
    75.         if (move.ReadValue<Vector2>().sqrMagnitude > 0.1f && direction.sqrMagnitude > 0.1f)
    76.         {
    77.             this.rb.rotation = Quaternion.LookRotation(direction, Vector3.up);
    78.         }
    79.         else
    80.         {
    81.             rb.angularVelocity = Vector3.zero;
    82.         }
    83.     }
    84.  
    85.     private Vector3 GetCameraForward(Camera playerCamera)
    86.     {
    87.         Vector3 forward = playerCamera.transform.forward;
    88.         forward.y = 0;
    89.         return forward.normalized;
    90.     }
    91.  
    92.     private Vector3 GetCameraRight(Camera playerCamera)
    93.     {
    94.         Vector3 right = playerCamera.transform.right;
    95.         right.y = 0;
    96.         return right.normalized;
    97.     }
    98.  
    99.     private void Jump(InputAction.CallbackContext obj)
    100.     {
    101.         if(IsGrounded())
    102.         {
    103.             forceDir += Vector3.up * jumpForce;
    104.         }
    105.     }
    106.  
    107.     private bool IsGrounded()
    108.     {
    109.         Ray ray = new Ray(this.transform.position + Vector3.up * 0.25f, Vector3.down);
    110.  
    111.         if (Physics.Raycast(ray, out RaycastHit hit, 0.3f))
    112.         {
    113.             return true;
    114.         }
    115.         else
    116.         {
    117.             return false;
    118.         }
    119.     }
    120.  
    121.     private void BasicAttack(InputAction.CallbackContext obj)
    122.     {
    123.         animator.SetTrigger("Attack");
    124.     }
    125.  
    126.     public float GetMaxSpeed()
    127.     {
    128.         return maxSpeed;
    129.     }
    130.  
    131.  
    132.  
    133. }

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ThirdPersonAnimation : MonoBehaviour
    6. {
    7.     private Animator animator;
    8.     private Rigidbody rb;
    9.  
    10.     [SerializeField] private ThirdPersonController controller;
    11.     private float maxSpeed;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         if (controller != null)
    17.         {
    18.             maxSpeed = controller.GetMaxSpeed();
    19.         }
    20.  
    21.         animator = this.GetComponent<Animator>();
    22.         rb = this.GetComponent<Rigidbody>();
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         float speed = rb.velocity.magnitude / maxSpeed;
    29.  
    30.         if (speed > 0)
    31.         {
    32.             animator.SetFloat("Speed", rb.velocity.magnitude / maxSpeed);
    33.         }
    34.        
    35.     }
    36. }
     
unityunity