Reputation: 1047
Following this tutorial, I can position an object in space.
How can I replace the object with another one in the same position? I need to have a public function and assign it to a button, so when I press the button, the "Kitty" model being replaced with another model.
Here is the main script in the tutorial:
using UnityEngine;
using System.Collections;
public class KittyUIController : MonoBehaviour
{
public GameObject m_kitten;
private TangoPointCloud m_pointCloud;
void Start()
{
m_pointCloud = FindObjectOfType<TangoPointCloud>();
}
void Update ()
{
if (Input.touchCount == 1)
{
// Trigger place kitten function when single touch ended.
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Ended)
{
PlaceKitten(t.position);
}
}
}
void PlaceKitten(Vector2 touchPosition)
{
// Find the plane.
Camera cam = Camera.main;
Vector3 planeCenter;
Plane plane;
if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane))
{
Debug.Log("cannot find plane.");
return;
}
// Place kitten on the surface, and make it always face the camera.
if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f)
{
Vector3 up = plane.normal;
Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized;
Vector3 forward = Vector3.Cross(right, plane.normal).normalized;
Instantiate(m_kitten, planeCenter, Quaternion.LookRotation(forward, up));
}
else
{
Debug.Log("surface is too steep for kitten to stand on.");
}
}
}
Upvotes: 4
Views: 498
Reputation: 1815
Added one method ReplaceKitten
<= Call this on your button click.
Added a public
yourOtherModel GameObject
for the desired model.
Made the Instantiated kitten global (into a variable) from the PlaceKitten
method.
using UnityEngine;
using System.Collections;
public class KittyUIController : MonoBehaviour
{
public GameObject m_kitten;
public GameObject yourOtherModel; // could be prefab
private TangoPointCloud m_pointCloud;
private GameObject createdKitten;
void Start()
{
m_pointCloud = FindObjectOfType<TangoPointCloud>();
}
void Update ()
{
if (Input.touchCount == 1)
{
// Trigger place kitten function when single touch ended.
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Ended)
{
PlaceKitten(t.position);
}
}
}
//This method will disable the kitten and instantiate or place a GameObject on the same spot.
public void ReplaceKitten()
{
GameObject modelToReplace;
//Do this only if you will pass a Prefab to this Script
modelToReplace = Instantiate(yourOtherModel);
//Set your new object at the same coordinates and reset position
modelToReplace.transform.parent = m_kitten.transform;
modelToReplace.transform.position = new Vector3(0,0,0);
//Disable kitten
m_kitten.SetActive(false);
}
void PlaceKitten(Vector2 touchPosition)
{
// Find the plane.
Camera cam = Camera.main;
Vector3 planeCenter;
Plane plane;
if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane))
{
Debug.Log("cannot find plane.");
return;
}
// Place kitten on the surface, and make it always face the camera.
if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f)
{
Vector3 up = plane.normal;
Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized;
Vector3 forward = Vector3.Cross(right, plane.normal).normalized;
createdKitten = Instantiate(m_kitten, planeCenter, Quaternion.LookRotation(forward, up));
}
else
{
Debug.Log("surface is too steep for kitten to stand on.");
}
}
}
Make sure there are no typos I'm not on an editor.
Upvotes: 2
Reputation: 273
Save the reference of the object that you have instantiated and whenever you want to add a new object check that whether the instance is null or not,
if not null than that means object is in the space. destroy it and create a new instance of the gameobject that you want and save its reference.
if null than that means space is empty create a new instance and save its reference
public GameObject OtherModel; // new model that you want to replace with kitty
GameObject myRef; // Reference for the object that is already in scene
void PlaceKitten(Vector2 touchPosition)
{
// Find the plane.
Camera cam = Camera.main;
Vector3 planeCenter;
Plane plane;
if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane))
{
Debug.Log("cannot find plane.");
return;
}
// Place kitten on the surface, and make it always face the camera.
if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f)
{
Vector3 up = plane.normal;
Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized;
Vector3 forward = Vector3.Cross(right, plane.normal).normalized;
// Check whether we have already placed something or not
if (myRef != null)
{
Transform temp = myRef.transform;
Destroy(myRef);
myRef = Instantiate(OtherModel, temp.position, temp.rotation);
}
else
{
myRef = Instantiate(m_kitten, planeCenter, Quaternion.LookRotation(forward, up));
}
}
else
{
Debug.Log("surface is too steep for kitten to stand on.");
}
}
Upvotes: 0