physics.Raycast返回虚假统一

发布于 2025-02-08 23:10:58 字数 2340 浏览 2 评论 0原文

我一直在尝试制作一个简单的射线播放,但我一直都会变得虚假,我的两个对象都有网框和盒子碰撞器。

我正在试图制造一条射线,如果玩家或敌人看到另一个射击

玩家,而敌人则具有以下组件:转换,盒子对撞机,网眼对撞机,僵化机构和脚本(ShothOnsightPlayer),Line Renderer,Nav Mesh Mesh Agent。

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

public class ShootOnSightPlayer : MonoBehaviour
{
    public float damage =10f;
    public float range = 100f;
    [SerializeField]
    public GameObject target;
    [SerializeField]
    public bool hasShoot = false;
    public RaycastHit hit;

    public Rigidbody bullet;
    private bool checkIfTurn = false;
    public Transform cannon;

    private Rigidbody clone;
    
    // Bit shift the index of the layer (8) to get a bit mask
    int layerMask = 1 << 8;

    void Start(){
        layerMask = ~layerMask;
         Debug.Log( " Raycast has hit from " + gameObject.transform.tag);
    }
    void FixedUpdate()
    {

        if(gameObject.GetComponent<EnemyController>()){
            checkIfTurn = gameObject.GetComponent<EnemyController>().isTurn;
        }else if(gameObject.GetComponent<PlayerController>()){
            checkIfTurn = gameObject.GetComponent<PlayerController>().isTurn;
        }   



        // This would cast rays only against colliders in layer 8.
        // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
        
        Debug.Log(Physics.Raycast( cannon.position,  target.transform.position, out hit));
        if (Physics.Raycast( cannon.position,   target.transform.position, out hit) ){

                 Debug.DrawLine( cannon.position, target.transform.position, Color.white, 2.5f);

                
             Debug.Log("Hit tag " +  hit.collider.tag + " Target Tag " + target.tag + " Has Shoot " + hasShoot + " is my turn " + checkIfTurn);

            if( hit.collider.tag == target.tag  && !hasShoot && checkIfTurn ) {

            // Instantiate the projectile at the position and rotation of this transform
            
            clone = Instantiate(bullet, cannon.position, gameObject.transform.rotation);
           
            clone.transform.SetParent(gameObject.transform);
           

            hasShoot = true;
               
            }
        }
        
    }
}


i been trying to make just a simple Raycast but im getting false all the time, both of my object has MeshColliders and Box Colliders.

I'm trying to make a ray that if the player or the enemy see the other one shoot

players and enemy has these components: Transform ,Box Collider,Mesh Collider,Rigidbody, and Script(ShootOnSightPlayer),Line Renderer,Nav Mesh Agent.

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

public class ShootOnSightPlayer : MonoBehaviour
{
    public float damage =10f;
    public float range = 100f;
    [SerializeField]
    public GameObject target;
    [SerializeField]
    public bool hasShoot = false;
    public RaycastHit hit;

    public Rigidbody bullet;
    private bool checkIfTurn = false;
    public Transform cannon;

    private Rigidbody clone;
    
    // Bit shift the index of the layer (8) to get a bit mask
    int layerMask = 1 << 8;

    void Start(){
        layerMask = ~layerMask;
         Debug.Log( " Raycast has hit from " + gameObject.transform.tag);
    }
    void FixedUpdate()
    {

        if(gameObject.GetComponent<EnemyController>()){
            checkIfTurn = gameObject.GetComponent<EnemyController>().isTurn;
        }else if(gameObject.GetComponent<PlayerController>()){
            checkIfTurn = gameObject.GetComponent<PlayerController>().isTurn;
        }   



        // This would cast rays only against colliders in layer 8.
        // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
        
        Debug.Log(Physics.Raycast( cannon.position,  target.transform.position, out hit));
        if (Physics.Raycast( cannon.position,   target.transform.position, out hit) ){

                 Debug.DrawLine( cannon.position, target.transform.position, Color.white, 2.5f);

                
             Debug.Log("Hit tag " +  hit.collider.tag + " Target Tag " + target.tag + " Has Shoot " + hasShoot + " is my turn " + checkIfTurn);

            if( hit.collider.tag == target.tag  && !hasShoot && checkIfTurn ) {

            // Instantiate the projectile at the position and rotation of this transform
            
            clone = Instantiate(bullet, cannon.position, gameObject.transform.rotation);
           
            clone.transform.SetParent(gameObject.transform);
           

            hasShoot = true;
               
            }
        }
        
    }
}


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光瘦了 2025-02-15 23:10:58

此问题的原因是第二个射线广播参数要求您提供方向轴,而不是第二个目标的位置。

Physics.Raycast(fromPosition, direction, out var hit);

问题有两种解决方案:

首先计算方向轴

,从梁的起点减去目标,以获得方向轴,通过减去两个轴,它为您提供了其全长方向,同样需要大小要始终保持1个。您可以将其归一化,这在这里不需要。

Physics.Raycast( cannon.position, target.transform.position-cannon.position, out hit)
var direction = (target.transform.position - cannon.position).normalized;

使用Linecast

您可以使用physics.linecast作为替代方案,实际上是面向目标的射线播放。

Physics.Linecast(cannon.position, target.transform.position, out hit);

The reason for this problem is that the second raycast parameter asks you for the direction axis, not the location of the second target.

Physics.Raycast(fromPosition, direction, out var hit);

There are two solutions to the problem:

Calculate Direction Axis

First, subtract the target from the starting point of the beam to obtain the direction axis, by subtracting the two axes, it gives you its full-length direction, also if you want the size to always be constant at 1. You can normalize it, which is not necessary here.

Physics.Raycast( cannon.position, target.transform.position-cannon.position, out hit)
var direction = (target.transform.position - cannon.position).normalized;

Using Linecast

You can use Physics.Linecast as an alternative, which is actually a target-oriented raycast.

Physics.Linecast(cannon.position, target.transform.position, out hit);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文