- Home /
AddForce is not working
Can anyone help me fix the bug? I put a script for my player tank and it has no problem shooting, but when I try to put similar another script to other tank as the "AI" is not working?
This is the code for the player tank:
var prefabBullet : Rigidbody;
var shootForce : float;
var shootPosition : Transform;
var fireRate = 1.0;
private var nextFire = 0.0;
function Update () {
if(Input.GetKeyDown("space") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
var instanceBullet = Instantiate(prefabBullet, transform.position,shootPosition.rotation);
instanceBullet.rigidbody.AddForce(shootPosition.forward * shootForce);
}
}
----------
This one is for the AI tank:
var prefabBulletAI : Rigidbody;
var shootForceAI : float;
var shootPositionAI : Transform;
var fireRate = 1.0;
private var nextFire = 0.0;
function Update () {
if (Input.GetKey("up") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
var instanceBulletAI = Instantiate(prefabBulletAI, transform.position,shootPositionAI.rotation);
instanceBulletAI.rigidbody.AddForce(transform.forward * shootForceAI);
}
}
The problem is why the AI bullet is not moving forward, when there is no error. The bullet simply doesn't have any force that move it forward eventhough the script for player is working properly. Please help me fix this bug. Thank you
I'd add in a Debug.Log to that part of the function to see if it is every being called correctly.
Also, make sure the object isn't set as kinematic or something similar.
Verify the value of 'shootForceAI' in the Inspector. It needs to be fairly substantial: 350 - 2000. Verify that your projectiles are not colliding with part of your AI tank.
Answer by akguldeniz · May 16, 2014 at 03:24 PM
this is just an idea but you can add this force inside of every bullets. this scripts crates bullets and try to force them.
crete bullets and put their awakefunction; function Awake () {
rigidbody.AddForce (transform.forward * 500);
}
edit code how do you want.
Your answer
Follow this Question
Related Questions
Rapid fire Help 1 Answer
sending message to bullet (javascript) 1 Answer
help with bullet shooting code? - javascript 1 Answer
Unity3D Ai wont shoot 1 Answer