C# Unity 3D如何将武器瞄准光线投射命中

C# Unity 3D如何将武器瞄准光线投射命中,c#,unity3d,raycasting,C#,Unity3d,Raycasting,我有一种可以发射射弹的武器,我试着将武器旋转到光线投射命中的方向。我还附上了两个脚本,一个用于射击,一个用于瞄准射击脚本效果很好。 这是我的武器脚本,用于激发我的射弹: using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Security.Cryptography; using System.Thre

我有一种可以发射射弹的武器,我试着将武器旋转到光线投射命中的方向。我还附上了两个脚本,一个用于射击,一个用于瞄准射击脚本效果很好。 这是我的武器脚本,用于激发我的射弹:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;
using UnityEngine.AI;
using Random = UnityEngine.Random; //   |source: https://community.gamedev.tv/t/solved-random-is-an-ambiguous-reference/7440/9

public class weapon_shooting : MonoBehaviour
{
    //deklariere projektil |source: https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
    public GameObject projectilePrefab;
    private float projectileSize = 0;
    public Rigidbody rb;
    public float projectileSpeed = 50;
    public float projectileSizeRandomMin = 10;
    public float projectileSizeRandomMax = 35;

    void Update()
    {
        // Ctrl was pressed, launch a projectile
        if (Input.GetButtonDown("Fire1"))
        {
            //random sized bullets
            projectileSize = Random.Range(projectileSizeRandomMin, projectileSizeRandomMax);
            projectilePrefab.transform.localScale = new Vector3(projectileSize, projectileSize, projectileSize);

            // Instantiate the projectile at the position and rotation of this transform
            Rigidbody clone;
            clone = Instantiate(rb, transform.position, transform.rotation);

            // Give the cloned object an initial velocity along the current
            // object's Z axis
            clone.velocity = transform.TransformDirection(Vector3.forward * projectileSpeed);
        }
    }
}
然后我试着从屏幕中间投射一条光线到鼠标位置,然后旋转我的武器到光线与我的世界碰撞的地方。但当我运行它时,它会向各个方向发射:=o(无错误)需要一些帮助才能解决这个问题:) 这是我的武器瞄准脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class weapon_aiming : MonoBehaviour
{
    public Camera camera;
    private bool ray_hit_something = false;

    void Update()
    {
        RaycastHit hit;
        Ray ray = camera.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit))
        {
            ray_hit_something = true;
        } else
        {
            ray_hit_something = false;
        }

        if (ray_hit_something == true) { 
            transform.LookAt(Camera.main.ViewportToScreenPoint(hit.point));
        }
    }
}
只需使用
LookAt(hit.point)
即可,因为
LookAt
需要在世界空间中找到一个位置,并且
hit.point
已经在世界空间中:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class weapon_aiming : MonoBehaviour
{
    public Camera camera;
    private bool ray_hit_something = false;

    void Update()
    {
        RaycastHit hit;
        Ray ray = camera.ScreenPointToRay(Input.mousePosition);

        ray_hit_something = Physics.Raycast(ray, out hit);

        if (ray_hit_something) { 
            transform.LookAt(hit.point);
        }
    }
}

为什么要使用Camera.main.ViewportToScreenPoint
LookAt
期望在世界空间中有一个位置,
hit.point
已经是世界空间中的一个位置,
ViewportToScreenPoint
与世界空间无关。你有没有尝试过简单的
transform.LookAt(hit.point)?另外,您可以只做
ray\u hit\u something=Physics.Raycast(ray,out hit)
如果(ray_hit_something){…}
你完全正确,我只是简单地改为transform.LookAt(hit.point);它的工作原理是你是一个天使