third attempt to answer:
Well, like I meant on the comment: d'oh. Colliders will be overwritten by any script, and you already showed yours were doing just that. I should have noted that before.
So you may need to go with either OnCollisionStay or OnTriggerStay. The second is not set every frame, so you'd have to try it and see if it behaviors is accepted. I'll give an instance on it because it's simpler! :P
By the way, if you still haven't done so, add a rigidbody as a requirement for the collider to actually collide (see Static vs Rigidbody Colliders on the link).
Then you should add a variable to check if the turret is coliding with the tank and verify it using OnTriggerStay and OnTriggerExit.
It could be something like this:
private var lastTransform : Transform;
function OnTriggerStay (other : Collider) {
if (other.tag = "turret") {
transform.rotation = lastTransform;
}
}
// This goes on the Update function, right before the transform.rotation
lastTransform = transform.rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
That must be enough. But if for whatever reason it isn't, you'll also need to add a boolean and check for that var before actually rotating the turret. Something like this:
private var canRotate = true;
private var lastTransform : Transform;
function OnTriggerStay (other : Collider) {
OnTriggerExit();
if (other.tag = "turret") {
canRotate = false;
transform.rotation = lastTransform;
}
}
function OnTriggerExit () {canRotate = true;}
// This goes on the Update function, right before the transform.rotation
if (canRotate) {
lastTransform = transform.rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
}
Note that this won't work just with that alone because the turret will just stop once it collides and nothing will make it exit!
second attempt:
Ok, so C.Joint didn't work for you... That became clear by reading your code now. Unless you're making a 2D game, it's very unlikely C.Joint would be useful in your case. The code was also actually not needed there, but it helped to elucidate the question (which was almost enough in itself).
I think what you need there is adding colliders to the tank and the turret, where the turret goes through to avoid that from happening. That's the whole point of colliders. Learn more about them, it should be enough to make the turret stop going through.
first attempt:
Well, to lock rotation to 1 axis you can simply add a Configurable Joint to it, and lock the other 2 "Angular Motion".
As for the rest, like Michael said, we need more details to help you.
Anyway, since we're here, I'd like to add: always avoid using "Find" as much as possible because it's slow and resource hungry. It would probably not make much difference in this case, but I still rather give you the example of a better usage. It's not an optimal solution:
function Start () {
findPlayer = GameObject.FindWithTag("Player").transform;
if (target == null && findPlayer)
target = findPlayer;
}