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; private Vector3 targetOffset; public float objectSpeed; public float magnetRange = 20; public float maxPullSpeed = 30; public float rotateSpeed; public float minDistance = 3.0f; public float impulsePower = 50.0f; private RigidbodyConstraints originalConstrants; private bool originalGravity; private float maxWalkSpeed; private Renderer lastTargetRenderer; private Color lastTargetColour; // Use this for initialization void Start () { if (topScreen) playerScreen = 2; else playerScreen = -1; maxWalkSpeed = playerController.movementSpeed; } // 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 ().color = Color.white; if (target != null){ if (target.GetComponent().tag == "moveable"){ Debug.Log("Moveable Item"); crossHair.GetComponent().color = Color.red; if (Vector3.Distance (rayHitPoint, camera.transform.position) <= minDistance) { crossHair.GetComponent().color = Color.blue; } } } updateColors (target); if (triggerL > 0) { if (!L_Trigger_Down) { pickUpItem (target); L_Trigger_Down = true; } } else { L_Trigger_Down = false; dropItem(GravityTarget); } impulsePush (target); 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)); Ray ray = camera.ScreenPointToRay(crossHair.transform.position); 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"){ originalConstrants = item.attachedRigidbody.constraints; originalGravity = item.attachedRigidbody.useGravity; item.attachedRigidbody.useGravity = false; item.attachedRigidbody.drag = 3.0f; item.attachedRigidbody.constraints = RigidbodyConstraints.FreezeRotation | originalConstrants; //camera.transform.LookAt(item.transform.position); //playerController.cameraX = camera.transform.eulerAngles.x; //playerController.cameraY = camera.transform.eulerAngles.y; gravityWell.transform.position = rayHitPoint; GravityTarget = item; targetOffset = GravityTarget.transform.position - rayHitPoint + (Vector3.up * 0.05f); playerController.movementSpeed = 200.0f; if (!topScreen){ playerController.cameraSpeedX = 0.0f; playerController.cameraSpeedY = 0.0f; } } } } private void impulsePush (Collider item){ if (item != null) { if (item.tag == "moveable" && Input.GetButtonDown("Quick Push")){ Vector3 direction = (transform.position - item.transform.position).normalized; item.attachedRigidbody.AddForce(direction * impulsePower * maxPullSpeed* item.attachedRigidbody.mass,ForceMode.Impulse ); } } } private void moveItem(Collider item){ if (item != null) { float step = objectSpeed * Time.deltaTime; Vector3 direction = gravityWell.transform.position - item.transform.position + targetOffset; direction = Vector3.ClampMagnitude(direction,1.0f); item.attachedRigidbody.AddForce(direction * objectSpeed * Time.deltaTime); } } private void dropItem(Collider item){ if (item != null) { item.attachedRigidbody.useGravity = originalGravity; item.attachedRigidbody.drag = 0.0f; item.attachedRigidbody.constraints = originalConstrants; if(topScreen) item.attachedRigidbody.velocity = Vector3.ClampMagnitude (item.attachedRigidbody.velocity, 5); GravityTarget = null; playerController.movementSpeed = maxWalkSpeed; 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 && Vector3.Distance (gravityWell.transform.position, camera.transform.position) < magnetRange) || 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; } } } public void updateColors(Collider target){ if (target != null && target.tag == "moveable") { lastTargetRenderer = target.GetComponent (); if (lastTargetRenderer.material.color != Color.white) { lastTargetColour = lastTargetRenderer.material.color; lastTargetRenderer.material.color = Color.white; } } else { if (lastTargetRenderer != null) lastTargetRenderer.material.color = lastTargetColour; } } }