using UnityEngine; using System.Collections; using UnityEngine.UI; public class magnetGun : MonoBehaviour { public Camera camera; public GameObject crossHair; public thirdPersonController playerController; public magnetGun otherPlayer; 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; public 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 closeRange = 5.0f; public float impulsePower = 50.0f; private RigidbodyConstraints originalConstrants; private bool originalGravity; private bool axisDown = false; private float normGrip; private Renderer lastTargetRenderer; private Color lastTargetColour; public GameObject magnetTipObject; //----------------------- // Use PlayerSound script // (specifically for object-rotation sounds for this MagnetGun script) public PlayerSounds _playerSoundScript; // Use this for initialization void Start () { if (topScreen) playerScreen = 2; else playerScreen = -1; normGrip = playerController.grip; } // Update is called once per frame void Update () { if (playerController.active) { 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) <= closeRange) { crossHair.GetComponent ().color = Color.blue; } } } //updateColors (target); if (getAxisDown (L_TRIGGER_INPUT)) { Debug.Log ("axis down"); //Debug.Log (GravityTarget.name); if (GravityTarget != null) { dropItem (GravityTarget); Debug.Log ("dropping item"); } else { pickUpItem (target); } } /* (triggerL > 0) { if (!L_Trigger_Down) { pickUpItem (target); L_Trigger_Down = true; } } else { L_Trigger_Down = false; dropItem(GravityTarget); } */ if ( GravityTarget == null) impulsePush (target); else pullItem (triggerR); } moveItem (GravityTarget); 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) { //_playerSoundScript.PlayPickUp(); if (item == otherPlayer.GravityTarget){ otherPlayer.dropItem(item); } if (item.tag == "moveable"){ playerController.animator.SetTrigger("startHolding"); playerController.animator.SetBool("Holding",true); 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.slowed = true; playerController.movementLock = true; playerController.grip = 15.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")){ if (item == otherPlayer.GravityTarget){ otherPlayer.dropItem(item); } 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) { if (item != playerController.curCollider){ 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) { playerController.animator.SetTrigger("stopHolding"); playerController.animator.SetBool("Holding",false); 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.slowed = false; playerController.movementLock = false; playerController.grip = normGrip; playerController.cameraSpeedX = 250.0f; playerController.cameraSpeedY = 120.0f; } } private void pullItem(float speed){ if (!Input.GetButton ("Quick Push")) return; float step = maxPullSpeed * 0.5f * 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); //MagnetLaserShoot(); 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)) //_playerSoundScript.PlayRotateL(); // Rotate-Object Sound. ERROR: Held object continuously rotates StartCoroutine (rotateItem(item,new Vector3 (0,90,0),0.3f)); if (Input.GetButtonDown(R_BUTTON_INPUT)) //_playerSoundScript.PlayRotateR(); // Rotate-Object Sound. ERROR: Held object continuously rotates 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) <= closeRange) { 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; } } private bool getAxisDown (string axis){ if( Input.GetAxisRaw(axis) != 0) { if(axisDown == false) { axisDown = true; return true; } } if( Input.GetAxisRaw(axis) == 0) { axisDown = false; } return false; } // FUNCTION: Shoot Magnet Ray // void MagnetLaserShoot(){ // RaycastHit hit; // // if(Physics.Raycast(magnetTipObject.transform.position, magnetTipObject.transform.forward, out hit)){ // if(hit.collider.tag == "moveable"){ // Debug.Log("Magnet laser has collided" + hit.collider.gameObject.name); // } // } // } }