arskrald/AR-1/Assets/RayScript.cs

116 lines
3.7 KiB
C#
Raw Normal View History

2019-02-11 10:04:28 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayScript : MonoBehaviour
{
// Start is called before the first frame update
Transform topCannon;
2019-02-11 10:40:07 +00:00
bool part1 = true;
2019-02-11 10:04:28 +00:00
bool part2 = false;
2019-02-11 10:40:07 +00:00
bool part3 = false;
2019-02-11 10:04:28 +00:00
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) {
2019-02-11 10:40:07 +00:00
print("Hit");
2019-02-11 10:04:28 +00:00
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();
}
}
}