I'm currently working on a simple baseball game. What I'm trying to do is have the player be able to swing the bat backwards to "charge up" power, so to speak, and then when a button is released he will swing the bat forward at a speed equal to the amount of power stored up.
This is all well and good, but my problem is I need to stop the motion of the bat when he reach a certain point on the y-axis, and I'm a bit unsure how to go about doing this as I cannot just tell it to stop rotating after a set time as the bat won't reach the front point at the same time every time due to the difference in speed each swing might have.
What I'm trying to do basically is something like if(reached 266 on the y-axis stop rotating)
, but I'm unsure of how to do this.
Anyway here is the code I've written so far:
public int rotateSpeed = 50;
public float AmountOfPowerChargedUp = 0;
void Update()
{
if (Input.GetMouseButton(0))
{
AmountOfPowerChargedUp += 5f;
transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime);
Debug.Log(AmountOfPowerChargedUp);
}
if (!Input.GetMouseButton (0))
{
transform.Rotate(Vector3.down * AmountOfPowerChargedUp * Time.deltaTime);
}
}
private void OnGUI()
{
GUI.Label (new Rect (50, 15, 250, 25), "AmountOfPowerChargedUp: " + AmountOfPowerChargedUp);
}