新建發射雷射的基準物體
本文使用的雷射是以物體的三維座標為主,所以可以在槍管或是隨便建立一個空物件,並且新增 Line Render 這個 Component。
雷射腳本
雷射所用的 Line Render 使用方式,是設定兩個點,就可以變成一個直線。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lazer : MonoBehaviour {
LineRenderer line;
void Start () {
line = gameObject.GetComponent();
line.enabled = true;
Screen.lockCursor = true; //鎖定滑鼠,可有可無
}
void Update () {
if (Input.GetButtonDown("Fire2"))
{
StopCoroutine("FireLaser");
StartCoroutine("FireLaser");
}
}
IEnumerator FireLaser()
{
line.enabled = true;
while (Input.GetButton("Fire2"))
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
line.SetPosition(0, ray.origin); //設定 Line Render 的起始點位置
if(Physics.Raycast(ray,out hit, 100))
{
line.SetPosition(1, hit.point); //設定第二個 Line Render 第二個點位置,即可連成一條線
if (hit.rigidbody)
{
hit.rigidbody.AddForceAtPosition(transform.forward * 50, hit.point); //如果 ray 打到的物體是剛體,就讓物體作功
}
}else
{
line.SetPosition(1, ray.GetPoint(100)); //如果都沒打到物體,就發射 100 這麼長的射線
}
yield return null;
}
line.enabled = false;
}
}
Reference:
https://docs.unity3d.com/ScriptReference/LineRenderer.SetPosition.html
https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
https://unity3d.com/learn/tutorials/topics/graphics/fun-lasers
沒有留言:
張貼留言