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 gpimp · May 31, 2015 at 08:50 PM · javascriptuiphysicsvector3gameplay

I want the pitcher to throw a ball to a target a location inside a GUI panel

I am wanting the user to mouse click a location inside the GUI Panel and have the pitcher throw the ball to that location in the panel.

The problem that I am having is even though I click a point in the panel the balls trajectory is way off. Could it be that when I convert from screen space to world space the target point in world space is wrong ?

alt text

Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerPitcher : MonoBehaviour 
 {
     public GameObject ball;
     public GameObject parentBoneToBall;
     public float ballForceX = 0.07f;
     public float ballForceY = 0.5f;
     public float ballForceZ = -3.0f;
     private Animator animator;
     private bool InMyState = false;
     private GameObject ballClone;
     private bool startPitchAgain = true;
     Vector3  targetDirection  =  Vector3.zero;
     Vector3 pitchTarget = Vector3.zero;
 
 
 
     // Use this for initialization
     void Start () 
     {
         animator = GetComponent<Animator>();
         //ball.transform.parent = parentBoneToBall.transform;
         ballClone = Instantiate(ball, ball.transform.position, ball.transform.rotation) as GameObject;
         ballClone.transform.parent = parentBoneToBall.transform;
         ballClone.rigidbody.useGravity = false;
     }
 
     private void UserThrowingPitch()
     {
         if (Input.GetMouseButtonDown (1) && startPitchAgain) 
         {
 
             animator.SetBool ("isThrowingFastBall", true);
     
 
             float distance = 10.0f;
             pitchTarget = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 100));
             Debug.Log("Mouse Target in World Space" + pitchTarget);  
 
             startPitchAgain = false;
         }
         
         if(this.animator.GetCurrentAnimatorStateInfo(0).IsName("Pitcher Base Layer.Pitch_ThrowFastBall"))
         {
             // Avoid any reload.
             InMyState = true;
         }
         else if (this.InMyState)
         {
             InMyState = false;
             // You have just left your state!
             animator.SetBool ("isThrowingFastBall", false);
             ballClone = Instantiate(ball, parentBoneToBall.transform.position, parentBoneToBall.transform.rotation) as GameObject;
             ballClone.transform.parent = parentBoneToBall.transform;
             ballClone.rigidbody.useGravity = false;
 
             startPitchAgain = true;
         }
     }
 
     // Update is called once per frame
     void Update () 
     {
         UserThrowingPitch ();
     }
 
     private Vector3 calculateBestThrowSpeed(Vector3 origin, Vector3 target, float timeToTarget) {
         // calculate vectors
         Vector3 toTarget = target - origin;
         Vector3 toTargetXZ = toTarget;
         toTargetXZ.y = 0;
         
         // calculate xz and y
         float y = toTarget.y;
         float xz = toTargetXZ.magnitude;
         
         // calculate starting speeds for xz and y. Physics forumulase deltaX = v0 * t + 1/2 * a * t * t
         // where a is "-gravity" but only on the y plane, and a is 0 in xz plane.
         // so xz = v0xz * t => v0xz = xz / t
         // and y = v0y * t - 1/2 * gravity * t * t => v0y * t = y + 1/2 * gravity * t * t => v0y = y / t + 1/2 * gravity * t
         float t = timeToTarget;
         float v0y = y / t + 0.5f * Physics.gravity.magnitude * t;
         float v0xz = xz / t;
         
         // create result vector for calculated starting speeds
         Vector3 result = toTargetXZ.normalized;        // get direction of xz but with magnitude 1
         result *= v0xz;                                // set magnitude of xz to v0xz (starting speed in xz plane)
         result.y = v0y;                                // set y to v0y (starting speed of y plane)
         
         return result;
     }
 
 
     public void ReleasePitch(AnimationEvent animEvent)
     {
         Debug.Log("PitchReleased");
     
         targetDirection = pitchTarget - parentBoneToBall.transform.position;
         ballClone.transform.LookAt(pitchTarget);    
         ballClone.transform.parent = null;
         ballClone.rigidbody.useGravity = false;
 
         Debug.Log("PitchTarget" + pitchTarget); 
 
         pitchTarget.z *= -1.0f;
 
 
         Vector3 throwSpeed = calculateBestThrowSpeed(parentBoneToBall.transform.position, pitchTarget, 0.3f);
         ballClone.rigidbody.AddForce(throwSpeed);
 
     }
 
     public void DestroyBall()
     {
         Destroy (ballClone);
     }
 }
 


forum.jpg (103.5 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Collision work on pc emulator but not on android device 0 Answers

Rigidody.velocity = Direction * speed; How to get direction? 1 Answer

AI Aircraft/Spacecraft yaw and pitch help 1 Answer

Vector3.Reflect problems 4 Answers

How do I make the bullet Tracer move towards the location of where the bullet hole prefab gets instantiated? 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