Hi i’m pretty new to programming and was following a tutorial on making 2D rpg games. I got to
void OnCollisionEnter2D(Collision2D other) {}, but the problem is my 2 objects (a player and a slime) are colliding but I don’t think its calling this method when it collides. Any ideas?
void OnCollisionEnter2D(Collision2D other)
{
transform.gameObject.SetActive(false);
if (other.gameObject.name == "Player")
{
other.gameObject.SetActive(false); //is it active or not? set it to false
reloading = true;
thePlayer = other.gameObject;
}
}
Looks like you are setting the gameObject to false when it collides with anything. Then, after it detects the player you are doing again. Delete the first time you do it and it should work. You have Rigidbody2D and Collider2Ds on both object so that is good.
Thank you for the quick reply! Even after deleting the first line, nothing happens when the player collides with the slime.
Maybe it has something to do with the rest of the code?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SlimeController : MonoBehaviour
{
public float moveSpeed;
private Rigidbody2D myRigidbody;
private bool moving;
public float timeBetweenMove;
private float timeBetweenMoveCounter;
public float timeToMove;
private float timeToMoveCounter; //the time when set will be 0, we need a way to reset the time
private Vector3 moveDirection;
public float waitToReload;
private bool reloading;
private GameObject thePlayer;
// Start is called before the first frame update
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
//timeBetweenMoveCounter = timeBetweenMove;
//timeToMoveCounter = timeToMove;
timeBetweenMoveCounter = Random.Range(timeBetweenMove *0.75f, timeBetweenMove * 1.25f);
timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeToMove * 1.25f);
}
// Update is called once per frame
void Update()
{
if (moving)
{
timeToMoveCounter -= Time.deltaTime;
myRigidbody.velocity = moveDirection;
if (timeToMoveCounter < 0f)
{
moving = false;
//timeToMoveCounter = timeToMove;
timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeToMove * 1.25f);
}
}
else
{
timeBetweenMoveCounter -= Time.deltaTime;
myRigidbody.velocity = Vector2.zero;
if (timeBetweenMoveCounter < 0f)//cuz what if it's not zero?
{
moving = true;
//timeBetweenMoveCounter = timeToMove;
timeBetweenMoveCounter = Random.Range(timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
moveDirection = new Vector3(Random.Range(-1f, 1f) * moveSpeed, Random.Range(-1f, 1f) * moveSpeed, 0f);
} //enemy comes online, it's not moving. when counter reaches 0, it starts moving again in a random direction. Repeat
if (reloading)
{
waitToReload -= Time.deltaTime;
if (waitToReload < 0)
{
Application.LoadLevel(Application.loadedLevel);//loaded level is the level that we is loaded right now
thePlayer.SetActive(true);
}
}
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.name == "Player")
{
other.gameObject.SetActive(false); //is it active or not? set it to false
reloading = true;
thePlayer = other.gameObject;
}
}
}
}
I also used a console message in void OnCollisionEnter2D(Collision2D other) as the first line, but it never prints. I think somehow collision isn’t being sensed.
OnTrigger works fine ,but collision doesnt work
Assets\Scripts\SlimeController.cs(69,14): warning CS8321: The local function ‘OnCollisionEnter2D’ is declared but never used. I also get this warning
It looks like you are calling OnCollisionEnter2D inside of Update. Move that collision function between lines 80 and 81.