void Update() { if (!aimingMode) { return; } Vector3 mousePos2D = Input.mousePosition; mousePos2D.z = -Camera.main.transform.position.z; Vector3 mousePos3D = Camera.main.ScreenToWorldPoint(mousePos2D); Vector3 mouseDelta = mousePos3D - launchPos; float maxMagnitude = this.GetComponent <SphereCollider>().radius; if (mouseDelta.magnitude > maxMagnitude) { mouseDelta.Normalize(); mouseDelta *= maxMagnitude; } Vector3 projPos = launchPos + mouseDelta; projectile.transform.position = projPos; if (Input.GetMouseButtonUp(0)) { aimingMode = false; projectileRigidBody.isKinematic = false; projectileRigidBody.velocity = -mouseDelta * velocityMult; FollowCam.POI = projectile; projectile = null; MissionDemolition.ShotsFired(); ProjectileLine.S.poi = projectile; } }
void Update() { //If Slingshot is not in aimingMode, don't run this code if (!aimingMode) { return; } //Get the current mouse position in 2D screen coordinates Vector3 mousePos2D = Input.mousePosition; mousePos2D.z = -Camera.main.transform.position.z; Vector3 mousePos3D = Camera.main.ScreenToWorldPoint(mousePos2D); //Find the delta from the launchPos to mousePos3D Vector3 mouseDelta = mousePos3D - launchPos; //Limit mouseDelta to the radius of the Slingshot SphereCollider float maxMagnitude = this.GetComponent <SphereCollider>().radius; if (mouseDelta.magnitude > maxMagnitude) { mouseDelta.Normalize(); mouseDelta *= maxMagnitude; } //Move the projectile to this new position Vector3 projPos = launchPos + mouseDelta; projectile.transform.position = projPos; if (Input.GetMouseButtonUp(0)) { //The Mouse has been released aimingMode = false; projectileRigidbody.isKinematic = false; projectileRigidbody.velocity = -mouseDelta * velocityMult; FollowCam.POI = projectile; projectile = null; MissionDemolition.ShotsFired(); ProjectileLine.S.poi = projectile; } }
private void Update() { if (!aimingMode) { return; } //Get the current mouse position in 2D screen coordinates Vector3 mousePos2D = Input.mousePosition; //This informs how far to push mousePos3D in the 3D space using the camera mousePos2D.z = -Camera.main.transform.position.z; //Sets the mouse's position in 3D space Vector3 mousePos3D = Camera.main.ScreenToWorldPoint(mousePos2D); //Find the delta from the launchPos to the mousePos3D Vector3 mouseDelta = mousePos3D - launchPos; //Limit mouseDealta to the radius of the Slingshot SphereCollider float maxMagnitude = this.GetComponent <SphereCollider>().radius; if (mouseDelta.magnitude > maxMagnitude) { mouseDelta.Normalize(); mouseDelta *= maxMagnitude; } //Move the projectile to this new position //projPos is the result of the launchPos (which is the starting position) plus the difference of where the mouse is Vector3 projPos = launchPos + mouseDelta; //This is the part that actually moves it by setting the projectile's position projectile.transform.position = projPos; if (Input.GetMouseButtonUp(0)) { //The primary mouse button has been released aimingMode = false; projectileRigidBody.isKinematic = false; projectileRigidBody.velocity = -mouseDelta * velocityMult; //Sets projectile as the POI we want our camera to follow FollowCam.POI = projectile; //Empties the projectile field so we can fire another projectile = null; MissionDemolition.ShotsFired(); ProjectileLine.S.poi = projectile; } }