gameObject change of direction on collision with other gameObject

Hi

Collision detection is working, the ball is a rigidbody and it bounces up and down nicely on the players head and eventually comes to rest under gravity and drag.

The player can be made to jump on the Y axis when the right moue button is pressed and again, my collision script knows that the player and ball have collided (as well as if the ball hits the ground) as the debug log is updating fine.

However, what I thought would result from this was that, when I jump and the ball hits the player, physics tells the ball to change direction and go back up. That sort of works, but then I realized the ball bounces just the same and comes to rest if you jump and hit it and also if you don’t). After messing with gravity settings, drag etc…no joy. So, I updated my collision debug script to do this:

// May want to set initial direction or something.
var direction = Vector3(0, 0, 0);

// Move it along its direction.
function Update () 
{
     transform.Translate(direction * Time.deltaTime); 
}

// If we hit the player invert y direction. Has debug collision messages for surface hit.

function OnCollisionEnter(theCollision : Collision) {

	if(theCollision.gameObject.name == "thisistheplayerobject"){
	
		Debug.Log("The ball hit the player. You scored!");}
		
			else if (theCollision.gameObject.name == "iKeppyChar"){
		
				direction.y *= 10000;} // this should send the ball towards this position
	
					else if(theCollision.gameObject.name == "theground"){
	
						Debug.Log("The ball hit the ground. You lose!");}
		
}

Nothing happens though! The ball doesn’t go anywhere and does not seem to be moved upon hitting the player object :confused: Am I missing something simple or perhaps I’m just going about it totally wrong? Any advice would be awesome thanks in advance from a guy just picking this form of JS.

UPDATE

Here is the new script thanks for getting me this far :slight_smile:

// Might want to set the direction at some point.
var direction = Vector3(0, 1, 0);

// Move it along its direction.
function Update () 
{
     transform.Translate(direction * Time.deltaTime); 
}

// If we hit the player, invert y direction on the object attached to this script. Has debug collision messages for surface hit.

function OnCollisionEnter(theCollision : Collision) {

	if(theCollision.gameObject.name == "soccer ball_Game"){
	
		direction.y *= 10; 
		
		Debug.Log("The ball hit the player. You scored!");}
			
			else if(theCollision.gameObject.name == "Emirates_Stadium_2"){
	
			Debug.Log("The ball hit the ground. You lose!");}
		
}

Problem 1) The success debug text for the ball hitting the player is not working - even though the ball is successfully being launched off in the y axis as I had intended.

Problem 2) I now have a ball bouncing up high into the air and it doesn’t seem to matter which values I assign.

There is also another problem I just thought of, which is that I only want this to happen if the user has pressed a button - not just under natural circumstances but hopefully I can solve that later.

Thank you for looking and any advice.

You may want to clarify this: "However, what I thought would result from this was that, when I jump and the ball hits the player, physics tells the ball to change direction and go back up. That sort of works, but then I realized the ball bounces just the same and comes to rest if you jump and hit it and also if you don’t). "

I am not sure what you are trying to do and where the script is failing. It would be easier to answer this.

Anyway, I think your problem is probably that you are multiplying direction.y * 10000, but direction.y is 0. 10000 * 0 = 0.