93 lines
2.2 KiB
C#
93 lines
2.2 KiB
C#
using DragonLi.Frame;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
[RequireComponent(typeof(LineRenderer))]
|
|
public class SuperRay : MonoBehaviour
|
|
{
|
|
public GameObject flash;
|
|
public GameObject hit;
|
|
public int damage = 1;
|
|
public AudioClip clip;
|
|
public AudioSource audioSource;
|
|
|
|
private LineRenderer lineRenderer;
|
|
private bool isShooting = false;
|
|
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
lineRenderer = transform.GetComponent<LineRenderer>();
|
|
}
|
|
|
|
public void CreateLine()
|
|
{
|
|
flash.SetActive(false);
|
|
hit.SetActive(false);
|
|
if (isShooting)
|
|
{
|
|
RaycastHit raycast;
|
|
Ray ray = new Ray();
|
|
ray.origin = transform.position;
|
|
ray.direction = transform.forward;
|
|
lineRenderer.SetPosition(0, transform.position);
|
|
if (Physics.Raycast(ray, out raycast))
|
|
{
|
|
lineRenderer.SetPosition(1, raycast.point);
|
|
hit.transform.position = raycast.point;
|
|
hit.SetActive(true);
|
|
if (raycast.transform.tag == "Enemy")
|
|
{
|
|
ApplyDamage(raycast);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lineRenderer.SetPosition(1, transform.position + ray.direction * 30);
|
|
}
|
|
flash.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
lineRenderer.SetPosition(0, transform.position);
|
|
lineRenderer.SetPosition(1, transform.position);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void Shot()
|
|
{
|
|
isShooting = true;
|
|
audioSource.enabled = true;
|
|
|
|
}
|
|
|
|
public void ColseShot()
|
|
{
|
|
isShooting = false;
|
|
audioSource.enabled = false;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (Input.GetKey(KeyCode.Q))
|
|
{
|
|
Shot();
|
|
}
|
|
CreateLine();
|
|
}
|
|
|
|
private void ApplyDamage(RaycastHit raycast)
|
|
{
|
|
// Debug.Log("造成伤害");
|
|
IDamagable component = raycast.transform.GetComponent<IDamagable>();
|
|
if (component != null)
|
|
{
|
|
component.ApplyDamage(damage, null, transform);
|
|
}
|
|
}
|
|
}
|