-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This is moved to this page: https://github.com/Sensorycc/Mixed-Reality-Hardware-Toolkit/wiki/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.
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 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)
do this through add component. his play and your egg should now fall to the floor.
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.
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 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");
}
}
public class AddElements : MonoBehaviour {
public GameObject startGo;
public GameObject copyGo;
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);
}
}
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);
}
public GameObject cloneScript;
void OnCollisionExit (Collision collision)
{
// Debug.Log("Exit called...");
cloneScript.GetComponent<AddElements>().makeObject();
}
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