Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by rd42 · May 03, 2010 at 12:46 PM · physicsvelocityangleprojectile

Projectile trajectory velocity and angle instead of force with a rigidbody

I'm currently using myRigidBody.AddForce(x,y,z) to move an object. I'm guessing that the three values in AddForce are in Newtons(kg m/s^2) I'm not sure. I've been asked to change the program to launch the projectile with and angle and initial velocity. I have not yet found anything in the scripting documentation that specifically mentions using angle and velocity and I don't know a way to work backwards from initial velocity and angle to get three vectors of force.

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

2 Replies

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

Answer by duck · May 03, 2010 at 01:59 PM

The X,Y,Z values represent a Vector, whose direction is the angle, and whose length is the velocity. However the AddForce function has another version which accepts a vector explicitly rather than having a parameter for each of the x,y & z values.

In addition, if you're applying a force which is the equivalent of a single push at an instant in time (rather than a push over a duration of time), you should use a special force mode called "Impulse".

You can apply a force in the direction of a particular gameobject by using that gameobject's transform.forward property. For example, to launch a projectile in the direction of the gameobject on which the script is placed, you could use something like this:

using UnityEngine;

public class FireTest : MonoBehaviour {

 public Transform bulletPrefab;
 public float shotPower = 80;

 void Start()
 {
     // wait 10 secs, then fire every 5 seconds
     InvokeRepeating("Fire", 10, 5);
 }

 void Fire () {

     // the starting conditions (the position and angle of the 'gun' object)
     Vector3 startPos = transform.position;
     Quaternion shotAngle = transform.rotation;

     // create the projectile object
     Transform bullet = (Transform)Instantiate(bulletPrefab, startPos, shotAngle);

     // apply the firing force
     bullet.rigidbody.AddForce(transform.forward * shotPower, ForceMode.Impulse);

 }

}

Comment
Add comment · Show 9 · 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 rd42 · May 05, 2010 at 01:19 PM 0
Share

For some reason it's not compiling, it says ';' expected. It's on this line -> Vector3 startPos = transform.position; and it has a semicolon?! I don't understand.

avatar image duck ♦♦ · May 05, 2010 at 07:51 PM 0
Share

Are you using Javascript or C#? (the example is c#). Also, it could be an error on one of the lines before the one that it's saying - did you miss a semicolon on the line immediately before that line?

avatar image rd42 · May 07, 2010 at 03:38 PM 0
Share

I tried attaching your exact code to a sphere gameobject with a rigidbody component. No extra script and still the ';' expected error. I also tried putting it inside function Update() . I'm not sure what I'm still doing wrong. thanks for your help.

avatar image duck ♦♦ · May 07, 2010 at 03:48 PM 0
Share

ok, you didn't answer whether you're using C# or JS (that's important). I updated the example to make it more complete - it's now a complete usable C# class.

avatar image rd42 · May 10, 2010 at 02:23 PM 0
Share

I've been using JS, maybe that's why? I think the file name was .js. I'll try it again as C#

Show more comments
avatar image
0

Answer by poncho · Dec 17, 2010 at 11:32 PM

well if you have the velocity that have its magnitud, and you have an angle you can transform that in a vector this means for trigonometry that velocity*sin(angle) will give you the velocity vector in the y axis and velocity*cos(angle) will give you the velocity vector in the x axis. i'm also using the force in unity to move the objects, but now i see that the velocity would be easier for the future use, so im changin to that =P

good luck

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Projectile not firing right - simple angle and velocity problem? 2 Answers

Projectile motion of an arrow after applying force 3 Answers

trajectory motion predictor 1 Answer

Projectile isn't reaching my target. 1 Answer

Increase velocity without changing trajectory 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