• 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 Pauls · Nov 28, 2012 at 01:24 PM · distanceangleballtrajectoryarc

Find the right angle for certain distance with arc effect?

Hi,

i have read that to find a trajectory, we can use this equation with pointA and pointB (AB = distance) :

AB = v0^2 sin2a / gravity

i am looking only for the angle, as i will have a constant speed to throw the ball, so depending on the distance, how could i find the right angle?

I found this thread : http://answers.unity3d.com/questions/145972/how-to-make-enemy-canon-ball-fall-on-mooving-targe.html

but the function uses a different velocity compared to the angle, i would need to use the same velocity. So i would like to find the angle automatically when we have the distance, so that the ball can start coming down at the middle of pointA and pointB.

Is the equation above correct to find this in a 3d environment?

If so, i thought i could calculate it like this : sin2a = AB g / v0^2
but then i am not quite sure how to go on to only find "a", should i calculate the result of AB
g / v0^2 , then :

 var distArc : float = Vector3.Distance(to.position, thisTransform.position);
 var sin2a : float = (distArc * -1) / (speedArc*speedArc);
 Debug.Log(Mathf.Asin(sin2a));

and divide the total by 2?
The result in the Debug.Log is something like 0.2, 0.4, until it reaches the target (i only tested with a simple addForce in the forward direction), i am not sure if i am following a wrong way or not...

Thanks

Comment
Add comment · Show 4
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 fafase · Nov 28, 2012 at 01:45 PM 0
Share

If you have the vector already,I would think you only need the angle with some world vectors like so:

 Vector3.Angle(theVec, Vector3.forward);

Is that so ?

avatar image Pauls · Nov 28, 2012 at 05:54 PM 0
Share

@fafase but i would need more to calculate the right angle for the right distance, i thought i could just stop the addForce when the middlepoint between A and B has been passed, but i miss the logic to find the right angle. Vector3.Angle would just give me the angle between 2 characters (x and z because both meshes have the same y) but not how much angle i would need to throw the ball in the air and make it come down at pointB

avatar image fafase · Nov 28, 2012 at 07:09 PM 0
Share

Ok you may want to have a look at this page http://unitygems.com/mistakes1/

Check out the rigidbody section. There is a AngryBird like demo. You will see that the ball goes depending on the angle made by the mouse. $$anonymous$$aybe that will give you an idea.

avatar image Pauls · Dec 02, 2012 at 02:27 AM 0
Share

@fafase thanks fafase, the link with angrybird is actually just a direction you take from the end point of the mouse, and then you add a force to this direction. I haven't found yet, but i am trying something, i set the same angle/direction for all kind of distance, and calculate when we're at the middle of the distance, if so i change the direction towards the target, i haven't tried yet, if it works ok i will confirm. ;) Thanks for the comments anyway, if anybody sees this thread and thinks of any other idea, please post, your idea/answer is welcome! ;)

1 Reply

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

Answer by Pauls · Dec 03, 2012 at 12:37 AM

So, this is not the best approach i think, but it kind of works, i used a characterController instead of a Rigidbody, and tell the ball to change its orientation comparing to the distance it has already made. If someone finds a good approach, he is welcome to share :) Hope this helps anyway :

 function Start () {
     thisTransform = GetComponent(Transform);
     character = GetComponent(CharacterController);
     
     distM = Vector3.Distance(cube.position, thisTransform.position);
     startPoint = thisTransform.position;
     
     //rotation to parallel
     parallel = thisTransform.rotation;
     parallel.eulerAngles = Vector3(0, 0, 0);
 }
 
 function Update () {
     var dir : Vector3 = cube.position - thisTransform.position;
     //if 1/3 of distM
     if (Vector3.Distance(startPoint, thisTransform.position) > distM/3 ){
         if (Vector3.Distance(startPoint, thisTransform.position) < distM/2){
             //from 1/3 to middle
             thisTransform.rotation = Quaternion.Slerp( thisTransform.rotation, parallel, Time.deltaTime  *speedRot );
         } else {
             //from middle to end
             thisTransform.rotation = Quaternion.Slerp( thisTransform.rotation, Quaternion.LookRotation(dir), Time.deltaTime  *speedRot );
         }
     }
 
     if (Input.GetAxis("Horizontal")>0) yes = true;
     if (yes) {
         character.Move(thisTransform.forward * Time.deltaTime  *30);
     }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Increase velocity without changing trajectory 1 Answer

How do i get joystick angle and distance from its origin place? 2 Answers

Fixed Joint2D Malfunctioning? 0 Answers

How to calculate the angle of a trajectory to hit the target 1 Answer

ballistic trajectory around a sphere 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