11,579 questions
Super Mario player movement Script help && newish to c#
SaldelticCode
1
Reputation point
i am getting the error PlayerMovement.ApplyGravity()' must declare a body because it is not marked abstract, extern, or partial [Assembly-CSharp]csharp(CS0501) I am currently working on a Super Mario lvl 1-1 unity project and am following along a tutorial for it. When i got to the, "ApplyGravity()," code block suddenly none of the code gets referenced and it comes back full of errors. How can I fix it?
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private new Camera camera;
private new Rigidbody2D rigidbody;
private float inputAxis;
private Vector2 velocity;
public float moveSpeed = 8.0f;
public float maxJumpHeight = 5.0f;
public float maxJumpTime = 1.0f;
public float jumpForce => (2.0f * maxJumpHeight) / (maxJumpTime / 2);
public float gravity => (-2.0f * maxJumpHeight) / Mathf.Pow((maxJumpTime / 2.0f), 2);
public bool grounded{ get; private set; }
public bool jumping { get; private set; }
private void Awake()
{
rigidbody = GetComponent<Rigidbody2D>();
camera = Camera.main;
}
private void Update()
{
HorizontalMovement();
grounded = rigidbody.Raycast(Vector2.down);
if(grounded)
{
GroundedMovement();
}
ApplyGravity();
}
private void HorizontalMovement()
{
inputAxis = Input.GetAxis("Horizontal");
velocity.x = Mathf.MoveTowards(velocity.x, inputAxis * moveSpeed, moveSpeed * Time.deltaTime);
}
private void FixedUpdate()
{
Vector2 position = rigidbody.position;
position += velocity * Time.deltaTime;
Vector2 leftEdge = camera.ScreenToWorldPoint(Vector2.zero);
Vector2 rightEdge = camera.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
position.x = Mathf.Clamp(position.x, leftEdge.x - 4.5f, rightEdge.x + 0.5f);
rigidbody.MovePosition(position);
}
void GroundedMovement()
{
velocity.y = Mathf.Max(velocity.y, 0f);
jumping = velocity.y > 0f;
if (Input.GetButtonDown("Jump"))
{
velocity.y = jumpForce;
jumping = true;
}
}
void ApplyGravity();
{
bool falling = velocity.y < 0f || !Input.GetButton("Jump");
float multiplier = falling ? 2.0f : 1.0f;
velocity.y += gravity * multiplier * Time.deltaTime;
velocity.y = Mathf.Max(velocity.y, gravity / 2.0f);
}
}
Developer technologies | C#
Sign in to answer