Hi people,
I am currently working on a project which requires me to place an object (a peg) at the transform of another (a hole) if they touch. At the moment I am destroying the peg and instantiating a new one where it needs to be (depending on which hole it is touching). This is causing me trouble… I need to be update the transform the peg is trying to access.
I have a square face (like a die) with 9 slots in it. If I enter the peg in to a slot in the top left I need the peg to recognise that slot. To move the peg the mouse button needs to be down and if the mouse button is up, then it shall snap to the holes transform. If I move out of the trigger I need the peg to realise this and see the transform as empty!
Here’s the code I am using at the moment!
using UnityEngine;
using System.Collections;
//[RequireComponent(typeof(BoxCollider))]
[RequireComponent(typeof(Rigidbody))]
public class InstantiateAndDelete : MonoBehaviour {
public GameObject peg;
public bool snapPeg = false;
public bool isHorizontal = false;
public bool isVertical = false;
public Transform holeTransform;
public Transform pegTransform;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerStay(Collider other)
{
if(other.name == "VerticalSlot" snapPeg == false)
{
snapPeg = true;
isVertical = true;
pegTransform.transform.position = holeTransform.transform.position;
}
else if(other.name == "HorizontalSlot" snapPeg == false)
{
snapPeg = true;
isHorizontal = true;
pegTransform.transform.position = holeTransform.transform.position;
}
}
public void OnMouseUp()
{
if(snapPeg == true isVertical == true)
{
Destroy(gameObject);
Instantiate(peg,holeTransform.position,Quaternion.Euler(0,270,270));
}
else if(snapPeg == true isHorizontal == true)
{
//Destroy(gameObject);
Instantiate(peg,holeTransform.position,Quaternion.Euler(90,0,0));
}
}
void OnTriggerExit(Collider other)
{
snapPeg = false;
isHorizontal = false;
isVertical = false;
}
}
Thanks,
Cas