Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ZeredaGames
Created February 21, 2019 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZeredaGames/7ea63bbc1881aa03d8cd605fd8cd0012 to your computer and use it in GitHub Desktop.
Save ZeredaGames/7ea63bbc1881aa03d8cd605fd8cd0012 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
namespace UnityDevelopment.BowlMaster
{
[RequireComponent (typeof(Ball))]
public class BallDragLaunch : MonoBehaviour
{
private Vector3 dragStart, dragEnd;
private float startTime, endTime;
private Ball ball;
// Use this for initialization
void Start ()
{
ball = GetComponent<Ball> ();
}
public void MoveStart (float amount)
{
if (!ball.inPlay) {
float xPos = Mathf.Clamp (ball.transform.position.x + amount, -35f, 35f);
float yPos = ball.transform.position.y;
float zPos = ball.transform.position.z;
ball.transform.position = new Vector3 (xPos, yPos, zPos);
}
}
public void DragStart ()
{
if (!ball.inPlay) {
// Capture time & position of drag start
dragStart = Input.mousePosition;
startTime = Time.time;
}
}
public void DragEnd ()
{
if (!ball.inPlay) {
// Launch the ball
dragEnd = Input.mousePosition;
endTime = Time.time;
float dragDuration = endTime - startTime;
float launchSpeedX = (dragEnd.x - dragStart.x) / dragDuration;
float launchSpeedY = (dragEnd.y - dragStart.y) / dragDuration;
float launchSpeedZ = (dragEnd.y - dragStart.y) / dragDuration;
Vector3 launchVelocity = new Vector3 (launchSpeedX, -launchSpeedY, launchSpeedZ);
ball.Launch (launchVelocity);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment