x


On Hit go up?

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)

more ▼

asked Jul 22 '11 at 01:09 AM

Babilinski gravatar image

Babilinski
84 40 42 43

actually you can, use the code above.

Jul 22 '11 at 01:35 AM fireDude67

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?

Jul 22 '11 at 01:38 AM aldonaletto

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

Jul 22 '11 at 04:25 AM Babilinski

sorry i put my comment in the answer section.

Jul 22 '11 at 05:34 AM Jarsh94

This code would work only if wall was a GameObject variable to which you had previously dragged the wall object.

Jul 22 '11 at 04:41 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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:

void OnCollisionEnter(Collision collision){
  if (collision.gameObject.name == "wall"){
  // do whatever you want
  }
}

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.

more ▼

answered Jul 22 '11 at 06:49 AM

aldonaletto gravatar image

aldonaletto
41.4k 16 42 197

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)
10|3000 characters needed characters left

What is the error. Also could you post entire script?

more ▼

answered Jul 22 '11 at 05:30 AM

Jarsh94 gravatar image

Jarsh94
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x193
x38

asked: Jul 22 '11 at 01:09 AM

Seen: 562 times

Last Updated: Jul 26 '11 at 04:31 AM