using UnityEngine; using System.Collections; using UnityEngine.UI; public class magnetGun2 : MonoBehaviour { public Camera camera; public GameObject crossHair; public GameObject gravityWell; public thirdPersonController playerController; public float magnetMaxRange; public float magnetMinRange; private Collider gravityTarget; // Use this for initialization void Start () { magnetMinRange += playerController.cameraDistance; magnetMaxRange += playerController.cameraDistance; } // Update is called once per frame void Update () { RaycastHit target = testItem (); if (gravityTarget != null) pickUpItem (target); } //test to see if player can move item private RaycastHit testItem(){ RaycastHit hit; //resets cross hair to white crossHair.GetComponent ().color = Color.white; //cast ray from camera through the crosshair on screen Ray ray = camera.ScreenPointToRay (crossHair.transform.position); Debug.DrawRay (ray.origin, ray.direction*magnetMaxRange, Color.green); // if player can pickup object turn crosshair red // if object is close enougth to move with it turn blue; if (Physics.Raycast (ray, out hit, magnetMaxRange)) { if (hit.collider.tag == "moveable"){ gravityTarget = hit.collider; Debug.DrawRay (ray.origin,ray.direction*magnetMaxRange, Color.red); crossHair.GetComponent().color = Color.red; if (Vector3.Distance (hit.point, camera.transform.position) <= magnetMinRange) { crossHair.GetComponent().color = Color.blue; }//end blue }//end red }//end raycast return hit; }// end testItem() private void pickUpItem(RaycastHit hit){ Collider item = hit.collider; //prepares item to be lifted item.attachedRigidbody.useGravity = false; item.attachedRigidbody.drag = 3.0f; item.attachedRigidbody.constraints = RigidbodyConstraints.FreezeRotation; gravityWell.transform.position = item.transform.position; transform.LookAt (gravityWell.transform.position, Vector3.up); } }