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][1]
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();
//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);
}
}
[1]: /storage/temp/47409-forum.jpg
↧