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);
}
}