Okay, I’ve modified how I’m going about this problem, and I need a second (or many) pair of eyes to help me figure out what’s going wrong here. The way I decided to handle it was to have two separate imported bodies…one with the actual geometry being rendered, and a “ghost” mesh with the Rigidbody and MeshCollider attached. I’ve created a script called GhostObjectManager that is very simple:
using UnityEngine;
using System.Collections;
public class GhostObjectManager : MonoBehaviour {
public bool triggerHandled = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter() {
if (!triggerHandled) {
triggerHandled = true;
}
}
}
All I’m interested in here is determining when the MeshCollider enters one of the level geometry triggers. This script is attached to the “Test2Ghost” object.
My other main script is attached to the rendered geometry “Test2” that I’ve imported. The ghost object is the same geometry. Now, in order to detect collisions and not allow movement of the main geometry unless there will be no resulting collision I’ve attached the following ObjectMovementManager script to the main “Test2” object:
using UnityEngine;
using System.Collections;
public class ObjectMovementManager : MonoBehaviour {
public string translationAxis = "";
public bool isTranslating = false;
private float translationSpeed = 40.0f;
private GameObject importedObject;
private GameObject importedGhostObject;
private GhostObjectManager importedGhostObjectManager;
//
// Used for initialization
//
void Start () {
importedObject = GameObject.Find("Test2");
importedGhostObject = GameObject.Find("Test2Ghost");
importedGhostObjectManager = importedGhostObject.GetComponent(typeof(GhostObjectManager)) as GhostObjectManager;
}
//
// Used to reset the ghost object position and rotation
//
public void ResetGhostObjectTransform() {
importedGhostObject.transform.position = new Vector3(importedObject.transform.position.x,
importedObject.transform.position.y, importedObject.transform.position.z);
importedGhostObjectManager.triggerHandled = false;
}
//
// Check for ghost object collision
//
bool CheckForGhostObjectCollision(Vector3 translation) {
importedGhostObject.transform.Translate(translation);
if (importedGhostObjectManager.triggerHandled) {
return true;
}
else {
return false;
}
}
//
// Used for smoothing the translation value
//
private float InterpolatedTranslation {
get {
float interpolatedValue = Mathf.SmoothStep(0f, Mathf.Clamp((Time.deltaTime * translationSpeed * Input.GetAxis("Mouse Y")),
-5.0f, 5.0f), 0.2f);
return interpolatedValue;
}
}
//
// Apply translation every fixed update
//
void FixedUpdate () {
Vector3 objectTranslation = Vector3.zero;
bool willCollide = false;
if (isTranslating Mathf.Abs(InterpolatedTranslation) > 0.0f) {
switch (translationAxis) {
case "X" :
objectTranslation.x = InterpolatedTranslation;
willCollide = CheckForGhostObjectCollision(objectTranslation);
if (!willCollide) {
transform.Translate(-InterpolatedTranslation, 0f, 0f);
importedObject.transform.Translate(InterpolatedTranslation, 0f, 0f);
break;
}
else {
ResetGhostObjectTransform();
break;
}
break;
case "Y" :
objectTranslation.y = InterpolatedTranslation;
willCollide = CheckForGhostObjectCollision(objectTranslation);
if (!willCollide) {
transform.Translate(0, InterpolatedTranslation, 0);
importedObject.transform.Translate(0, InterpolatedTranslation, 0);
break;
}
else {
ResetGhostObjectTransform();
break;
}
case "Z" :
objectTranslation.z = -InterpolatedTranslation;
willCollide = CheckForGhostObjectCollision(objectTranslation);
if (!willCollide) {
transform.Translate(0, 0, InterpolatedTranslation);
importedObject.transform.Translate(0, 0, -InterpolatedTranslation);
break;
}
else {
ResetGhostObjectTransform();
break;
}
default :
break;
}
}
}
}
This doesn’t seem to be working…every time I call ResetGhostObjectTransform() it seems to screw up the MeshCollider trigger. That is, I’m getting inconsistent OnTriggerEnter() “triggerHandled” values which results in being able to translate the geometry right through the scene geometry. I feel like I’m just missing something stupid here. Can someone take a look at this code and let me know if you see anything wrong with it? There is one other script which just sets isTranslating to “true” when the user drags the mouse after selecting an axis object, but I don’t think the problem is there (pretty simple script).
Thanks in advance for any help! I’m driving myself crazy looking at this code.
Cheers,
Josiah