C
C#•14mo ago
MRW419

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); } } } "'
7 Replies
Hazel 🌊💃
Use ``` to create a code block, not '''.
MRW419
MRW419OP•14mo ago
Oh
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);
}
}
}
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);
}
}
}
Hazel 🌊💃
OnCollisionEnter is a Unity specific method isn't it?
OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider. In contrast to OnTriggerEnter, OnCollisionEnter is passed the Collision class and not a Collider. The Collision 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.
From the documentation; does at least one of the entities in question have a non-kinematic rigidbody attached?
Hazel 🌊💃
Additionally, there is a $unity server that might be able to help with more specific questions like this one.
MRW419
MRW419OP•14mo ago
Got it, I think I got it tho. I figured it out
Want results from more Discord servers?
Add your server