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 Mungoid · Dec 01, 2011 at 05:00 AM · angleprojectile

Fire/Shoot at intersect point

Ok so i know this has been answered before and i know its simple game math and i thought i had the right function (FromToRotation) but it just keeps going in one direction and i cant figure out what math im looking for, so i apologize

I want to fire a projectile from my player to wherever on the ground i clicked. I am able to Instantiate the projectile and make it move in a fixed direction but i cannot seem to figure out how to angle the projectile to face the intersect point on the ground and move towards it. I know where the intersect point is as well, just not the math to do the rest

 if(Input.GetMouseButtonDown(0)){
     var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit : RaycastHit;
     var didHit : boolean = Physics.Raycast(ray, hit);
     //var cursor = GameObject.Find(currentCursor.name);
     
     if(didHit) {
         
         var angle = Quaternion.FromToRotation(shootFrom.transform.position, hit.point);
         
         var spell = Instantiate(prefab, shootFrom.transform.position, angle);
         spell.rigidbody.AddForce(transform.right * shootSpeed);
         
     }
 }

I tried to determine the angle but it doesn't seem to work. What am i doing wrong?

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

Answer by syclamoth · Dec 01, 2011 at 05:03 AM

You're looking for Quaternion.LookRotation

As in,

 var angle = Quaternion.LookRotation(hit.point - shootFrom.transform.position);

Seriously, the script reference is your friend!

Comment
Add comment · Show 5 · 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 Mungoid · Dec 01, 2011 at 05:43 PM 0
Share

Edit: Scratch this, i had something somewhere else affecting it. Thanks!

Thanks for your input! I had actually used that originally but the projectile still fired along the x axis ins$$anonymous$$d of where i clicked so i thought it was the wrong one and used FromToRotation. Is there something else i need to do with angle before i put it in the instantiate method?

avatar image syclamoth · Dec 01, 2011 at 09:18 PM 0
Share

Do you want it clamped in one axis? Or do you want it to point directly at where you clicked? If you want it clamped in one axis, all you need to do is change the 'hit.point' bit-

 var correctedPosition = new Vector3(0, hit.point.y, hit.point.z);
 // Zero out a different value to clamp a different axis!

Then use correctedPosition, ins$$anonymous$$d of hit.point!

avatar image Mungoid · Dec 02, 2011 at 01:41 AM 0
Share

Nope, i just wanted to shoot towards wherever i clicked. However.. $$anonymous$$y guy fires in a direction when i click on the ground but its off a little and not sure what to do to fix it. When i use the LookRotation by itself, my projectile barely moves so i attempted to apply a speed to it by multiplying the angle by a vector3

var newAngle = angle * new Vector3(1000, 1, 1000);

Screenshot here - http://enspired.us/example.png

I clicked where my mouse is but as you can see it is off a little. Ideas? I know it has to do with my math but i wasnt sure how else to apply it

avatar image syclamoth · Dec 02, 2011 at 09:23 AM 0
Share

Oh, if the shot is a rigidbody then there's no way of preventing it from falling down over time- this happens because of gravity! I think what you really need here is trajectory analysis- which is actually a bit more complicated.

avatar image Mungoid · Dec 03, 2011 at 05:37 PM 0
Share

Heh thats what i was afraid of. Ok then, guess its time to start studying up! Thanks!! =-)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I get a normalized vector2D from an angle? 1 Answer

Quaternion.FromToRotation misunderstanding 2 Answers

How do I fire a 2D projectile based on it's current angle/location. 1 Answer

Trouble Understanding Vector3.Angle() Result 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