[英]unity3d Move object to touch point
我是初學者程序員。 我找不到有關解決我的問題的信息。 我有一個 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;
}
}
第一:將對撞機添加到您的敵人或您想要射擊的任何東西上。
然后制作一個新游戲 Object 並將其重命名為子彈。
然后制作一個名為Bullet
的新 C# 腳本並將其添加到您的子彈游戲 object 中。 然后將子彈游戲 object 轉為預制件。 然后將此代碼放入bullet
class 中:
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
的新 C# 腳本。 將其添加到您的 PLAYER 游戲 object 並將此代碼粘貼到其中:
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
是你要拍攝的東西。 注意:如果它不起作用,請為您的玩家對撞機添加另一個蒙版,然后在PlayerShoot
腳本檢查器中從shootMask
列表中取消選中該蒙版。
我會為此使用光線投射來找到子彈“打算”向哪里移動。
這將在有或沒有對撞機的情況下工作,但在有對撞機的目標上射擊會更准確。 請務必將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;
}
很簡單。
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;
}
一個完整的例子:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.