-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPaddlePlayer1.cs
85 lines (74 loc) · 2.38 KB
/
PaddlePlayer1.cs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using UnityEngine;
using System.Collections;
public class PaddlePlayer1 : MonoBehaviour {
public int speed = 50;
private Vector3 movement;
private RaycastHit hit;
private bool selected = false;
// Use this for initialization
void Start () {
hit = new RaycastHit();
}
void FixedUpdate () {
if (Input.GetKey("right")){
if (this.transform.localPosition.x < 0.326f){
movement = Vector3.right;
this.transform.Translate(movement * 500 * Time.deltaTime);
}
}
if (Input.GetKey("left")){
if (this.transform.localPosition.x > -0.326f){
movement = Vector3.left;
this.transform.Translate(movement * 500 * Time.deltaTime);
}
}
if(Input.GetMouseButton(0)){
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit)) {
if(hit.collider.gameObject.CompareTag("RedPad")){
selected = true;
}
}
if (selected){
Vector3 mousepos = Input.mousePosition;
Transform camTransform = Camera.main.transform;
Vector3 move = Camera.main.ScreenToWorldPoint(new Vector3 (mousepos.x, mousepos.y,
camTransform.position.y - this.transform.position.y)) - this.transform.position;
move.y = 0.0f;
move.z = 0.0f;
Vector3 newPosition = this.rigidbody.position + move;
if ((newPosition.x > -525f) && (newPosition.x < 525f) ){
this.rigidbody.transform.Translate(move);
}
}
}
foreach (Touch touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
// Construct a ray from the current touch coordinates
Ray ray = Camera.main.ScreenPointToRay (touch.position);
if (Physics.Raycast (ray, out hit)) {
if(hit.collider.gameObject.CompareTag("RedPad")){
selected = true;
}
}
}
else if( touch.phase == TouchPhase.Moved){
if (selected){
Vector3 touchpos = touch.position;
Transform camTransform = Camera.main.transform;
Vector3 move = Camera.main.ScreenToWorldPoint(new Vector3 (touchpos.x, touchpos.y,
camTransform.position.y - this.transform.position.y)) - this.transform.position;
move.y = 0.0f;
move.z = 0.0f;
Vector3 newPosition = this.rigidbody.position + move;
if ((newPosition.x > -525f) && (newPosition.x < 525f) ){
this.rigidbody.transform.Translate(move);
}
}
}
else if( touch.phase == TouchPhase.Ended){
selected = false;
}
}
}
}