im not sure if its the CODE or the Inspectors Components.
The tutorial i used:
Unity 2D Character Controller Tutorial - Roystan
The code: (its pretty long)
using UnityEngine;
[RequireComponent(typeof(BoxCollider2D))]
public class PlayerMovement : MonoBehaviour
{
[SerializeField, Tooltip("Max speed, in units per second, that the character moves.")]
float speed = 9;
[SerializeField, Tooltip("Acceleration while grounded.")]
float walkAcceleration = 75;
[SerializeField, Tooltip("Acceleration while in the air.")]
float airAcceleration = 30;
[SerializeField, Tooltip("Deceleration applied when character is grounded and not attempting to move.")]
float groundDeceleration = 70;
[SerializeField, Tooltip("Max height the character will jump regardless of gravity")]
float jumpHeight = 4;
private BoxCollider2D boxCollider;
private Vector2 velocity;
/// <summary>
/// Set to true when the character intersects a collider beneath
/// them in the previous frame.
/// </summary>
private bool grounded;
private void Awake()
{
boxCollider = GetComponent<BoxCollider2D>();
}
private void Update()
{
// Use GetAxisRaw to ensure our input is either 0, 1 or -1.
float moveInput = Input.GetAxisRaw("Horizontal");
if (grounded)
{
velocity.y = 0;
if (Input.GetButtonDown("Jump"))
{
// Calculate the velocity required to achieve the target jump height.
velocity.y = Mathf.Sqrt(2 * jumpHeight * Mathf.Abs(Physics2D.gravity.y));
}
}
float acceleration = grounded ? walkAcceleration : airAcceleration;
float deceleration = grounded ? groundDeceleration : 0;
if (moveInput != 0)
{
velocity.x = Mathf.MoveTowards(velocity.x, speed * moveInput, acceleration * Time.deltaTime);
}
else
{
velocity.x = Mathf.MoveTowards(velocity.x, 0, deceleration * Time.deltaTime);
}
velocity.y += Physics2D.gravity.y * Time.deltaTime;
transform.Translate(velocity * Time.deltaTime);
grounded = false;
// Retrieve all colliders we have intersected after velocity has been applied.
Collider2D[] hits = Physics2D.OverlapBoxAll(transform.position, boxCollider.size, 0);
foreach (Collider2D hit in hits)
{
// Ignore our own collider.
if (hit == boxCollider)
continue;
ColliderDistance2D colliderDistance = hit.Distance(boxCollider);
// Ensure that we are still overlapping this collider.
// The overlap may no longer exist due to another intersected collider
// pushing us out of this one.
if (colliderDistance.isOverlapped)
{
transform.Translate(colliderDistance.pointA - colliderDistance.pointB);
// If we intersect an object beneath us, set grounded to true.
if (Vector2.Angle(colliderDistance.normal, Vector2.up) < 90 && velocity.y < 0)
{
grounded = true;
}
}
}
}
}
When i touch the floor, it teleports me back up, making it so i cant jump, and it just doesnt look good too.
on the player, i have a sprite renderer, transform, box collider, (no rigid body because i cant have one with the code im using.)
since im new, dunno how to fix it, and theres no tutorials on how to fix this issue… im going here. please help!
