-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cs
97 lines (85 loc) · 3.11 KB
/
Player.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
86
87
88
89
90
91
92
93
94
95
96
97
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SocialPlatforms;
using UnityEngine.XR;
public class Player : MonoBehaviour
{
public GameObject boxPrefab;
public GameObject ballPrefab;
public GameObject bombPrefab;
public Transform Hand;
public float throwForce;
public Camera camera;
public LayerMask layerMask;
public float maxDistance;
public GameObject objInHand;
public float grabDistance;
public float attractionForce;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
GameObject box = Instantiate(boxPrefab, Hand.position, Quaternion.identity);
box.GetComponent<Rigidbody>().AddForce(camera.transform.forward * throwForce, ForceMode.Impulse);
}
if (Input.GetKeyDown(KeyCode.T))
{
GameObject ball = Instantiate(ballPrefab, Hand.position, Quaternion.identity);
ball.GetComponent<Rigidbody>().AddForce(camera.transform.forward * throwForce, ForceMode.Impulse);
}
if (Input.GetKey(KeyCode.Mouse1) && objInHand == null)
{
Ray ray = new Ray(camera.transform.position, camera.transform.forward);
//Just for helping
Debug.DrawLine(ray.origin, ray.GetPoint(maxDistance));
RaycastHit hit;
if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
{
if (Vector3.Distance(Hand.position, hit.transform.position) < grabDistance)
{
objInHand = hit.transform.gameObject;
objInHand.transform.position = Hand.position;
objInHand.GetComponent<Rigidbody>().isKinematic = true;
objInHand.transform.parent = Hand;
}
else
{
Vector3 dir = Vector3.Normalize(Hand.position - hit.transform.position);
hit.transform.GetComponent<Rigidbody>().AddForce(dir * attractionForce * Time.deltaTime, ForceMode.Impulse);
}
}
}
if (Input.GetKeyDown(KeyCode.Mouse0))
{
if (objInHand != null)
{
objInHand.GetComponent<Rigidbody>().isKinematic = false;
objInHand.transform.parent = null;
objInHand.GetComponent<Rigidbody>().AddForce(camera.transform.forward * throwForce, ForceMode.Impulse);
objInHand = null;
}
else
{
objInHand = Instantiate(bombPrefab, Hand.position, Quaternion.identity);
objInHand.GetComponent<Rigidbody>().AddForce(camera.transform.forward * throwForce, ForceMode.Impulse);
}
}
if (Input.GetKeyDown(KeyCode.H))
{
if (Time.timeScale == 1)
{
Time.timeScale = 0.5f;
}
else
{
Time.timeScale = 1f;
}
}
}
}