physics.Raycast返回虚假统一
我一直在尝试制作一个简单的射线播放,但我一直都会变得虚假,我的两个对象都有网框和盒子碰撞器。
我正在试图制造一条射线,如果玩家或敌人看到另一个射击
玩家,而敌人则具有以下组件:转换,盒子对撞机,网眼对撞机,僵化机构和脚本(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此问题的原因是第二个射线广播参数要求您提供方向轴,而不是第二个目标的位置。
问题有两种解决方案:
首先计算方向轴
,从梁的起点减去目标,以获得方向轴,通过减去两个轴,它为您提供了其全长方向,同样需要大小要始终保持1个。您可以将其归一化,这在这里不需要。
使用Linecast
您可以使用
physics.linecast
作为替代方案,实际上是面向目标的射线播放。The reason for this problem is that the second raycast parameter asks you for the direction axis, not the location of the second target.
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.
Using Linecast
You can use
Physics.Linecast
as an alternative, which is actually a target-oriented raycast.