Unity 官方程式API 也是同樣功能 LookAt (在transform.LookAt(gameobjcet.transform));
------------------
Script(c#):
更新程式碼 ( 功能含 射程範圍、限制物件旋轉角度)
Script(c#):
using UnityEngine;
using System.Collections;
public class CannonData : MonoBehaviour {
// 我方觀看物件
private GameObject lookOjcetType;
public GameObject lookObjcet {
get { return lookOjcetType; }
set { lookOjcetType = value; }
}
// 我方自己物件
private GameObject meOjcetType;
public GameObject meOjcet {
get { return meOjcetType; }
set { meOjcetType = value; }
}
// 我方選轉角度
private float rotationAngleTpye;
public float rotationAngle {
get { return rotationAngleTpye; }
set { rotationAngleTpye = value; }
}
// 我方射程範圍
private float rangeType;
public float range {
get { return rangeType; }
set { rangeType = value; }
}
// 選擇正與反向
private int modeType;
public int mode {
get { return modeType; }
set { modeType = value; }
}
public void LookAtFouction() {
switch (modeType) {
case 1:
if (Vector3.Distance(lookOjcetType.transform.position, meOjcetType.transform.position) < rangeType) {
// 計算向量,目標座標 - 自己自身座標
Vector3 vec = lookOjcetType.transform.position - meOjcetType.transform.position;
// 降量轉為四元素即旋轉值
Quaternion rota = Quaternion.LookRotation(new Vector3(-vec.x, -vec.y, -vec.z));
// 計算rota與自身旋轉角度的夾角,在此砲台的默認選轉設為 Quaternion.identity
float angle = Quaternion.Angle(rota, Quaternion.identity);
if (angle > rotationAngleTpye * 0.5f)
{
meOjcetType.transform.rotation = Quaternion.Lerp(meOjcetType.transform.rotation, rota, 0.2f);
Debug.DrawLine(meOjcetType.transform.position, lookOjcetType.transform.position, Color.red);
}
}
break;
case 2:
if (Vector3.Distance(lookOjcetType.transform.position, meOjcetType.transform.position) < rangeType)
{
Vector3 vec2 = lookOjcetType.transform.position - meOjcetType.transform.position;
Quaternion rota2 = Quaternion.LookRotation(vec2);
float angle2 = Quaternion.Angle(rota2, Quaternion.identity);
if (angle2 > rotationAngleTpye * 0.5f)
{
meOjcetType.transform.rotation = Quaternion.Lerp(meOjcetType.transform.rotation, rota2, 0.2f);
Debug.DrawLine(meOjcetType.transform.position, lookOjcetType.transform.position, Color.red);
}
}
break;
}
}
}
------------------
Script(c#):
public class PlasmaData : MonoBehaviour { // 物件觀看 public void lookAtObjcet(GameObject lookOjcet, GameObject meOjcet) { Vector3 vec = meOjcet.transform.position - lookOjcet.transform.position; Quaternion rota = Quaternion.LookRotation(new Vector3(-vec.x, -vec.y, -vec.z)); float angle = Quaternion.Angle(rota, Quaternion.identity); meOjcet.transform.rotation = Quaternion.Lerp(meOjcet.transform.rotation, rota, 0.2f); } }------------------
更新程式碼 ( 功能含 射程範圍、限制物件旋轉角度)
Script(c#):
using UnityEngine;
using System.Collections;
public class CannonData : MonoBehaviour {
// 我方觀看物件
private GameObject lookOjcetType;
public GameObject lookObjcet {
get { return lookOjcetType; }
set { lookOjcetType = value; }
}
// 我方自己物件
private GameObject meOjcetType;
public GameObject meOjcet {
get { return meOjcetType; }
set { meOjcetType = value; }
}
// 我方選轉角度
private float rotationAngleTpye;
public float rotationAngle {
get { return rotationAngleTpye; }
set { rotationAngleTpye = value; }
}
// 我方射程範圍
private float rangeType;
public float range {
get { return rangeType; }
set { rangeType = value; }
}
// 選擇正與反向
private int modeType;
public int mode {
get { return modeType; }
set { modeType = value; }
}
public void LookAtFouction() {
switch (modeType) {
case 1:
if (Vector3.Distance(lookOjcetType.transform.position, meOjcetType.transform.position) < rangeType) {
// 計算向量,目標座標 - 自己自身座標
Vector3 vec = lookOjcetType.transform.position - meOjcetType.transform.position;
// 降量轉為四元素即旋轉值
Quaternion rota = Quaternion.LookRotation(new Vector3(-vec.x, -vec.y, -vec.z));
// 計算rota與自身旋轉角度的夾角,在此砲台的默認選轉設為 Quaternion.identity
float angle = Quaternion.Angle(rota, Quaternion.identity);
if (angle > rotationAngleTpye * 0.5f)
{
meOjcetType.transform.rotation = Quaternion.Lerp(meOjcetType.transform.rotation, rota, 0.2f);
Debug.DrawLine(meOjcetType.transform.position, lookOjcetType.transform.position, Color.red);
}
}
break;
case 2:
if (Vector3.Distance(lookOjcetType.transform.position, meOjcetType.transform.position) < rangeType)
{
Vector3 vec2 = lookOjcetType.transform.position - meOjcetType.transform.position;
Quaternion rota2 = Quaternion.LookRotation(vec2);
float angle2 = Quaternion.Angle(rota2, Quaternion.identity);
if (angle2 > rotationAngleTpye * 0.5f)
{
meOjcetType.transform.rotation = Quaternion.Lerp(meOjcetType.transform.rotation, rota2, 0.2f);
Debug.DrawLine(meOjcetType.transform.position, lookOjcetType.transform.position, Color.red);
}
}
break;
}
}
}
-----------------------
使用方式-說明:
※ 利用物件裡面的程式加入Component記得在Awake方法呼叫,如果使用Update 方式呼叫很吃效能,也可以寫在Start裡面呼叫,但是小弟並不建議就是了。
Script(c#) :
using UnityEngine;
using System.Collections;
public class PlasmaFunction : PlasmaData{
public GameObject[] door, charged;
public float x = 327, speed = 5;
public GameObject enemy, barrel; // 敵人, 自己
public float rotationAngle; // 大砲選轉角度
public float range; // 射程
public int mode; // 大砲選轉模式
CannonData cannon;
void Awake() {
cannon = gameObject.AddComponent<CannonData>();
cannon.lookObjcet = enemy;
cannon.meOjcet = barrel;
cannon.rotationAngle = rotationAngle;
cannon.range = range;
cannon.mode = mode;
}
void Update(){
cannon.LookAtFouction();
}
}
沒有留言:
張貼留言