Created
February 21, 2019 17:32
-
-
Save ZeredaGames/7ea63bbc1881aa03d8cd605fd8cd0012 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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