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 Shayan Rais · Oct 15, 2013 at 02:00 PM · vector3directionangletarget

Angle to Direction

I am making a tennis game. I am able to shoot the ball at the given target.. but the ball is moving in a straight line .. I want the ball to move at certain angles.. 30, 45, 60 so that I can make diffrent shots in my game ... loop Shot, Power Shot etc etc

alt text

"Right now the ball is going in the direction of the target and I've also figured out the angles for the height of the shot but the ball doesn't land exactly on the target."

CODE UPTILL NOW:

 Vector3 direction;
 
 direction = (target.transform.localPosition - ball.transform.localPosition).normalized;
         
 direction.y = Mathf.Sin(30 * Mathf.PI / 180);
 
 ball.rigidbody.AddForce(direction * 100);
target.png (453.7 kB)
Comment
Add comment · Show 2
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 robertbu · Oct 15, 2013 at 03:52 PM 0
Share

I don't know what you are asking for here. What do the angles represent with respect to this drawing and the target? What is a 'power shot'? What is a 'loop shot'? How are you moving the ball...Rigidbody and AddForce or some other way? Posting your current code might get you a more accurate answer.

avatar image Owen-Reynolds · Oct 15, 2013 at 05:14 PM 0
Share

$$anonymous$$ake the Q more specific. Does 30 degrees mean thirty from a line straight down the court (global) or 30 from the way he is ai$$anonymous$$g (local)? Is the angle left/right (y) or up/down(x)? Or within a cone of thirty (to represent imperfect shots?)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Oct 18, 2013 at 08:19 AM

Doing what you ask for here is a pretty complicated calculation. There are several detailed answers on UA on projectile motion and targeting. Do a bit of searching and they will turn up along with (if I remember correctly) dense functions that implement the functionality. But if you make a couple of simplifications and look at this problem a bit differently, you can solve it in a few lines of code.

For projectile motion, typically the horizontal velocity is split from the vertical velocity. Instead of calculating an angle, I'm suggesting you define your different shots in terms of horizontal velocity. Horizontal velocity will define how long the ball will take to be over the target.

Given a horizontal velocity, you can calculate time it will take for the ball to be over the target. So the question is how much vertical velocity do you give the ball so that it hits at the same time the horizontal velocity places the ball over the target. If you assume/place the target at the same height as the balls initial height, then the velocity needed is easy to calculate:

 -gravity  * time / 2.0; 

So here is a bit of source code to demonstrate. Before integrating the concept into your code, I encourage you to play with it in a clean scene:

  • Start a new scene

  • Create a plane at (0.0, -0.5, 0.0), and scaled up larger (20,20,20).

  • Create a sphere and place it a the origin

  • Add a rigidbody to the sphere

  • Adjust the camera so that more can be seen: up on the 'Y', forward, and increase the angle of view.

  • Put a game object somewhere on the plane to act as a target

  • Add the following script to the ball

  • Drag the target game object onto the 'target' variable in the script.

  • Run the app and hit [Space] to launch the ball


    pragma strict

    var target : Transform; var speed = 5.0;

    function Update () {

       if (Input.GetKeyDown(KeyCode.Space)) {
         
             transform.position = Vector3.zero;  // Start with the ball at the origin
     
             var toTarget = target.position - transform.position;  // Ground vector to target
             toTarget.y = 0.0;
             
             var shotTime = toTarget.magnitude / speed;  // Time to reach target
             
             var velocity = toTarget.normalized * speed; // Setup horizontal vector
             
             velocity.y = -Physics.gravity.y * shotTime / 2.0;  // Upward velocity
         
             rigidbody.velocity = velocity;
         }
     }
    
    

While the script is running, you can adjust the speed and the target position and hit [Space] again to relaunch the ball.

Comment
Add comment · Show 1 · 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 robertbu · Oct 22, 2013 at 09:14 PM 0
Share

Here is the link to trajectory code if you want to solve this by angle:

http://answers.unity3d.com/questions/248788/calculating-ball-trajectory-in-full-3d-world.html

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

Vector3.Angle() Never Reaching 0 1 Answer

Get Direction from 2 Vectors - and Apply to Transform 0 Answers

How to get offset postition after rotation ? (Spent hours figuring it out) 3 Answers

Angle Of Ray 2 Answers

from Quaternion to RotateAround 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