- Home /
Are there any errors in this bullet script?
Im learning to script in Unity and I decided to make a simple Bullet script. Sorry for the primitive code, but is the best I can do legit. I havent added code for any special efects. Please tell me any errors you find or if it is the correct way to code bullet movements. If everything is perfect, is there any way I can improve it?
1: var bulletspeed : float = 10.0;
2: var bulletshoot : float = 1.0;
3: var bullettimer : float = 2.0;
4:
5: function Update (){
6: if(Input.GetMouseButtonDown(0)){
7: if(bulletshoot > 0){
8: transform.Translate(Vector3(0,0,bulletspeed) * Time.deltaTime);
9: bulletshoot += -1;
10 bullettimer -= Time.deltaTime;
11: if(bullettimer <= 0){
12: Destroy(gameObject);
13: bulletshoot += 1;
14: }
15: }
16: }
17: }
18:
19: function OnCollisionEnter(theCollision : Collision){
20: if(theCollision.gameObject.name == "Enemy"){
21: Destroy(gameObject.Find("Enemy"));
22: Destroy(gameObject);
23: }
24: }
I mixed a lot of basic commands, so there may be a lot of errors, I hope you can understand what the bullet is suposed to do. I have NOT tested it because I dont have time, Ill do that tomorrow
Answer by aldonaletto · Apr 11, 2012 at 12:32 AM
1- You should not move the bullet with Translate: this fools the collision detection system, and you may not hit anything. Furthermore, with this code the bullet will only move a little each time you press Fire1 (not a good way to kill an enemy, for sure - unless you want it to die of old age...)
2- You should place only the collision code in the projectile. The shooting code must be attached to the weapon, so you can control the ammunition, interval between shots, shooting etc.
3- In OnCollisionEnter, destroy the bullet anyway - no matter if the enemy was hit or not.
Usually things are done in a different way: you create the bullet, add a rigidbody and the collision script to it, and drag the bullet to the Project window to make it a prefab. In the shooting script (usually attached to the weapon) you instantiate the bullet prefab, set its rigidbody.velocity and forget it - the physics engine move the projectile and check for collisions automatically. You can also use Destroy(projectile.gameObject, time) to destroy the projectile automatically after time seconds, like this:
// Weapon script:
var bulletspeed : float = 10.0; var bulletshoot : int = 5; // how many bullets you have var bullettimer : float = 2.0; // self destruction time var bulletPrefab: Transform; // drag the bullet prefab here
function Update (){ if(Input.GetMouseButtonDown(0)){ if (bulletshoot > 0){ // if you have ammunition... // calculate a place at the weapon tip to instantiate the bullet: var shootPos = transform.position + transform.forward 0.9; var bullet: Transform = Instantiate(bulletPrefab, shootPos, transform.rotation); bullet.rigidbody.velocity = transform.forward bulletspeed; // accelerate the bullet bulletshoot += -1; // count the ammunition Destroy(bullet.gameObject, bullettimer); // make it die after bullettimer seconds } } }
// Projectile script:
function OnCollisionEnter(theCollision : Collision){ if(theCollision.gameObject.name == "Enemy"){ // if object hit is enemy... Destroy(theCollision.gameObject); // destroy it! } Destroy(gameObject); // destroy the bullet anyway }
Great, now how can I control the direction of the bullet? (1: Shooting the bollet in the right direction, in this moment it is flying through the pistol side) (2: Ai$$anonymous$$g at $$anonymous$$ouseX and $$anonymous$$ouseY)
The bullet is going in the weapon Z axis direction. To solve problems like flipped axes and to allow weapon ai$$anonymous$$g, usually we create an empty object (let's call it Weapon), child it to the camera, reset its Rotation and Position in the Inspector, attach the shooting script and child the weapon model to it - this way the Weapon will always point in the same direction as the camera, and the weapon model may be adjusted to the correct orientation. If you're using the First Person Controller, your weapon is ready: the F P Controller already has camera control by $$anonymous$$ouseX and $$anonymous$$ouseY.
Your answer
Follow this Question
Related Questions
Structs in C# Question 3 Answers
Convert Timer to HH:MM:SS 1 Answer
Can I change the color of the script editor? 1 Answer
Need delayed reaction after key press 1 Answer
Animation Controlled Script 0 Answers