67 lines
1.7 KiB
C#
67 lines
1.7 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
public class CrossbowArrow : MonoBehaviour {
|
|
|
|
public Vector3 StartPoint;
|
|
public Vector3 EndPoint;
|
|
public Vector3 CurrentPosition;
|
|
public Vector3 NextPosition;
|
|
public float Speed;
|
|
public GameObject physicsHits;
|
|
private float distance = 0.0f;
|
|
private RaycastHit hit;
|
|
private Vector3 forward;
|
|
|
|
// Use this for initialization
|
|
void Start () {
|
|
forward = (EndPoint - StartPoint).normalized;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update () {
|
|
CurrentPosition = StartPoint + (EndPoint - StartPoint).normalized * distance;
|
|
distance += Time.deltaTime * Speed;
|
|
|
|
NextPosition = StartPoint + (EndPoint - StartPoint).normalized * distance;
|
|
|
|
|
|
if (distance >= 50) {
|
|
Destroy (this);
|
|
} else {
|
|
RaycastHit hit;
|
|
if(Physics.Raycast(new Ray(CurrentPosition, forward), out hit)){
|
|
//Debug.Log("Pointing on: " + hit.collider.gameObject.name);
|
|
if(hit.distance <= (NextPosition - CurrentPosition).magnitude){
|
|
NextPosition = CurrentPosition + transform.forward * hit.distance;
|
|
Destroy(gameObject);
|
|
|
|
var mat = hit.collider.sharedMaterial;
|
|
string hitName = "HitDefault";
|
|
if(mat != null && mat.name == "Glass"){
|
|
hitName = "HitGlass";
|
|
}
|
|
|
|
GameObject hitObjSrc = physicsHits.transform.Find(hitName).gameObject;
|
|
GameObject hitObj = (GameObject)Instantiate(hitObjSrc);
|
|
hitObj.transform.localPosition = hit.point;
|
|
hitObj.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
|
|
hitObj.SetActive(true);
|
|
|
|
//hit.normal
|
|
|
|
}
|
|
}else{
|
|
//Debug.Log("Pointing on: Nothing");
|
|
}
|
|
|
|
}
|
|
|
|
transform.position = NextPosition;
|
|
}
|
|
|
|
void OnDrawGizmos(){
|
|
Gizmos.DrawLine (CurrentPosition, CurrentPosition + forward * 10);
|
|
}
|
|
}
|