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 biob988 · May 31, 2013 at 01:26 PM · javascriptvelocity

Adding force to a projectile? (SOLVED)

Hi, I am relearning how to Unity.. I left it for about two years, and I now have the motivation once more to get back into it! Happy days. Anyway. It's been a while, as you can see... I have forgotten many things.. One of which I can't seem to find an answer that works.

That is my reason for coming to you today. How do you add force to a projectile, so that it moves towards the (AI Controlled, in my case) player when it's fired from the turret.

Irony is I can remember how to make a complex AI character which decides its movements based on raycasting.. But I can't for the life of me crack the code to make the projectile move towards the player(john)... The Aim Target (at) its positioned when John is is range, and visible. Also positioned by raycasting etc.

This is what I have so far (ORIGINAL CODE).

 function Fire()
 {
     if(canFire) 
     {
     
         canFire = false; 
         
             yield WaitForSeconds(Random.Range(0,2)); // random firing time 
         
         var proj : Transform;
         
         proj = Instantiate(pre, spawn.transform.position, transform.rotation);
         proj.rigidbody.AddForce((at.transform.position - transform.position) * Time.deltaTime * 500); 
             // ^ el diablo line 
         
             yield WaitForSeconds(5); 
         
         canFire = true; 
     }
 }

WORKING CODE: function Fire() { if(canFire) {

         canFire = false; 
         
             yield WaitForSeconds(Random.Range(0,2)); 
         
         var proj : Transform;
         
         proj = Instantiate(pre, spawn.transform.position, transform.rotation);
         proj.transform.LookAt(at.transform); 
         proj.rigidbody.AddRelativeForce(Vector3.forward * 500);
         
             yield WaitForSeconds(5); 
         
         canFire = true; 
     }
 }

Thanks for you help -f

Comment
Add comment · Show 1
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 KiraSensei · May 31, 2013 at 01:44 PM 0
Share

Are you sure you have a rigidbody attached to your projectile ?

Use Debug.Log(...) to know the value of "at.transform.position - transform.position" (maybe it's (0,0,0) ?)

1 Reply

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

Answer by Eric5h5 · May 31, 2013 at 01:42 PM

For simplicity I would use Transform.LookAt followed by Rigidbody.AddRelativeForce (Vector3.forward). You should not use Time.deltaTime when adding the force. Also, using integers in Random.Range(0, 2) will pick either 0 or 1, which is unlikely to be what you want.

Comment
Add comment · Show 13 · 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 biob988 · May 31, 2013 at 01:56 PM 0
Share

Hey! I added this code: proj.transform.LookAt(at.transform); proj.rigidbody.AddRelativeForce(Vector3.forward);

after the projectile was instantiated.. But I'm still having the same problem: it just falls to the ground. Any suggestions?

avatar image KiraSensei · May 31, 2013 at 02:00 PM 0
Share

You never told that it falls to the ground :). Do you have the gravity check box ticked in the inspector for your projectile ?

avatar image Eric5h5 · May 31, 2013 at 02:04 PM 0
Share

Add more force.

avatar image biob988 · May 31, 2013 at 02:05 PM 0
Share

$$anonymous$$y bad. Forgot to mention that... Anyway, yea, gravity is checked.

avatar image KiraSensei · May 31, 2013 at 02:08 PM 0
Share

So uncheck it if you want it to go forward only.

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

15 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 avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Errors with script using Javascript. 2 Answers

Script Not Working At All.... :) (SOLVED) 1 Answer

The name 'List' does not denote a valid type ('not found')? 1 Answer

How to call a function from another script? 1 Answer


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