I have been trying to implement a wall jump with AddForce, but I keep on getting an error:
"Assets\Movement.cs(56,62): error CS0119: 'Rigidbody2D.AddForce(Vector2)' is a method, which is not valid in the given context"
Does anyone know what's wrong with my script?
[SerializeField] private float speed;
[SerializeField] private float jumpPower;
[SerializeField] private LayerMask groundLayer;
[SerializeField] private LayerMask wallLayer;
private Rigidbody2D body;
private Animator anim;
private BoxCollider2D boxCollider;
private float wallJumpCooldown;
private float horizontalInput;
public float AddForce;
private void Awake()
{
body = GetComponent();
anim = GetComponent();
boxCollider = GetComponent();
}
private void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
if (horizontalInput > 0.01f)
transform.localScale = Vector3.one;
else if (horizontalInput < -0.01f)
transform.localScale = new Vector3(-1, 1, 1);
anim.SetBool("Running", horizontalInput != 0);
anim.SetBool("grounded", isGrounded());
if (wallJumpCooldown < 0.2f)
{
body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);
if (Input.GetKey(KeyCode.Space))
Jump();
}
wallJumpCooldown = Time.deltaTime;
}
private void Jump()
{
if (isGrounded())
{
body.velocity = new Vector2(body.velocity.x, jumpPower);
anim.SetTrigger("Jump");
}
else if (onWall() && !isGrounded())
{
//Here is the wall Jump
body.AddForce = new Vector(body.AddForce.y, body.AddForce.x, - 10, 5, ForceMode.Impulse);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
}
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;
}
}
↧