- Home /
Platform Collider Not Working When Enemy Collides With It
So, I have this infinite runner / platformer game in which I generate an enemy on a platform. The platforms have 2 enemy colliders that is supposes to collide with the enemy, but my enemies just go past the colliders. My 2 colliders (left and right collider) are a child of my platforms. My platforms are generate using a script that is attached to my first platform.
Enemy.cs Script
using UnityEngine;
public class Enemy : MonoBehaviour {
private SpriteRenderer sp;
private float disMax;
private float disMin;
private float dis; // Distance travel / frame
private void Start() {
sp = GetComponent<SpriteRenderer>();
PickDis();
Flip();
}
private void Update() {
Move();
}
/// <summary>
/// Makes enemy move
/// </summary>
private void Move() {
float x = transform.position.x + dis * Time.deltaTime;
transform.position = new Vector3(x, transform.position.y, transform.position.z);
}
/// <summary>
/// Picks a random distance / frame
/// </summary>
private void PickDis() {
disMin = -1f;
disMax = 1f;
float rand = 0f;
while(rand == 0f) {
rand = Random.Range(disMin, disMax);
}
dis = rand;
}
private void Flip() {
if (dis > 0f) {
// Right
sp.flipX = true;
} else if (dis < 0f) {
// Left
sp.flipX = false;
}
}
}
Platform Generator Script
using UnityEngine;
public class GeneratePlatforms : MonoBehaviour {
public static GeneratePlatforms Instance = null;
[SerializeField] private GameObject platform;
private SpriteRenderer sp;
private Camera cam; // Main camera
private float camHeight; // Height of the camera view port
private Vector2 currentPos; // Last platforms position
private int maxPlatforms; // Maximum number of platforms
// Min and max height range for generating platforms
private float yMin;
private float yMax;
// Min and max scale range for width
private float scaleWMin;
private float scaleWMax;
private float gap; // Gap between each platform
private float w; // Width of the platform object
private void Start() {
sp = GetComponent<SpriteRenderer>();
cam = Camera.main;
camHeight = 2 * cam.orthographicSize;
currentPos = transform.position;
maxPlatforms = 20;
yMin = (-camHeight / 2) + 0.8f;
yMax = (camHeight / 2) - 3f;
scaleWMin = 0.05f;
scaleWMax = 0.14f;
gap = 7f;
w = sp.bounds.size.x;
if (Instance == null) {
Instance = this;
} else if (Instance != this) {
Destroy(gameObject);
}
Generate(maxPlatforms);
}
/// <summary>
/// Generates max number of platforms on start
/// </summary>
/// <param name="max">Max number of platforms to generate</param>
public void Generate(int max) {
for (int i = 0; i < max; i++) {
float randomY = Random.Range(yMin, yMax);
float randomScaleW = Random.Range(scaleWMin, scaleWMax);
// Up
if (randomY > currentPos.y + 3f) {
gap = 10f;
}
// Down
if (randomY < currentPos.y - 4f) {
gap = 5f;
}
float x = (currentPos.x + w) + gap;
Vector2 lastPos = new Vector2(x, randomY);
GameObject newPlatform = Instantiate(platform, lastPos, Quaternion.identity);
newPlatform.transform.localScale = new Vector3(randomScaleW, newPlatform.transform.localScale.y, newPlatform.transform.localScale.z);
currentPos = lastPos;
}
}
}
Images
Answer by $$anonymous$$ · Oct 17, 2017 at 12:20 AM
I figured it out, it turns out I needed to add a rigidbody to one gameobject.
Your answer
Follow this Question
Related Questions
How do I set an object's velocity to the velocity of an object that collided with it? 0 Answers
How to destroy a gameobject's clone when colliding with the ground? 1 Answer
OnTriggerEnter2D being called by GameObject without the trigger 1 Answer
Trigger Triggers Without Collision 1 Answer
Help with bombs destroying colliders in different position from gameobject 0 Answers