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
1
Question by Karsnen_2 · Dec 02, 2011 at 03:27 AM · quaternionvelocityaddforceangleprojectile

Angle and Velocity in a Projectile

Hello,

I am trying to input angle and velocity with which the cannon should fire in a projectile fashion. The prototype is

  1. Side scroller ( meant that the view is 2D).

  2. The canon has to be placed on the left most corner and fire in a Projectile Fashion so that the bullet lands within the screen (no camera movement).

Game Objects :

I have placed the "First Person Controller" from the standard assets and placed the following script on the camera. My assumption was to shoot from the camera. (I hope I am not colliding within the "First Person Controller".

Code :

 var canonPrefab : Transform;
 var speed = 555.0f ;
 var angle = 90.0f;
 function Update()
 {
     var    elevationAngle = Vector3(0,0,angle);
     if(Input.GetButtonDown("Fire1"))
     {
         var canon =  Instantiate (canonPrefab, transform.position, Quaternion.identity);
         canon.rigidbody.AddForce( Quaternion.Euler(elevationAngle)*transform.forward * speed);
 
     }
 }

Code Explanation :

As far as the angle is concerned, I have received the I/P as a float then parsed it to a Vector 3 variable and used Quaternion.Euler function. I believe transform.forward is to move the Prefab along the z axis. I also think that I have rightly used ForceMode.Impulse.

Problem :

No matter how I alter the angle, the canon fires at the very same angle. I assume the angle at which it is fired (projected) is just 'zero'.

Request :

My humble request is could someone help me out. I have been working around and I think I am stuck. My main aim is to I/P the velocity and angle through GUI and then fire the canon with respect to the I/P given. My another issue is (not concern right now), I would like to I/P with real time velocity values but with the I/P I give in, it seems to be not in relation with the Velocity in real life.

Thank you for your patience.

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
3
Best Answer

Answer by aldonaletto · Dec 02, 2011 at 04:14 AM

The whole thing is somewhat confusing: why are you using a First Person Controller in a 2D side scroller game? The FPC shows the world from its own point of view, while in a side scroller game the world is showed from an external point of view.
I would place the cannon in the left side, and shoot to the right (Vector3.right); instead of use AddForce, I would set the rigidbody.velocity directly - AddForce depends on the projectile mass and the time during which the force is applied. With these settings, the script would be attached to the cannon:

var canonPrefab : Transform; var speed = 10.0f ; var angle = 45.0f;

function Update(){ if(Input.GetButtonDown("Fire1")) { var canon = Instantiate (canonPrefab, transform.position, Quaternion.identity); var shootDir = Quaternion.Euler(0, 0, angle) Vector3.right; canon.rigidbody.velocity = shootDir speed; } } NOTE: In this case, the camera would be aiming in the Z direction, thus the space showed in the screen would be the YX plane.

Comment
Add comment · Show 6 · 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 Karsnen_2 · Dec 02, 2011 at 10:17 PM 0
Share

Perfect. Thank you very much. I highly appreciate your help. I had a turret model and I happen to add it at the end. It works. I just got one doubt. The velocity which I I/P - is it at m/s?

avatar image aldonaletto · Dec 03, 2011 at 03:00 AM 1
Share

Yes, the velocity is in m/s - Unity uses the $$anonymous$$$$anonymous$$S system (metro/kg/seconds).

avatar image 24adithya · Jun 07, 2012 at 06:57 PM 0
Share

If i want to move the projectile along x-axis at a particular anlge say 30 degrees, what i thought was required var shootDir = Quaternion.Euler(30, 0, 0) * Vector3.right; //because i am assu$$anonymous$$g the projectile is making 30 degrees angle //with x-axis. Please correct me if i am wrong.

but here we are specifying the angle along the z-direction i guess. How does the angle get deter$$anonymous$$ed ? I mean what is the reference ? How do i understand 'with' which axis the angle is established y the projectile and how ?

avatar image aldonaletto · Jun 08, 2012 at 12:23 AM 0
Share

In the case above, Y is the vertical axis and X is the horizontal axis. The axis Z runs forward from the camera, thus in order to shoot along the X axis we must define an angle around Z, like below:

avatar image 24adithya · Jun 08, 2012 at 04:14 AM 0
Share

Thanks. That helps a lot. Now I am trying to shoot the projectile along the z-axis and i have written my code as:

var shootDir = Quaternion.Euler(45 ,0, 0) Vector3.forward; // transform.rigidbody.velocity = shootDir 10; transform.rigidbody.AddForce(shootDir * 50);

but that isn't working ! Rotation along x-axis and forward direction should accomplish this i thought.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Increase velocity without changing trajectory 1 Answer

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

Projectile trajectory velocity and angle instead of force with a rigidbody 2 Answers

Projectile launch insufficient velocity 2 Answers

Velocity and AddForce Problems 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