いろいろな掲示板を見ていると、これはよくある問題なのですが、オンラインで良い答えを見つけることができませんでした。
私のプロジェクトには、矢印キーで動く一人称の車があります。車に搭載された銃が、現在の画面のどこにでも向けることができる十字線を介して撃つことができるようにしたい. 今のところ、画面をクリックしても何も起こらないとき (約 50%) を除いて、弾丸は常に真ん中を通り抜けます。これは、Web上のさまざまなスクリプトを介して取得したコードです。
var speed = 20;
var bullet: GameObject;
function Update () {
var hit : RaycastHit;
if(Input.GetButtonDown("Fire1")){
var ray = Camera.main.ScreenPointToRay (Input.mousePosition); //ray from
// through the mousePosition.
if(Physics.Raycast(ray, hit, 1000)) { //does the ray collide with
// anything.
//add stuff here for finding type of object and such.
Debug.Log("Hit Something at mouse position");
Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
//Display the ray.
var projectile:GameObject = Instantiate(bullet,transform.position,transform.rotation);
projectile.rigidbody.velocity = transform.forward * speed;
}
}
}
誰かが助けることができれば、それは非常に高く評価されます.