Player damages without pressing attack

Im at 71% in the course but since the start of the course the damage from the player against enemies and the bushes is turned on and just walking into them damages them UNTIL I press attack and then it functions normally. I tried calling attack in the start and awake functions hoping that it would run as the game started but it hasnt fixed it. I had hoped it would iron out later in the course but as I am near the end it has maintained this issue. Ill post some scripts below and screenshots of my player’s inspector

Player Controls Script:strong text

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PlayerController : Singleton<PlayerController>

{

    public bool FacingLeft{get {return facingLeft;} }  

   

    [SerializeField] private float moveSpeed = 1f;

    [SerializeField] private float dashSpeed = 4f;

    [SerializeField] private TrailRenderer myTrailRenderer;

    [SerializeField] private Transform weaponCollider;

    private PlayerControls playerControls;

    private Vector2 movement;

    private Rigidbody2D rb;

    private Animator myAnimator;

    private SpriteRenderer mySpriteRenderer;

    private Knockback knockback;

    private float startingMoveSpeed;

    private bool facingLeft = false;

    private bool isDashing = false;

    protected override void Awake() {

        base. Awake();

        playerControls = new PlayerControls();

        rb = GetComponent <Rigidbody2D>();

        myAnimator = GetComponent <Animator>();

        mySpriteRenderer = GetComponent<SpriteRenderer>();

        knockback = GetComponent<Knockback>();

    }

    private void Start(){

        playerControls.Combat.Dash.performed +=_=> Dash();

        startingMoveSpeed = moveSpeed;

    }

    private void OnEnable() {

        playerControls.Enable();

       

    }

    private void Update(){

        PlayerInput();

    }

    private void FixedUpdate() {

        AdjustPlayerFacingDirection();

        Move();

    }

    public Transform GetWeaponCollider(){

        return weaponCollider;

    }

    private void PlayerInput(){

        movement = playerControls.Movement.Moves.ReadValue<Vector2>();

        myAnimator.SetFloat("movex", movement.x);

        myAnimator.SetFloat("movey", movement.y);

       

    }

    private void Move(){

        if (knockback.GettingKnockedBack) {return;}

        rb.MovePosition(rb.position + movement * (moveSpeed *Time.fixedDeltaTime));

    }

    private void AdjustPlayerFacingDirection(){

        Vector3 mousPos = Input.mousePosition;

        Vector3 playerScreenPoint = Camera.main.WorldToScreenPoint(transform.position);

        if (mousPos.x < playerScreenPoint.x) {

            mySpriteRenderer.flipX = true;

            facingLeft = true;

        } else {

            mySpriteRenderer.flipX = false;

            facingLeft = false;

        }

   

        }

        private void Dash(){

        if(!isDashing){

            isDashing = true;

            moveSpeed *= dashSpeed;

            myTrailRenderer.emitting = true;

            StartCoroutine(EndDashRoutine());

        }

    }

    private IEnumerator EndDashRoutine(){

        float dashTime = .2f;

        float dashCD = .25f;

        yield return new WaitForSeconds(dashTime);

        moveSpeed =startingMoveSpeed;

        myTrailRenderer.emitting = false;

         yield return new WaitForSeconds(dashCD);

         isDashing = false;

    }

   

}

Sword Script



using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Sword : MonoBehaviour, IWeapon

{

[SerializeField] private GameObject slashAnimPrefab;

[SerializeField] private Transform slashAnimSpawnPoint;

[SerializeField] private float swordAttackCD = .5f;

[SerializeField] private WeaponInfo weaponInfo;

private Transform weaponCollider;

private Animator myAnimator;

private GameObject slashAnim;

private void Awake(){

               

    myAnimator = GetComponent <Animator>();

           

}

private void Start(){

    weaponCollider = PlayerController.Instance.GetWeaponCollider();

    slashAnimSpawnPoint = GameObject.Find("Slash Animation Spawn Point").transform;

}

    private void Update(){

        MouseFollowWithOffset();

       

    }

    public WeaponInfo GetWeaponInfo(){

        return weaponInfo;

    }

 

    public void Attack(){

               

        myAnimator.SetTrigger("Attack");

        weaponCollider.gameObject.SetActive(true);

        slashAnim = Instantiate (slashAnimPrefab, slashAnimSpawnPoint.position, Quaternion.identity);

        slashAnim.transform.parent = this.transform.parent;

                       

    }

     

    public void DoneAttackingAnimEvent(){

        weaponCollider.gameObject.SetActive(false);

    }

    public void SwingUpFlipAnim(){

        slashAnim.gameObject.transform.rotation = Quaternion.Euler(-180,0,0);

        if (PlayerController.Instance.FacingLeft){

            slashAnim.GetComponent<SpriteRenderer>().flipX = true;

        }

    }

    public void SwingDownFlipAnim(){

        slashAnim.gameObject.transform.rotation = Quaternion.Euler(0,0,0);

        if (PlayerController.Instance.FacingLeft){

            slashAnim.GetComponent<SpriteRenderer>().flipX = true;

        }

    }

   
   

    private void MouseFollowWithOffset(){

        Vector3 mousePos = Input.mousePosition;

        Vector3 playerScreenPoint = Camera.main.WorldToScreenPoint(PlayerController.Instance.transform.position);

        float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;

        if (mousePos.x < playerScreenPoint.x){

            ActiveWeapon.Instance.transform.rotation = Quaternion.Euler(0, -180, angle);

            weaponCollider.transform.rotation = Quaternion.Euler(0, -180,0);

           

        } else {

            ActiveWeapon.Instance.transform.rotation = Quaternion.Euler(0, 0, angle);

            weaponCollider.transform.rotation = Quaternion.Euler(0, 0,0);

        }

    }

    // Update is called once per frame

   

}

Damage Source

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class DamageSource : MonoBehaviour

{

   private int damageAmount;

   private void Start(){

           

         MonoBehaviour currentActiveWeapon = ActiveWeapon.Instance.CurrentActiveWeapon;

         damageAmount = (currentActiveWeapon as IWeapon).GetWeaponInfo().weaponDamage;

    }

   

   private void OnTriggerEnter2D(Collider2D other){

    EnemyHealth enemyHealth = other.gameObject.GetComponent<EnemyHealth>();

    enemyHealth?.TakeDamage(damageAmount);

   

   }

}
```csharp
![player screenshot 1|690x337](upload://5KEZqSafCnDn8X1S40KWpkuOdSk.png)
![player screenshot 2|690x340](upload://7910GCT6TJXRJS3FR6HP3hYgA9H.png)

So you’re hitting everything without attacking until the first time you attack, then everything functions normally? Most likely, the weaponCollider is nice and active when the game starts until the sword’s animations turn it off.

Add

weaponCollider.gameObject.SetActive(false);

to the end of Start to ensure that the WeaponCollider is properly disabled.

1 Like

Thank you dude…

I added that to my player controller start and it seems to have fixed it.

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

Privacy & Terms