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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by colly · Dec 02, 2012 at 12:13 PM · turretfirewont

My bullets wont slow down

When my bullet is fired from the turret all i can see if the flash of the bullet then it is gone. It hits the target perfectly.

i also try to add my own var called speed and that didnt do anything it just wont slow down. Here is my code.

var LookAtTarget:Transform; var damp = 1.0f; var bullitPrefab:Transform; var savedTime; private var range : float = 1000f; private var nextFire : float = 0.1f;

function Update ()

{ if(LookAtTarget) {

     var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position);
     var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);

if (distance <= range) { transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); }

var seconds : int = Time.time; var oddeven = (seconds & 9999); //shots per second if(oddeven) { Shoot(seconds); //goes to shoot funcation } } }

function Shoot(seconds) { var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position); if (distance <= range)

if(seconds!=savedTime && Time.time > nextFire) {
var bullit = Instantiate(bullitPrefab, transform.Find("Spawnpoint").transform.position ,Quaternion.identity);

     bullit.rigidbody.AddForce(transform.forward * 1000); //how fast bullet travels

savedTime=seconds; } }

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
1

Answer by fafase · Dec 02, 2012 at 01:46 PM

Aaaah, If Unity would get a penny every time someone using the Tornado Twins tutorial comes to ask a question, Unity pro would be free...

Well, there is no reason not to learn a "better" way:

 var LookAtTarget:Transform; 
 var _transform :Transform;
 var damp = 1.0f; 
 var bullitPrefab:Transform; 
 private var rangeSqr : float = 10000f; //The range is squared
 private var spawn:Transform;   //The spawn is cached
 var wait:float;
 var seconds:float;
 
 function Start(){
     _transform = GetComponent(Transform); //Cache the transform of the turret
     spawn = _transform.Find("Spawnpoint").transform;
     if(!LookAtTarget)LookAtTarget = GameObject.Find("Player").GetComponent(Transform);        
 }
 
 function Update (){         
     if ((LookAtTarget.position - _transform.position).sqrMagnitude <= rangeSqr){ 
        var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
         _transform.rotation = Quaternion.Slerp(_transform.rotation, rotate, Time.deltaTime * damp); 
        seconds += Time.deltaTime; 
        if(seconds > wait) { 
          Shoot(); //goes to shoot function 
          seconds = 0;
        }
     } 
 }
 function Shoot() { 
     var bullit = Instantiate(bullitPrefab, spawn.position ,Quaternion.identity);    
     bullit.rigidbody.AddForce(_transform.forward * 1000); 
 }

First it looks shorter. Also, the Find function is now done only once. Avoid a Find function in the Update when you can. Here you could. My shoot function has two lines only. The whole timer system is way simple now. No more modulus calculation (yes your & should be a % in (seconds & 9999)<- this is bitwise manipulation) and parameters being passed.

Also, avoid Distance function when you can. Distance uses sqr root which is to be left on the side. Use the squared magnitude instead which is way cheaper on resources.

Let us know if I made any mistakes there...

Comment
Add comment · Show 11 · 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 colly · Dec 02, 2012 at 07:37 PM 0
Share

Thanks for getting back to me but it says that line 15 Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform'

And also line 19 says '<=' cannot be used with a left hand side of type 'float' and a right hand side of type 'function(int):

avatar image fafase · Dec 02, 2012 at 07:52 PM 0
Share

I fixed the first mistake, the second range should be rangeSqr.

avatar image colly · Dec 02, 2012 at 08:19 PM 0
Share

hi thanks for the help so far, i have no error messages now but it the bullet still wont slow down, it appears for a second then disappears, i tried putting the AddForce to a low number and even high but still wont slow down

avatar image Loius · Dec 02, 2012 at 08:30 PM 0
Share

I swear that tutorial causes more questions than it answers. (plus - bullit? really? REALLY?)

Is there any code on the bullet itself? Is it hitting its source with low force and then destroying itself? Is it a solid object (non-trigger collider) being created inside another solid object?

avatar image fafase · Dec 02, 2012 at 08:37 PM 0
Share

Comment out the

 bullit.rigidbody.AddForce(_transform.forward * 1000);

If your "bullit" still moves, you have something somewhere else.

Show more comments

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to detect an object that comes in the "Crosshair" field of view? 1 Answer

Turret control Problem 4 Answers

Can I randomize LookRotation? 1 Answer

Fire in Unity3D - Fire particles 1 Answer

How do i change Automatic guns into Semi-Auto? 2 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