-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDragLaunch.cs
More file actions
47 lines (38 loc) · 1.15 KB
/
Copy pathDragLaunch.cs
File metadata and controls
47 lines (38 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof(Ball))]
public class DragLaunch : MonoBehaviour {
private Ball ball;
private Vector3 dragStart, dragEnd;
private float startTime, endTime;
// Use this for initialization
void Start () {
ball = GetComponent<Ball>();
}
public void DragStart(){
// Capture time and position of mouse click
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;
Vector3 launchVelocity = new Vector3(launchSpeedX, 0, launchSpeedZ);
if(!ball.ballLaunched){
ball.Launch(launchVelocity);
}
}
public void MoveStart(float xNudge){
if(!ball.ballLaunched){
float xPos = Mathf.Clamp(ball.transform.position.x + xNudge, -51, 51);
float yPos = ball.transform.position.y;
float zPos = ball.transform.position.z;
ball.transform.position = new Vector3(xPos, yPos, zPos);
}
}
}