I'm doing a puzzle game where I have a sphere made up of multiple mesh colliders as slots, and the player is supposed to move puzzle pieces(also mesh colliders) into contact with the sphere where they will stick to the mesh collider 'slot' they collided with. However, my script behaves strangely. It adheres sometimes, and on other times it floats off into the game world by itself. How would I get it to work?
Here's the sphere the puzzle pieces are supposed to stick to, and one of the puzzle pieces itself:
The sphere is composed of several meshes each with their own mesh collider. The irregularly shaped object on the left is one of the puzzle pieces. When the player moves it towards the sphere it should stick to the first mesh collider on the sphere it comes into contact with.
And here's my script:
using UnityEngine;
using System.Collections;
public class StickyObject : MonoBehaviour {
DragToMove1 otherScript;
GameObject mainObj, jigsphere;
bool stuck;
public LayerMask layerMask;
// Use this for initialization
void Start ()
{
otherScript = this.GetComponent<DragToMove1>();
mainObj = GameObject.Find("GameObject");
stuck = false;
}
// Update is called once per frame
void Update ()
{
if (stuck)
{
}
}
void OnCollisionEnter(Collision c)
{
if (!stuck)
{
//By contact point
ContactPoint touched = c.contacts[0];
Debug.Log ("Collision with " + this.name);
touched.otherCollider.gameObject.transform.parent = this.transform;
Debug.Log ("Parented to " + touched.otherCollider);
stuck = true;
if (otherScript != null)
{
otherScript.Stuck();
otherScript.selected = false;
}
mainObj.SendMessage ("Wake");
}
}
bool getStuck()
{
return stuck;
}
}