Hi …
I’m trying do the following : If I pressed Right Click and object 1 collision object 2, object 2 change its position I used this code but object 2 change its position even not collide object 1
var FirstHit : Transform;
var touch : boolean = false;
function OnCollisionEnter(collision : Collision)
{
if(FirstHit==null)
FirstHit = collision.transform;
}
function Update()
{
if(FirstHit != null Input.GetMouseButtonDown(1))
{
transform.position = FirstHit.position + Vector3.up;
touch = true;
}
}
Maybe their colliders are overlapping at the start of the game? Or maybe you set the firstHit variable to something before the start as well?
And also if it were me, I’d use GameObject, not Transform, as sometimes Unity gets confused as to which you’re trying to represent. So I’d try
var FirstHit : GameObject;
and
FirstHit = collision.gameObject;
And then of course you should look at the variable in the editor itself.
Also WoodyGoody, the FirstHit variable should be private like I showed you in the other thread.
Otherwise you, or some one using the script could add something to that variable in the inspector, as the script is now, you don’t want that, it would make the object that script is in follow what ever is assigned to it in the inspector.
Also, you could use the touch variable you added instead, probably better down the road any way if you want to have other collisions or the ability to drop the object your carrying. If you do that though, just get rid of the FirstHit==null and FirstHit!=null and replace them with !touch and touch, respectively.