,Why isnt my attack working?
So I have two scripts, player movement and player attack. My can attack method is in the player movement script and I want to use it in the player attack. However when I type it into the player attack script it doesn't turn yellow like the other methods. I now cant attack in the game and I don't know how to fix it. Anyone know how to help? Here are both the scripts, player movement first.
using UnityEngine;
public class PlayerMovement : MonoBehaviour { [SerializeField] private float speed; [SerializeField] private float jumpPower; [SerializeField] private LayerMask groundLayer; [SerializeField] private LayerMask wallLayer; private Rigidbody2D body; private Animator anim8; private BoxCollider2D boxCollider; private float wallJumpCooldown; private float horizontalInput;
private void Awake()
{
body = GetComponent<Rigidbody2D>();
anim8 = GetComponent<Animator>();
boxCollider = GetComponent<BoxCollider2D>();
}
private void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
if (horizontalInput > 0.01f)
transform.localScale = Vector3.one;
else if (horizontalInput < -0.01f)
transform.localScale = new Vector3(-1, 1, 1);
//animator parameters
anim8.SetBool("Run", horizontalInput != 0);
anim8.SetBool("grounded", isGrounded());
//wall jumping
if (wallJumpCooldown > 0.2f)
{
body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);
if (onWall() && !isGrounded())
{
body.gravityScale = 0;
body.velocity = Vector2.zero;
}
else
body.gravityScale = 7;
if (Input.GetKey(KeyCode.Space))
Jump();
}
else
wallJumpCooldown += Time.deltaTime;
}
private void Jump()//jump method
{
if (isGrounded())
{
body.velocity = new Vector2(body.velocity.x, jumpPower);
anim8.SetTrigger("Jump");
}
else if (onWall() && !isGrounded())
{
if (horizontalInput == 0)
{
body.velocity = new Vector2(-Mathf.Sign(transform.localScale.x) * 10, 0);
transform.localScale = new Vector3(-Mathf.Sign(transform.localScale.x), transform.localScale.y, transform.localScale.z);
}
else
body.velocity = new Vector2(-Mathf.Sign(transform.localScale.x) * 3, 6);
wallJumpCooldown = 0;
}
}
private bool isGrounded()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, Vector2.down, 0.1f, groundLayer);
return raycastHit.collider != null;
}
private bool onWall()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, new Vector2(transform.localScale.x, 0), 0.1f, wallLayer);
return raycastHit.collider != null;
}
public bool canAttack()
{
return horizontalInput == 0 && isGrounded() && !onWall();
}
}
now player attack
using UnityEngine;
public class PlayerAttack : MonoBehaviour {
[SerializeField] private float attackCooldown;
private Animator anim8;
private PlayerMovement playerMovement;
private float cooldownTimer = Mathf.Infinity;
private void awake()
{
anim8 = GetComponent<Animator>();
playerMovement = GetComponent<PlayerMovement>();
}
private void update()
{
if (Input.GetMouseButton(0) && cooldownTimer > attackCooldown && playerMovement.canAttack())
attack();
cooldownTimer += Time.deltaTime;
}
private void attack()
{
anim8.SetTrigger("Attack");
cooldownTimer = 0;
}
},So I have two scripts, player movement and player attack. My can attack method is in the player movement script and I want to use it in the player attack. However when I type it into the player attack script it doesn't turn yellow like the other methods. I now cant attack in the game and I don't know how to fix it. Anyone know how to help? Here are both the scripts, player movement first.
using UnityEngine;
public class PlayerMovement : MonoBehaviour { [SerializeField] private float speed; [SerializeField] private float jumpPower; [SerializeField] private LayerMask groundLayer; [SerializeField] private LayerMask wallLayer; private Rigidbody2D body; private Animator anim8; private BoxCollider2D boxCollider; private float wallJumpCooldown; private float horizontalInput;
private void Awake()
{
body = GetComponent<Rigidbody2D>();
anim8 = GetComponent<Animator>();
boxCollider = GetComponent<BoxCollider2D>();
}
private void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
if (horizontalInput > 0.01f)
transform.localScale = Vector3.one;
else if (horizontalInput < -0.01f)
transform.localScale = new Vector3(-1, 1, 1);
//animator parameters
anim8.SetBool("Run", horizontalInput != 0);
anim8.SetBool("grounded", isGrounded());
//wall jumping
if (wallJumpCooldown > 0.2f)
{
body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);
if (onWall() && !isGrounded())
{
body.gravityScale = 0;
body.velocity = Vector2.zero;
}
else
body.gravityScale = 7;
if (Input.GetKey(KeyCode.Space))
Jump();
}
else
wallJumpCooldown += Time.deltaTime;
}
private void Jump()//jump method
{
if (isGrounded())
{
body.velocity = new Vector2(body.velocity.x, jumpPower);
anim8.SetTrigger("Jump");
}
else if (onWall() && !isGrounded())
{
if (horizontalInput == 0)
{
body.velocity = new Vector2(-Mathf.Sign(transform.localScale.x) * 10, 0);
transform.localScale = new Vector3(-Mathf.Sign(transform.localScale.x), transform.localScale.y, transform.localScale.z);
}
else
body.velocity = new Vector2(-Mathf.Sign(transform.localScale.x) * 3, 6);
wallJumpCooldown = 0;
}
}
private bool isGrounded()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, Vector2.down, 0.1f, groundLayer);
return raycastHit.collider != null;
}
private bool onWall()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, new Vector2(transform.localScale.x, 0), 0.1f, wallLayer);
return raycastHit.collider != null;
}
public bool canAttack()
{
return horizontalInput == 0 && isGrounded() && !onWall();
}
}
now player attack
using UnityEngine;
public class PlayerAttack : MonoBehaviour {
[SerializeField] private float attackCooldown;
private Animator anim8;
private PlayerMovement playerMovement;
private float cooldownTimer = Mathf.Infinity;
private void awake()
{
anim8 = GetComponent<Animator>();
playerMovement = GetComponent<PlayerMovement>();
}
private void update()
{
if (Input.GetMouseButton(0) && cooldownTimer > attackCooldown && playerMovement.canAttack())
attack();
cooldownTimer += Time.deltaTime;
}
private void attack()
{
anim8.SetTrigger("Attack");
cooldownTimer = 0;
}
}