You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

203 lines
5.0 KiB

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class magnetGun : MonoBehaviour {
public Camera camera;
public GameObject crossHair;
public thirdPersonController playerController;
public string L_TRIGGER_INPUT;
public string R_TRIGGER_INPUT;
public string L_BUTTON_INPUT;
public string R_BUTTON_INPUT;
private bool L_Trigger_Down;
public bool topScreen = true;
private float playerScreen = 2;
public GameObject gravityWell;
private Collider GravityTarget;
private Vector3 rayHitPoint;
public float objectSpeed;
public float magnetRange = 20;
public float maxPullSpeed = 30;
public float rotateSpeed;
public float minDistance = 3.0f;
// Use this for initialization
void Start () {
if (topScreen)
playerScreen = 2;
else
playerScreen = -1;
}
// Update is called once per frame
void Update () {
float triggerL = Input.GetAxis(L_TRIGGER_INPUT);
float triggerR = Input.GetAxis (R_TRIGGER_INPUT);
Collider target = testItem ();
crossHair.GetComponent<RawImage> ().color = Color.white;
if (target != null){
if (target.GetComponent<Collider>().tag == "moveable"){
Debug.Log("Moveable Item");
crossHair.GetComponent<RawImage>().color = Color.red;
}
}
if (triggerL > 0) {
if (!L_Trigger_Down) {
pickUpItem (target);
L_Trigger_Down = true;
}
} else {
L_Trigger_Down = false;
dropItem(GravityTarget);
}
moveItem (GravityTarget);
pullItem (triggerR);
rotateInput (GravityTarget);
cameraRotateTest (GravityTarget);
}
private Collider testItem(){
RaycastHit hit;
//Vector3 rayDirection = camera.transform.rotation * Vector3.forward;
Ray ray = camera.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2 + (Screen.height/6 * playerScreen), 0));
Debug.DrawRay (ray.origin, ray.direction*magnetRange, Color.green);
if (Physics.Raycast (ray, out hit, magnetRange)) {
if (hit.collider.tag == "moveable"){
Debug.DrawRay (ray.origin,ray.direction*magnetRange, Color.red);
rayHitPoint = hit.point;
}
}
return hit.collider;
}
private void pickUpItem(Collider item){
if (item != null) {
if (item.tag == "moveable"){
item.attachedRigidbody.useGravity = false;
item.attachedRigidbody.drag = 3.0f;
item.attachedRigidbody.constraints = RigidbodyConstraints.FreezeRotation;
gravityWell.transform.position = rayHitPoint;
GravityTarget = item;
playerController.movementSpeed = 200.0f;
if (!topScreen){
playerController.cameraSpeedX = 0.0f;
playerController.cameraSpeedY = 0.0f;
}
}
}
}
private void moveItem(Collider item){
if (item != null) {
float step = objectSpeed * Time.deltaTime;
Vector3 direction = gravityWell.transform.position - item.transform.position;
direction = Vector3.ClampMagnitude(direction,1.0f);
item.attachedRigidbody.AddForce(direction * objectSpeed * Time.deltaTime);
}
}
private void dropItem(Collider item){
if (item != null) {
item.attachedRigidbody.useGravity = true;
item.attachedRigidbody.drag = 0.0f;
item.attachedRigidbody.constraints = RigidbodyConstraints.None;
if(topScreen)
item.attachedRigidbody.velocity = Vector3.ClampMagnitude (item.attachedRigidbody.velocity, 5);
GravityTarget = null;
playerController.movementSpeed = 5000.0f;
playerController.cameraSpeedX = 250.0f;
playerController.cameraSpeedY = 120.0f;
}
}
private void pullItem(float speed){
float step = maxPullSpeed * speed * Time.deltaTime;
Vector3 maxPull;
maxPull = camera.transform.position + (camera.transform.rotation * Vector3.forward);
if (Vector3.Distance (gravityWell.transform.position, camera.transform.position) > minDistance || maxPullSpeed<1) {
gravityWell.transform.position = Vector3.MoveTowards (gravityWell.transform.position, maxPull, step);
if ((Vector3.Distance (gravityWell.transform.position, camera.transform.position) > magnetRange) && !topScreen)
dropItem (GravityTarget);
}
}
private void rotateInput(Collider item){
if (item != null) {
if (Input.GetButtonDown(L_BUTTON_INPUT))
StartCoroutine (rotateItem(item,new Vector3 (0,90,0),0.3f));
if (Input.GetButtonDown(R_BUTTON_INPUT))
StartCoroutine (rotateItem(item,new Vector3 (90,0,0),0.3f));
}
}
IEnumerator rotateItem (Collider item, Vector3 byAngles, float inTime){
Quaternion startAngle = item.transform.rotation;
Quaternion endAngle = Quaternion.Euler (item.transform.eulerAngles + byAngles);
for (float i = 0; i < 1; i+=Time.deltaTime/inTime) {
item.transform.rotation = Quaternion.Lerp(startAngle,endAngle,i);
yield return null;
}
}
public void cameraRotateTest(Collider item){
if (item != null) {
Debug.Log ("distance test: " + Vector3.Distance (gravityWell.transform.position, camera.transform.position));
if (Vector3.Distance (gravityWell.transform.position, camera.transform.position) <= minDistance) {
playerController.cameraSpeedX = 250.0f;
playerController.cameraSpeedY = 120.0f;
} else {
playerController.cameraSpeedX = 0.0f;
playerController.cameraSpeedY = 0.0f;
}
}
}
}