Hello,
I’m trying to make a 2d game where the “jump” mechanic is similar to the Angry Birds catapult. I want to draw the trajectory on screen as the player pulls back the mouse and then fire the ball (player) along the path when the mouse button has been released.
I found a formula for plotting trajectory here.
I’m moving a square to debug the position of the trajectory and the first attempt seems to work. I move the mouse around the ball and the little square is opposite the mouse and the further I pull back the further from the player it goes. When I release the mouse button, the ball travels at the right direction, but not with enough force.
I’m not sure where I’m going wrong, could anyone shed some light on this? I’ve been stuck for a week or so now.
Thanks
//waiting for mouse button to be lifted AND player initially clicked - FIRE!
if (Input.GetMouseButtonUp(0) && clickedPlayer == true){
playerCurrentPos.rigidbody.velocity = jumpVector * multiplier;
}
//Draw trajectory
if (Input.GetMouseButton(0)){
//Pull back position in screen space
jumpEnd = Input.mousePosition;
//Convert screen space to world space
p = Camera.main.ScreenToWorldPoint (Vector3 (jumpEnd.x, jumpEnd.y, 12.7));
//Try and detect angle
angle1 = (p - playerCurrentPos.transform.position);
angle2 = new Vector3(1, 0, 0);
angle = Vector3.Angle(angle1, angle2);
//Getting opposite angle
if (p.y <= playerCurrentPos.transform.position.y){
jumpAngle = (180 - angle) * Mathf.Deg2Rad;
}
else{
jumpAngle = angle * Mathf.Deg2Rad;
}
//Draw vector (player position to mouse drag position)
Debug.DrawLine(playerCurrentPos.transform.position, p, Color.red, 40);
//Work out jump vector
jumpVector = playerCurrentPos.transform.position - p;
//Vetical and horizontal velocity calculation
var xVelocity : float = (jumpVector.magnitude*multiplier) * Mathf.Cos(jumpAngle);
var yVelocity : float = (jumpVector.magnitude*multiplier) * Mathf.Sin(jumpAngle);
//Trajectory of X
var trajX : float = xVelocity * 0.5;
//Trajectory of Y
var trajY : float = (yVelocity * 0.5) - 0.5 * Physics.gravity.y * Mathf.Pow(0.5, 2);
//Debug - position a square object in the scene to match the velocity of the projectile after 0.5 seconds
square.transform.position = Vector3(trajX, trajY, 0);
}