- Home /
Question by
Galaga · May 30, 2020 at 12:49 PM ·
cubeplanec# tutorial
Cube fall to the plane and gets Velocity
Hi I want to write simple C# Script. for Cube and a Plane The Cube has [Rigidbody] and it Fall to the Plane, When Cube hit the Plane the Cube get [Velocity] and go Up again!
6543.jpg
(8.9 kB)
Comment
Best Answer
Answer by RYNdesigns · May 30, 2020 at 01:08 PM
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Test : MonoBehaviour
{
//Set this in the editor, upwards force
public float force;
//Detects when the Cube collides with something (you can use tags to filter everything other than the floor)
private void OnCollisionEnter(Collision collision)
{
//Gets the Rigidbody component of the cube and applies a force in the Y direction
GetComponent<Rigidbody>().AddForce(0, force, 0);
}
}
Here's how it looks: https://gyazo.com/2ff4ffa5fd982eb03bd80786020738df
You can also freeze the rotation constraints so it doesn't start rotating and falling off the plane. https://gyazo.com/a1b37c2c6d991657d668c5a6d50926ba