116 lines
3.7 KiB
C#
116 lines
3.7 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
public class RayScript : MonoBehaviour
|
|||
|
{
|
|||
|
// Start is called before the first frame update
|
|||
|
Transform topCannon;
|
|||
|
bool part1 = true;
|
|||
|
bool part2 = false;
|
|||
|
bool part3 = false;
|
|||
|
Material material;
|
|||
|
|
|||
|
bool fire = false;
|
|||
|
|
|||
|
Vector3 leftCannon = new Vector3((float)0.077, 0, (float)0.450);
|
|||
|
Vector3 rightCannon = new Vector3((float)-0.077, 0, (float)0.450);
|
|||
|
Vector3 topCannonPoint = new Vector3(0, (float)0.25, (float)-0.09);
|
|||
|
|
|||
|
Transform explosion;
|
|||
|
Transform target;
|
|||
|
|
|||
|
Matrix4x4 falconTrans;
|
|||
|
Vector3 partThreeRay;
|
|||
|
Quaternion cannonRotation;
|
|||
|
|
|||
|
|
|||
|
void Start()
|
|||
|
{
|
|||
|
topCannon = GameObject.Find("TopCannon").transform;
|
|||
|
explosion = GameObject.Find("Explosion").transform;
|
|||
|
target = GameObject.Find("FighterTarget").transform;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
// Update is called once per frame
|
|||
|
void Update() {
|
|||
|
falconTrans = transform.localToWorldMatrix;
|
|||
|
partThreeRay = falconTrans.MultiplyPoint3x4(topCannonPoint);
|
|||
|
cannonRotation = Quaternion.Euler(0, 30, 0);
|
|||
|
|
|||
|
bool hit = false;
|
|||
|
|
|||
|
if (Input.GetKeyDown("space")) {
|
|||
|
print("Firing ray");
|
|||
|
fire = true;
|
|||
|
|
|||
|
if (part1) {
|
|||
|
hit = Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), Mathf.Infinity);
|
|||
|
} else if (part2) {
|
|||
|
var trans = transform.localToWorldMatrix;
|
|||
|
var transLeft = trans.MultiplyPoint3x4(leftCannon);
|
|||
|
var transRight = trans.MultiplyPoint3x4(rightCannon);
|
|||
|
hit = (Physics.Raycast(transLeft, transform.TransformDirection(Vector3.forward), Mathf.Infinity) ||
|
|||
|
Physics.Raycast(transRight, transform.TransformDirection(Vector3.forward), Mathf.Infinity));
|
|||
|
} else if (part3) {
|
|||
|
hit = Physics.Raycast(partThreeRay, transform.TransformDirection(cannonRotation * Vector3.forward), Mathf.Infinity);
|
|||
|
}
|
|||
|
|
|||
|
} else if (Input.GetKeyUp("space")) {
|
|||
|
fire = false;
|
|||
|
}
|
|||
|
|
|||
|
if (hit) {
|
|||
|
print("Hit");
|
|||
|
explosion.position = target.position;
|
|||
|
explosion.GetComponent<ParticleSystem>().Play();
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
void OnRenderObject()
|
|||
|
{
|
|||
|
|
|||
|
if (!fire) return;
|
|||
|
if (material == null)
|
|||
|
material = new Material(Shader.Find("Hidden/Internal-Colored"));
|
|||
|
material.SetPass(0);
|
|||
|
|
|||
|
if (part1) {
|
|||
|
|
|||
|
GL.Begin(GL.LINES);
|
|||
|
GL.Color(Color.red);
|
|||
|
GL.Vertex(transform.position);
|
|||
|
GL.Vertex(transform.position + (transform.TransformDirection(Vector3.forward)));
|
|||
|
GL.End();
|
|||
|
} else if (part2) {
|
|||
|
var trans = transform.localToWorldMatrix;
|
|||
|
var transLeft = trans.MultiplyPoint3x4(leftCannon);
|
|||
|
var transRight = trans.MultiplyPoint3x4(rightCannon);
|
|||
|
|
|||
|
|
|||
|
GL.Begin(GL.LINES);
|
|||
|
GL.Color(Color.red);
|
|||
|
GL.Vertex(transLeft);
|
|||
|
GL.Vertex(transLeft + (transform.TransformDirection(Vector3.forward)));
|
|||
|
|
|||
|
GL.Begin(GL.LINES);
|
|||
|
GL.Color(Color.red);
|
|||
|
GL.Vertex(transRight);
|
|||
|
GL.Vertex(transRight + (transform.TransformDirection(Vector3.forward)));
|
|||
|
GL.End();
|
|||
|
} else if (part3) {
|
|||
|
topCannon.localPosition = topCannonPoint;
|
|||
|
|
|||
|
GL.Begin(GL.LINES);
|
|||
|
GL.Color(Color.red);
|
|||
|
GL.Vertex(partThreeRay);
|
|||
|
GL.Vertex((partThreeRay + transform.TransformDirection(cannonRotation * Vector3.forward)));
|
|||
|
GL.End();
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|