Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ferapellido · Apr 10, 2012 at 11:11 PM ·

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ferapellido · Apr 11, 2012 at 10:31 AM 0
Share

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)

avatar image aldonaletto · Apr 11, 2012 at 12:28 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges