前提・実現したいこと
クリックしたところに球を飛ばしたい
奥にあるオブジェクトでも当たるようにしたい
発生している問題・エラーメッセージ
Rayを可視化したらカーソルを右にしているのに左斜め奥をRayが指している。
画面右にカーソルを置いてクリックしたら球は飛ぶが左斜め奥に飛んでいく。
該当のソースコード
C#
1ソースコード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClickShotScript : MonoBehaviour {
public Camera camera;
public GameObject bullet;
public Transform muzzle;
public float speed = 50;
// Use this for initialization void Start () { } // Update is called once per frame void Update () { ClickShot(); } void ClickShot() { int distance = 50; Ray ray = camera.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo, distance)) { if (Input.GetMouseButtonDown(0)) { GameObject bullets = Instantiate(bullet) as GameObject; Vector3 force = hitInfo.point; bullets.GetComponent<Rigidbody>().AddForce(force); bullets.transform.position = muzzle.position; DenkiManager.CreateNumbers -= 1; } Debug.DrawRay(ray.origin, hitInfo.point, Color.red); } }
}
試したこと
Game画面をMaximizeしてカーソルを画面右においた
→Rayが指せる範囲は変わらなかった
補足情報(FW/ツールのバージョンなど)
Unity version 2017.3.1f1 Personal
あなたの回答
tips
プレビュー