0

I Am trying to add a swinging system to my game but whenever my player does the swinging animation it goes back to the same place how would I fix something like that? here is a video showing what it does: https://streamable.com/7jxgqx

I Use a character controller to move the player so does anyone know how to prevent the player from snapping back to the place it came from

Problem: https://streamable.com/7jxgqx

my codes: 1)

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

public class Swinging : MonoBehaviour
{

    [SerializeField]Animator m_Animator;
    CharacterController cc;
    [SerializeField]GameObject jumpArea;
    PlayerController playerController;

    private void Start()
    {
        cc = GetComponent<CharacterController>();
        playerController = GetComponent<PlayerController>();
        playerController.enabled = true;
        cc.enabled = true;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Collider"))
        {
            StartCoroutine(StartSwinging());
        }
    }

    IEnumerator StartSwinging()
    {
        m_Animator.SetTrigger("swing");
        cc.enabled = false;
        playerController.enabled = false;
        yield return new WaitForSeconds(2.3f);
        m_Animator.SetBool("afterSwing", true);
        Destroy(jumpArea);
        playerController.enabled = true;
        cc.enabled = true;
    }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    CharacterController cc;
    public Transform groundCheck;
    public LayerMask groundLayer;
    float wallJumpVelocity;
    public Animator m_Animator;
    private Vector3 direction;
    public float speed = 5f;
    public float jumpForce = 8f;
    public float gravity = -20f;

    public bool canDoubleJump = true;


    public bool isGrounded;
    void Start()
    {
        cc = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        direction.x = horizontalInput * speed;
        m_Animator.SetFloat("run", Mathf.Abs(horizontalInput)); // Mathf.Abs i igivea rac  modulebi anu |-5| = 5 
        isGrounded = Physics.CheckSphere(groundCheck.position, 0.2f, groundLayer);
        m_Animator.SetBool("isGrounded", isGrounded);

        Jump();
        if (horizontalInput != 0)
        {
            Quaternion flip = Quaternion.LookRotation(new Vector3(0, 0, horizontalInput));
            transform.rotation = flip;
        }
        cc.Move(direction * Time.deltaTime);
    }

    void Jump()
    {
        // es kodi anichebs chvens motamashes axtomis funqicas

        if (isGrounded)
        {
            canDoubleJump = true;
            if (Input.GetButtonDown("Jump"))
            {
                direction.y = jumpForce;
            }
        }
        else
        {
            if (canDoubleJump && Input.GetButtonDown("Jump"))
            {
                m_Animator.SetTrigger("doubleJump");
                direction.y = jumpForce;
                canDoubleJump = false;
            }
        }

        direction.y += gravity * Time.deltaTime;
    }

}

I Tried applying root motion too but it did not work i am using animations from mixamo.

Thanks <3

  • 1
    Id guess you need root motion and to not be trying to move while the animation plays/ends as a start. – BugFinder Feb 16 '22 at 17:25
  • yeah I dont know much about animations in unity but for these types of questions turning on root motion always seems to be in the answer. – Ruzihm Feb 16 '22 at 19:27
  • Enabling "Write Defaults" on the animation state should be what you are looking for: https://docs.unity3d.com/Manual/class-State.html – JDormer Feb 17 '22 at 09:26

0 Answers0