C# 抓取的对象与同一层上的对象碰撞时未被光线投射丢弃
我有可以被第一人称玩家抓取和携带的物品。光线投射仅在一个图层上检查可以拾取的所有对象是否都在该图层上 如果对象碰撞到不在同一层上的对象,则保留的对象将被丢弃,因为光线不再击中抓取的对象。这是预期的行为 但是,如果抓取的对象碰撞到同一层上的对象,则不会丢弃该对象。我相信这是因为光线继续记录层上的对象仍然处于选中状态 如何设置它,使光线投射知道它已切换对象,并删除当前对象 非常感谢您的帮助 下面是一个发生了什么的例子: 以下代码控制玩家的光线投射,以检查是否可以抓取对象:C# 抓取的对象与同一层上的对象碰撞时未被光线投射丢弃,c#,unity3d,raycasting,C#,Unity3d,Raycasting,我有可以被第一人称玩家抓取和携带的物品。光线投射仅在一个图层上检查可以拾取的所有对象是否都在该图层上 如果对象碰撞到不在同一层上的对象,则保留的对象将被丢弃,因为光线不再击中抓取的对象。这是预期的行为 但是,如果抓取的对象碰撞到同一层上的对象,则不会丢弃该对象。我相信这是因为光线继续记录层上的对象仍然处于选中状态 如何设置它,使光线投射知道它已切换对象,并删除当前对象 非常感谢您的帮助 下面是一个发生了什么的例子: 以下代码控制玩家的光线投射,以检查是否可以抓取对象: public class
public class PlayerPickUp : MonoBehaviour
{
// Public variables
public float rayLength;
public bool objectSelected;
// Private variables
Camera camera;
RaycastHit hit;
GameObject hitObject;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// Set layerMask as layer 10
int layerMask = 1 << 10;
// Create ray and check for object hit
if( Physics.Raycast(transform.position, transform.TransformDirection( Vector3.forward ), out hit, rayLength, layerMask ) )
{
// Set objectSelected to true
objectSelected = true;
// Set hit object as hitObject
hitObject = hit.collider.gameObject;
// Set hitObject's selectedByPlayer var to true
hitObject.GetComponent<AnimalGrab>().selectedByPlayer = true;
// Draw debug line
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.blue);
}
// Object no longer being selected
else if( objectSelected )
{
// Set objectSelected to false
objectSelected = false;
// Set hitObject's selectedByPlayer var to false
hitObject.GetComponent<AnimalGrab>().selectedByPlayer = false;
}
// Ray has not hit anything
else {
// Sit hit object as empty
hitObject = null;
// Draw debug line
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * rayLength, Color.yellow);
}
}
}
下面是控制抓取对象的代码:
public class ObjectGrab : MonoBehaviour
{
// Public variables
public bool beingHeld;
public bool selectedByPlayer;
public GameObject selfObject;
public GameObject tempParentObject;
public GameObject playerObject;
// Private variables
float throwForce;
float lookSpeed;
float targetDistance;
Vector3 selfPosition;
Rigidbody rb;
float selfDistance;
// Start is called before the first frame update
void Start()
{
// Assign rigidbody component
rb = selfObject.GetComponent<Rigidbody>();
// Set beingHeld as false
beingHeld = false;
// Set initial throwForce
throwForce = 1000f;
// Set initial lookSpeed
lookSpeed = 10f;
}
// Update is called once per frame
void Update()
{
// Calculate distance from parent object
selfDistance = Vector3.Distance( selfObject.transform.position, tempParentObject.transform.position );
// If being held
if( beingHeld == true )
{
// Set velocity to 0
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
// Set as child of GrabPoint
selfObject.transform.SetParent(tempParentObject.transform);
// ROTATION
//Distance between target and the actual rotating object
Vector3 targetDistance = playerObject.transform.position - transform.position;
// Calculate the Quaternion for the rotation
Quaternion rot = Quaternion.Slerp( transform.rotation, Quaternion.LookRotation( targetDistance ), lookSpeed * Time.deltaTime );
//Apply the rotation
transform.rotation = rot;
// put 0 on the axis you do not want for the rotation object to rotate
transform.eulerAngles = new Vector3( 0, transform.eulerAngles.y, 0 );
// THROWING
// If right click pressed
if( Input.GetMouseButtonDown( 1 ) )
{
// Add force to self
rb.AddForce( tempParentObject.transform.forward * throwForce );
// Set beingHeld as false
beingHeld = false;
}
}
// If not being held
else
{
selfPosition = selfObject.transform.position;
selfObject.transform.SetParent( null );
rb.useGravity = true;
selfObject.transform.position = selfPosition;
}
// If not being selected by player
if( selectedByPlayer == false )
{
// Set beingHeld as false
beingHeld = false;
}
}
// Mouse Down
void OnMouseDown()
{
if( selectedByPlayer == true )
{
// Set beingHeld as true
beingHeld = true;
// Disable gravity
selfObject.GetComponent<Rigidbody>().useGravity = false;
// Enable collision detection
selfObject.GetComponent<Rigidbody>().detectCollisions = true;
}
}
// Mouse Up
void OnMouseUp()
{
// Set beingHeld as false
beingHeld = false;
}
}
在更新函数的第一个if中:
if( Physics.Raycast[...])
您可以执行另一项检查,以测试命中对象是否已发生如下更改:
if(hit.collider.gameObject != hitObject)
{
// The object changed
}
然后在那里处理对象更改。您尝试过使用吗?我想这可能就是问题所在。您当前的对象正在触发命中,因此它后面的对象不会注册。