- Home /
Can i make istrigger true just for spesific object ?
I want to make istrigger is true just for my ball, so my ball can go through line and my player can not pass it, it is like net in tennis. This is the screenshot 
Answer by matmel · May 17, 2015 at 07:18 PM
Hey there! Are you looking for something like this:
GameObject.Find ("Ball").GetComponent<Collider> ().isTrigger = true;
more or less, but it will make isTrigger = true for all objects. I just pull y axis of net, so the ball can pass through the net and player can not.
I'm a little unsure of what you want here, but if you want to make a tennis net, then the ball and the player should not be able to pass through it. Are you making a 2D topdown tennis game? In that case I would use the code i gave you here for a while ago. The code will only make the ball collider a trigger. You will need the ball collider to collide with the bat again on the other side, so heres what im thinking:
float dist = Vector3.Distance(net.transform.position, ball.transform.position);
if(dist < 5f) //Dunno if 5 fits
{
GameObject.Find ("Ball").GetComponent<Collider> ().isTrigger = true;
}
if(dist > 5f) //Dunno if 5 fits
{
GameObject.Find ("Ball").GetComponent<Collider> ().isTrigger = false;
}
Sorry for long delayed answers
I have edited the question, that's all I do to solve my problem, But I'm still thinking. Is there anyway to make the ball pass wall, but the others can't do it.
I think the code i posted earlier will solve your problem. I've also found another possibility, take a look at ignore collision: http://docs.unity3d.com/ScriptReference/Physics.IgnoreCollision.html
I think it will work, thanks but I prefer without script.
Your answer