Created
January 11, 2018 23:56
-
-
Save h1ggs/9784a222831d21ca60cc65f845a0236d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class gunController : MonoBehaviour { | |
public float fireRate; | |
public GameObject endofLine; | |
public LineRenderer laserLineTrace; | |
// Use this for initialization | |
void Start () { | |
Cursor.visible = false; | |
} | |
// Update is called once per frame | |
void Update () { | |
//This draws the LineRenderer to start from the gun's position to end at the crosshair | |
Vector3 playerPositionv3 = this.gameObject.transform.position; | |
laserLineTrace.SetPosition (0, playerPositionv3); | |
laserLineTrace.SetPosition (1, endofLine.transform.position); | |
//This locates the mouse cursor position and puts the crosshair at that point | |
Vector3 crosshairLocation = Camera.main.ScreenToWorldPoint(Input.mousePosition); | |
crosshairLocation.z = 0; | |
endofLine.transform.position = crosshairLocation; | |
} | |
void FixedUpdate(){ | |
//This gets the mouse cursor position relative to the screen | |
Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint (transform.position); | |
aimGun (dir); | |
Vector3 mouseCursorLocation = dir; | |
Vector2 playerLocation = GameObject.Find ("Player").transform.position; | |
RaycastHit2D hit = Physics2D.Raycast (playerLocation, mouseCursorLocation); | |
Debug.DrawRay (playerLocation, mouseCursorLocation); | |
} | |
void aimGun(Vector3 dir){ | |
//This aims the gun | |
float angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg; | |
this.gameObject.transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward); | |
if (this.gameObject.transform.rotation.z > 90) { | |
Vector3 gunRot = this.gameObject.transform.localScale; | |
gunRot.x *= -1; | |
this.gameObject.transform.localScale = gunRot; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment