|
I want to create a script where when a ball hits a wall it will move on the y axis. how would I do that? Im using C# so I cant do if (collision.gameObject == wall)
(comments are locked)
|
|
You should post the code! Anyway, supposing collision is a Collision generated on the OnCollisionEnter event, you could compare the collided object's name or tag: You must of course rename the object to "wall". You can also use gameObject.tag instead of gameObject.name - you must set the object tag to "wall" at the Inspector, in this case. I did nit have a code all I had was if (collision.gameObject == wall) i did not know about void OnCollisionEnter(Collision collision). thank you so much !
Jul 23 '11 at 12:31 AM
Babilinski
How would i add more objects?
Jul 24 '11 at 03:21 AM
Babilinski
Which objects? Walls? If you have several walls or other objects to which the ball will collide, it's better to compare gameObject.tag: click the button where the object tag is defined in the Inspector, then click Add tag... and add the Wall tag. It will be easier with the other objects: just click the tag button and select Wall. In the code, use:
if (collision.gameObject.tag == "Wall"){
Jul 24 '11 at 03:33 AM
aldonaletto
like lets say that when the ball hits the wall i want it to move up but when it hits a box i want it to get destroyed public gameobject collision = wall; void OnCollisionEnter(Collision collision){ if (collision.gameObject.name == ("wall")){ // my moving script } if (collision.gameObject.name == ("box")) { Destroy(gameObject); } } but when I do that my ball gets deleted right when the game starts.
Jul 24 '11 at 05:01 AM
Babilinski
It shouldn't - unless you had renamed the terrain as "box" by mistake. It's a more frequent error than you expect: since the terrain is big, any misplaced click may affect the terrain instead of what we're trying to change. Double clike any object named box in the hierarchy: the scene view will focus it.
Jul 24 '11 at 12:04 PM
aldonaletto
(comments are locked)
|
|
What is the error. Also could you post entire script?
(comments are locked)
|

actually you can, use the code above.
You must give more details. What are you trying to do? Make the ball "climb" the wall and go ahead? Or is this ball a projectile that must be deflected up when hitting the wall? Anyway, the main question is: at which velocity should it go up? The same velocity it hit the wall? And is this ball a rigidbody?
i want the ball to bounce on a plane and i want to in cress the hight of the balls bounce by if (collision.gameObject == wall). but i get an error when I use this code
sorry i put my comment in the answer section.
This code would work only if wall was a GameObject variable to which you had previously dragged the wall object.