Applying force to an object. (Unity)

Started by
0 comments, last by mamanybono 9 years, 6 months ago

Hi every body. I have a problem about applying force to an object. My scene is this. There is two cube on the scene and they have distance

between them. At the middele o f these 2 cuve there is a sphere. When the game start as soon as sphere hit the plane the plane applying just

for 1 time a force to right. Then sphere hits the right cube and the cube applyes force to left, left cube applyes force to right, it goes in

this way.

When player push the uparrow, I tried the eliminate all the forces on sphere and try to give force on forward direction. This codes are

working good but sometimes when player shoots the ball it doesnt eliminates all power, sphere goes to forward and right or forward and left.

What is the reason do you think.

Here is the video link maybe it helps to you for easier understanding. First shoot is okey for me but second one is fail. By the way for both shoots it says "SHOOT" at the console so it proves my codes in the uparrow case was worked.

" title="External link">


Here is the code.



public var first: int =1;
public var firstshoot: int =1;
function Start () {

}


function Update () {
if(Input.GetKeyDown(KeyCode.UpArrow)) {
firstshoot=0;
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;


rigidbody.AddForce(Vector3.forward * 3000);
Debug.Log ("Shoot");
}




}


function OnCollisionEnter (thecollision : Collision){

if (thecollision.gameObject.name == "FinishLine"){
Application.LoadLevel (0);
}

if (thecollision.gameObject.name == "Plane" && first == 1){
Debug.Log ("Floor Hit");
rigidbody.AddForce(Vector3.right * 500);
first=0;
}


if (thecollision.gameObject.name == "Cube1" && firstshoot == 1){
Debug.Log ("Right Wall Hit");
rigidbody.AddForce(Vector3.right * -3000);
}

if (thecollision.gameObject.name == "Cube2" && firstshoot == 1){
Debug.Log ("Left Wall Hit");
rigidbody.AddForce(Vector3.right * 3000);
}

}

Advertisement

I am suspecting about this, do you think, is it possible?

When my problem occurs, unity consol shows "Wright wall hit" then "Shoot" in this case, first wall applies force to ball on left direction, then uparrowe codes eliminate all forces and gives a forward force. But ball is moving to left side.

I think unity first reading codes for collision of right cube and shows the "Right wall hit", then it applies the force to left direction.

If ? push the uparrow button, while collision of right cube is occuring, system gives priority to my uparrow button and it is freezing the collision of right cube just after console writes "Right wall hit", then uparrow codes works, they eliminate all forces and gives just forward direction force, after uparrow codes finish, right cube collision continue and it applies to force on left direction.

This topic is closed to new replies.

Advertisement