Skip to content
james tichenor edited this page Jun 18, 2018 · 18 revisions

Tutorial on physics events in Unity.

One of the unique aspects of working in Unity is that it has a built in physics engine. Leaning to think through this engine will allow you to build more in Unity. Today we are just going to quickly go over the physics event system and how it connects with code we can integrate it with what have learned so far.

Outline:

Build initial elements the egg and the floor

to the scene add a sphere and a box (GameObject -> 3DObject ->Sphere , GameObject -> 3DObject ->Cube) move the sphere to be floating above the box but still in the camera view (I just eyeballed it clicking and dragging with the objects and using Move command on the interface)

Elongate the sphere to an egg shape

elongate the box to to a floor shape and rename them both. (I just eyeballed it clicking and dragging with the objects and using Move command on the interface)

Add a rigid body component to the egg shape

do this through add component. his play and your egg should now fall to the floor.

Add a platform so the egg rolls down

add a new gameObject of a box and position it as a platform in between the egg and the floor tilted so the egg will roll off the platform and land on the floor below and then roll of the whole floor.

Make a Collision Events Script on the Egg object

to this script we will add 3 methods:

void OnCollisionEnter(Collision collision){
	Debug.Log("colliion started");
}

void OnCollisionStay (Collision collision){
	Debug.Log("Stay occuring..");
}

void OnCollisionExit (Collision collision)
{
	Debug.Log("Exit called...");
}

Create a Clone Script

create this script on an empty game object that you can rename cloner. We will make this script with public variables for the GameObjects so that we can drag and drop what we want to clone. Lets start by just making sure we can get spacebar input by adding this to your new Clone Script

    void Update()
    {
        if (Input.GetKeyDown("space"))
        {
            Debug.Log("spacebar");
        }
    }

Create Public Game Objects for Object to Clone and Start

public class AddElements : MonoBehaviour {
	public GameObject startGo;
	public GameObject copyGo;

Create Instantiate

public class AddElements : MonoBehaviour {
	public GameObject startGo;
	public GameObject copyGo;

    void Update()
    {
        if (Input.GetKeyDown("space"))
        {
            Debug.Log("spacebar");
	    Vector3 startPos;
            Quaternion startRotation;

	    startPos = startGo.transform.position;
            startRotation = startGo.transform.rotation; 
	    Instantiate(copyGo,startPos,startRotation);
        }
    }

Move Instantiate to its own Method

	public void makeObject ()
	{
		//Debug.Log("space key was pressed");
		Vector3 startPos;
		Quaternion startRotation;

		startPos = startGo.transform.position;
		startRotation = startGo.transform.rotation; 
		Instantiate(copyGo,startPos,startRotation);
	}

Use Use Cloning Script from Egg

	public GameObject cloneScript;
	
	void OnCollisionExit (Collision collision)
	{
	//	Debug.Log("Exit called...");
	cloneScript.GetComponent<AddElements>().makeObject();
	
	}

Activate on exit of floor

     if(collide.gameObject.name == "floor")        
     {
         //Do Something 
     }

https://unity3d.com/learn/tutorials/topics/physics/detecting-collisions-oncollisionenter

https://unity3d.com/learn/tutorials/topics/physics/rigidbodies

https://unity3d.com/learn/tutorials/topics/scripting/getbutton-and-getkey

-end