Why is it not calling the OnCollisionEnter method?
"'cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class Player : MonoBehaviour
{
// TODO: Reference to Rigidbody2D component should have class scope. public Rigidbody2D rigidbody2d; // TODO: A float variable to control how high to jump / how much upwards public float Jump = 10.0f; // force to add. public float force = 5.0f; // Start is called before the first frame update void Start() {
// TODO: Use GetComponent to get a reference to attached Rigidbody2D rigidbody2d = gameObject.GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update() { // TODO: On the frame the player presses down the space bar, add an instant upwards if(Input.GetKeyDown(KeyCode.Space)) { OnCollisionEnter(Collision, other);
} // force to the rigidbody. } void OnCollisionEnter(Collision other) { if(other.gameObject.CompareTag("Ground")) { Vector3 jump = transform.up; GetComponent<Rigidbody2D>().AddForce(jump * force, ForceMode2D.Impulse); } } } "'
// TODO: Reference to Rigidbody2D component should have class scope. public Rigidbody2D rigidbody2d; // TODO: A float variable to control how high to jump / how much upwards public float Jump = 10.0f; // force to add. public float force = 5.0f; // Start is called before the first frame update void Start() {
// TODO: Use GetComponent to get a reference to attached Rigidbody2D rigidbody2d = gameObject.GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update() { // TODO: On the frame the player presses down the space bar, add an instant upwards if(Input.GetKeyDown(KeyCode.Space)) { OnCollisionEnter(Collision, other);
} // force to the rigidbody. } void OnCollisionEnter(Collision other) { if(other.gameObject.CompareTag("Ground")) { Vector3 jump = transform.up; GetComponent<Rigidbody2D>().AddForce(jump * force, ForceMode2D.Impulse); } } } "'
7 Replies
Use ``` to create a code block, not '''.
Oh
OnCollisionEnter
is a Unity specific method isn't it?
From the documentation; does at least one of the entities in question have a non-kinematic rigidbody attached?OnCollisionEnter
is called when this collider/rigidbody has begun touching another rigidbody/collider. In contrast toOnTriggerEnter
,OnCollisionEnter
is passed theCollision
class and not aCollider
. TheCollision
class contains information, for example, about contact points and impact velocity. Notes: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached. Collision events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.
Additionally, there is a $unity server that might be able to help with more specific questions like this one.
Got it, I think I got it tho. I figured it out