I didn’t really know what else to title this, but I’m making a 2d side scrolling game and I’m currently trying to figure out how to make it so when the character jumps, he will fall back down and hit the ground. I have created my code in C#, so when I run the game, the block just stays in the air and doesn’t fall, what am I doing wrong? I am new at this, so don’t hate on me too much. Here is the code so far:
Here is the Controller2d tab
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (BoxCollider2D))]
public class Controller2d : MonoBehaviour {
public LayerMask collisonMask;
const float skinWidth= 0.015f;
public int horizontalRayCount = 4;
public int verticalRayCount = 4;
float horizontalRaySpacing;
float verticalRaySpacing;
RaycastOrigins raycastOrigins;
void Start() {
BoxCollider2D collider;
collider = GetComponent<BoxCollider2D> ();
CalculateRaySpacing ();
}
public void Move(Vector3 velocity) {
UpdateRaycastOrigins();
VerticalCollisons (ref velocity);
transform.Translate (velocity);
}
void VerticalCollisons(ref Vector3 velocity) {
float directionY = Mathf.Sign (velocity.y);
float rayLength = Mathf.Abs (velocity.y) + skinWidth;
for (int i = 0; i < verticalRayCount; i ++) {
Vector2 rayOrigin = (directionY == -1)?raycastOrigins.bottomLeft : raycastOrigins.topLeft;
rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x);
RaycastHit2D hit = Physics2D.Raycast (rayOrigin, Vector2.up * directionY, rayLength, collisonMask);
Debug.DrawRay (raycastOrigins.bottomLeft + Vector2.right * verticalRaySpacing * i, Vector2.up * -2, Color.blue);
if (hit) {
velocity.y = (hit.distance - skinWidth) * directionY;
rayLength = hit.distance;
}
}
}
void UpdateRaycastOrigins() {
Bounds bounds = GetComponent<Collider>().bounds;
bounds.Expand (skinWidth * -2);
raycastOrigins.bottomLeft = new Vector2 (bounds.min.x, bounds.min.y);
raycastOrigins.bottomRight = new Vector2 (bounds.max.x, bounds.min.y);
raycastOrigins.topLeft = new Vector2 (bounds.min.x, bounds.max.y);
raycastOrigins.topRight = new Vector2 (bounds.max.x, bounds.max.y);
}
void CalculateRaySpacing() {
Bounds bounds = GetComponent<Collider>().bounds;
bounds.Expand (skinWidth * -2);
horizontalRayCount = Mathf.Clamp (horizontalRayCount, 2, int.MaxValue);
verticalRayCount = Mathf.Clamp (verticalRayCount, 2, int.MaxValue);
horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1);
verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
}
struct RaycastOrigins {
public Vector2 topLeft, topRight;
public Vector2 bottomLeft, bottomRight;
}
}
Here is the Player tab
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Controller2d))]
public class Player : MonoBehaviour {
float gravity = -20;
Vector3 velocity;
Controller2d controller;
void Start() {
controller = GetComponent<Controller2d> ();
}
void Update() {
velocity.y += gravity * Time.deltaTime;
controller.Move (velocity * Time.deltaTime);
}
}
I have created a new Layer named Obstacle and made the collision mark on the player set to “Obstacle”
P.S. Since I am new and want to learn, please don’t just give me the answer, instead, give me a hint on what I should look for or change