Improvement for Play Testing - Getting the Perfect Shot

Hi Guys, just thought I would post this here, it is a modified DragLaunch Script that allows you the developer to switch on and off a test mode, that will allow you to bowl the perfect game meaning now more stopping and starting

  • Simply select the ball and click the developer test mode and enter in the perfect shot velocity mine was 0,0,1500
  • When you want a challenge simply deselect the developer test mode

Code is Below

private Ball ball;
private Vector3 dragStart, dragEnd;
private float startTime, endTime;

//Test Code (To Be Removed)
public bool developerTest;
public Vector3 developerTestVelocity;

// Use this for initialization
void Start () {
	ball = GetComponent<Ball>();
}

public void DragStart(){
	//Capture time and posistion of drag start
	dragStart = Input.mousePosition;
	startTime = Time.time;
}

public void DragEnd(){
	//Launch the ball
	dragEnd = Input.mousePosition;
	endTime = Time.time;

	float dragDuration = endTime - startTime;

	float launchSpeedX = (dragEnd.x - dragStart.x) / dragDuration;
	float launchSpeedZ = (dragEnd.y - dragStart.y) / dragDuration;

	//Note developerTest & developerTestVelocity to be removed just in place to auto roll the ball correctly
	if (developerTest == false){
		Vector3 launchVelocity = new Vector3(launchSpeedX, 0, launchSpeedZ);
		if(ball.inPlay == false){
			ball.Launch(launchVelocity);
		}
	}else {
		ball.Launch(developerTestVelocity);
	}

}

public void MoveStart (float amount){
	//Debug.Log ("Ball Moved " + amount);
	if(ball.inPlay == false){
		ball.transform.Translate(new Vector3 (amount,0,0));
	}
}

Hi, really nice solution, thanks for sharing :slight_smile:

One improvement I would make, is to switch the check around for the check on whether the ball is in ‘test mode’, and return early. There is no point calculating the player input velocity if it won’t be used… it’s a micro-optimisation, but they all add up :wink:

So you would have…

public void DragEnd()
    {
        if (developerTest)
        {
            ball.Launch(developerTestVelocity);
            return;
        }

        //Launch the ball
        dragEnd = Input.mousePosition;
        endTime = Time.time;

        float dragDuration = endTime - startTime;

        float launchSpeedX = (dragEnd.x - dragStart.x) / dragDuration;
        float launchSpeedZ = (dragEnd.y - dragStart.y) / dragDuration;

        Vector3 launchVelocity = new Vector3(launchSpeedX, 0, launchSpeedZ);
        if (ball.inPlay == false)
        {
            ball.Launch(launchVelocity);
        }
    }
1 Like

Nice improvement every little helps

Privacy & Terms