C#C
C#3y 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);
}
}
}
"'
Was this page helpful?