Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
DMD2542-Project003-RaycastingRepo-JacobTomassi/DMD2542-Project003-Raycasting-JacobTomassi/Assets/Let's Try Shooter/Scripts/Raycastshooter.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
63 lines (46 sloc)
1.46 KB
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 Raycastshooter : MonoBehaviour | |
{ | |
public int gundamage = 1; | |
public float fireRate = .05f; | |
public float weaponRange = 50f; | |
public float hitForce = 100f; | |
public Transform gunEnd; | |
private Camera fpsCam; | |
private WaitForSeconds shotDuration = new WaitForSeconds(.07f); | |
private LineRenderer laserLine; | |
void Start() | |
{ | |
laserLine = GetComponent<LineRenderer>(); | |
gunAudio = GetComponent<AudioSource>(); | |
fpsCam = GetComponentInParent<Camera>(); | |
} | |
void Update() | |
{ | |
if (Input.GetButtonDown ("Firel") && Time.time > nextFire) | |
{ | |
nextFire = Time.time + fireRate; | |
StartCoroutine(ShotEffect()); | |
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0)); | |
RaycastHit hit; | |
laserLine.SetPosition(0, gunEnd.position); | |
if (Physics.Raycast(rayOrigin,fpsCam.transform.forward, out hit, weaponRange)) | |
{ | |
laserLine.SetPosition(1, hit.point); | |
} | |
else | |
{ | |
laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange)); | |
} | |
} | |
} | |
private IEnumerator ShotEffect() | |
{ | |
gunAudio.Play(); | |
laserLine.enabled = true; | |
yield return shotDuration; | |
laserLine.enabled = false; | |
} | |
} |