Sword getting detached

i am doing the unity 2d combat rpg course and i am stuck in the the part where we have to create sword swing both direction…



when i click play the sword gets detached i checked the code no errors…`using UnityEngine;

public class Sword : MonoBehaviour
{

private PlayerControls playerControls;
private Animator myAnimator;
private PlayerController playerController;
private ActiveWeapon activeWeapon;

private void Awake()
{
    playerController = GetComponentInParent<PlayerController>();
    activeWeapon = GetComponentInParent<ActiveWeapon>();
    myAnimator = GetComponent<Animator>();
    playerControls = new PlayerControls();
}

private void OnEnable()
{
    playerControls.Enable();
}

// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
    playerControls.Combat.Attack.started += _ => Attack();
}

private void Update()
{
    MouseFollowWithOffset();
}

private void Attack()
{
    myAnimator.SetTrigger("Attack");
}

private void MouseFollowWithOffset()
{
    Vector3 mousePos = Input.mousePosition;
    Vector3 playerScreenPoint = Camera.main.WorldToScreenPoint(playerController.transform.position);

    if (mousePos.x < playerScreenPoint.x)
    {
        activeWeapon.transform.rotation = Quaternion.Euler(0, -180, 0);
    } else
    {
        activeWeapon.transform.rotation = Quaternion.Euler(0, 0, 0);
    }
}

}`

When you swing back in the other direction, does the sword regain it’s proper position?
My first instinct is that there is a key in the animation that is setting that X off by 3 or so, but I’m not certain.
Let’s take a look at your ActiveWeapon.cs and your Sword.cs scripts as well.

No it didn’t regain its normal position if I switched direction… But since u told tht there’s problem in animation I deleted my animation and made the animation again it works perfectly fine now… Thank you so much for guidance sir!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms