Question by
Ducktor_Cid · Jun 24, 2017 at 12:56 PM ·
jumpingplatformer
Jumping with convex ground
I'm trying to make my 2D player jump. Currently, I'm casting a ray ALONG the bottom of my rectangle player (only way I could find out by myself to stop a problem where if I casted a single ray down the centre, the player could only jump is more than half of their body was on the ground/platform)
The main problem I have now is that it doesn't really like the convex shapes that can litter my game. I'm using an asset called Unity sprite cutter which allows me to cut a 2D shape in anyway. If I try to jump onto a part that I've cut into a wedge shape, it can cause my character to either go incredibly high or not be able to jump at all. Anyone know how I can fix this?
My current player control code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterMovement : MonoBehaviour {
public Rigidbody2D rBody;
[Range(1, 10)]
public int Speed;
private float lastDistance;
public bool isGrounded = true;
private LayerMask environment;
void Start () {
environment = LayerMask.GetMask("Environment");
}
void FixedUpdate() {
// Check if the user is attempting to jump
if (Input.GetKey(KeyCode.A)) {
rBody.velocity = new Vector2(-Speed, rBody.velocity.y);
}
if (Input.GetKey(KeyCode.D)) {
rBody.velocity = new Vector2(Speed, rBody.velocity.y);
}
if (Input.GetKey(KeyCode.Space)) {
RaycastHit2D hit2D = Physics2D.Raycast(rBody.position+new Vector2(0,-1.5f), Vector2.right,1f,environment);
if (hit2D) {
if (hit2D.distance < lastDistance) {
lastDistance = hit2D.distance;
}
else {
lastDistance = 100f;
rBody.AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
}
}
}
}
}
Comment