我是一個初級程式員。我不會找到有關解決我的問題的資訊。我有一個 3d 游戲。任務:當您觸摸螢屏時,會創建一個子彈并向觸摸飛去。在我的代碼中,子彈飛了 https://imgur.com/fsBaDSe
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
Vector2 touchPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
Vector2 dir = touchPos - (new Vector2(transform.position.x, transform.position.z));
dir.Normalize();
GameObject bullet = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
bullet.GetComponent<Rigidbody2D>().velocity = dir * bulletSpeed;
}
}
uj5u.com熱心網友回復:
第一:向你的敵人或任何你想射擊的物體添加碰撞器。
然后創建一個新的游戲物件并將其重命名為子彈。
然后呼叫一個新的 C# 腳本Bullet并將其添加到您的子彈游戲物件中。然后將子彈游戲物件變成預制件。然后將此代碼放在bullet類中:
public float speed;
public Rigidbody rb;
Vector3 target;
public void Initialize(Vector3 _target)
{
target = _target;
transform.forward = target - transform.position;
}
public void Update()
{
rb.AddForce(transform.forward * Time.DeltaTime);
}
然后創建一個名為PlayerShoot. 將它添加到您的 PLAYER 游戲物件并將此代碼粘貼到其中:
public LayerMask shootMask;
public float shootRange;
public Transform player;
public GameObject bulletPrefab;
public Transform shootPos;
public void Update()
{
foreach(Touch t in Input.touches)
{
if(t.phase == TouchPhase.began)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.RayCast(ray, out RaycastHit hit, shootRange, shootMask)
{
player.transform.LookAt(new Vector3(hit.point.x, player.position.y, hit.point.z);
Bullet b = Instantiate(bulletPrefab, shootPos.transform.position, shootPos.rotation).GetComponent<Bullet>();
b.Initialize(hit.point);
}
}
}
}
shootPos是子彈將從哪里射出并確保shootPos向前朝您想要射出的方向。
shootRange是玩家可以射多遠。注意:如果它不起作用,請帶上shootRange鞋面。
shootMask是你想要射擊的物件。注意:如果它不起作用,請為您的玩家碰撞器添加另一個掩碼,然后從腳本檢查器的shootMasks 串列中取消選中該掩碼PlayerShoot。
uj5u.com熱心網友回復:
我會為此使用光線投射來找到子彈“打算”朝哪里移動。
這在有或沒有對撞機的情況下都可以使用,但是在有對撞機的目標上射擊會更準確。請務必將 配置layerMask為忽略您的播放器或其他應忽略的物件!
評論中的解釋:
// define what things the ray ignores
// try different configuration in the inspector to see what works for your game
[SerializeField] LayerMask raycastMask;
// define a default distance for ray use if nothing collides
[Serializefield] float defaultRayDist = 20f;
// Where the bullet is meant to be fired from
[SerializeField] Transform bulletOrigin;
//...
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
// Turn touch into ray
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
Vector3 dest;
// Determine if raycast hit occurs
if (Physics.Raycast(ray, out RaycastHit hitInfo, layerMask: raycastMask))
{
// If hit occurs use its position
dest = hitInfo.point;
}
else
{
// If no hit occurs find point along ray to use as bullet destination
dest = Ray.GetPoint(defaultRayDist);
}
// Optionally, put dest at same height as bulletOrigin
// dest.y = bulletOrigin.position.y;
// Find direction from start to destination
Vector3 dir = (dest - bulletOrigin.position).normalized;
// Create bullet and set its rotation and velocity
GameObject bullet = Instantiate(projectile, bulletOrigin.position,
Quaternion.LookRotation(dir)) as GameObject;
bullet.GetComponent<Rigidbody2D>().velocity = dir * bulletSpeed;
}
uj5u.com熱心網友回復:
很簡單。
public Transform target;
public float depth = 4f;
public void Update()
{
Vector3 touchPoint = Camera.main.ScreenPointToWorld(new Vector3(Input.mousePosition.x, Input.mousePosition.y, depth);
target.position = touchPoint;
}
uj5u.com熱心網友回復:
一個完整的例子:
void Update()
{
Vector3 touchPoint = Camera.main.ScreenPointToWorld(new
Vector3(Input.mousePosition.x, Input.mousePosition.y, depth);
float speed = .3f;
bullet.transform.position = Vector3.Lerp(bullet.transform.position,
touchPoint,
speed * Time.deltaTime);
bullet.transform.forward = touchPoint - bullet.transform.position;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/365050.html
上一篇:如何獲取陣列中每個物件的屬性?
