3D游戲
我需要在 C# 中改進 Unity 中的射擊代碼,以便武器不僅沿一條軌跡射擊,因為子彈沿 z 方向飛行并且在轉彎時不會改變軌跡。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotToClick : MonoBehaviour
{
public GameObject Bullet;
public float Power;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
GameObject b = Instantiate(Bullet, transform.position, transform.rotation);
b.GetComponent<Rigidbody>().AddForce(Vector3.forward * Power, ForceMode.Impulse);
}
}
}
我是初學者,我嘗試通過創建專案來學習,希望對您有所幫助
uj5u.com熱心網友回復:
也許像這樣。使用變換的方向
GameObject b = Instantiate(Bullet, transform.position, transform.rotation);
b.GetComponent<Rigidbody>().AddForce(transform.TransformDirection (Vector3.forward) * Power, ForceMode.Impulse);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/528947.html
標籤:C#unity3d