-1

我有一个玩家可以在“2.5D平台射击”游戏中用枪携带物体,但我现在遇到的问题是每次我让玩家拿着物体(它有一个刚体)并按下靠墙,物体穿过墙壁。请帮助团结的粉丝。

这是我在下面用来携带对象的代码。

#pragma strict

var catchRange = 30.0;
var holdDistance = 4.0;
var minForce = 1000;
var maxForce = 10000;
var forceChargePerSec = 3000;
var layerMask : LayerMask = -1;       

@HideInInspector
var anim : Animator;

enum GravityGunState { Free, Catch, Occupied, Charge, Release};
private var gravityGunState : GravityGunState = 0;
private var rigid : Rigidbody = null;
private var currentForce = minForce;

function FixedUpdate() 
{
if(gravityGunState == GravityGunState.Free) 
{
if(Input.GetButton("Fire1")) 
{   anim.SetBool("Shoot", true);
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, catchRange, layerMask)) 
{
if(hit.rigidbody) 
{ 
rigid = hit.rigidbody;
gravityGunState = GravityGunState.Catch;

}
}
}else if(!hit.rigidbody){anim.SetBool("Shoot", false);}
}

else if(gravityGunState == GravityGunState.Catch) 
{    
rigid.useGravity = false;
rigid.MovePosition(transform.position + transform.forward * holdDistance);
if(!Input.GetButton("Fire1"))
gravityGunState = GravityGunState.Occupied;
}

else if(gravityGunState == GravityGunState.Occupied) 
{

if(Physics.Raycast(transform.position, transform.forward, hit, catchRange, layerMask)){
Debug.DrawLine (transform.position, hit.point, Color.red);
}    
rigid.MovePosition(transform.position + transform.forward * holdDistance);
if(Input.GetButton("Fire1"))
gravityGunState = GravityGunState.Charge;
}

else if(gravityGunState == GravityGunState.Charge) 
{
rigid.MovePosition(transform.position + transform.forward * holdDistance);
if(currentForce < maxForce) 
{
currentForce += forceChargePerSec * Time.deltaTime;
}

else 
{       
currentForce = maxForce;
}

if(!Input.GetButton("Fire1"))
gravityGunState = GravityGunState.Release; 
rigid.useGravity = true; 
}

else if(gravityGunState == GravityGunState.Release) 
{   
rigid.AddForce(transform.forward * currentForce);
currentForce = minForce;
gravityGunState = GravityGunState.Free;
}

}

@script ExecuteInEditMode()
4

1 回答 1

1

碰撞器添加到游戏对象将阻止对象相互移动,如果它已经有一个碰撞器并且它正在通过,请检查您的设置如下
Rigidbody Is Kinamatic : False, Is Trigger :false
将刚体的碰撞检测设置为ContinuousContinuous Dynamic
有时对于快速移动的游戏对象,在这些场景中不会检测到碰撞,您可以使用Raycasting进行碰撞检测这里是 DontGoThrough 脚本,它使用光线投射进行碰撞检测

      using UnityEngine;    
      using System.Collections;

        public class DontGoThroughThings : MonoBehaviour        
        {

            public LayerMask layerMask; //make sure we aren't in this layer

            public float skinWidth = 0.1f; //probably doesn't need to be changed

            private float minimumExtent;

            private float partialExtent;

            private float sqrMinimumExtent;

            private Vector3 previousPosition;

            private Rigidbody myRigidbody;

            //initialize values

            void Awake()

            {

               myRigidbody = rigidbody;

               previousPosition = myRigidbody.position;

               minimumExtent = Mathf.Min(Mathf.Min(collider.bounds.extents.x, collider.bounds.extents.y), collider.bounds.extents.z);

               partialExtent = minimumExtent * (1.0f - skinWidth);

               sqrMinimumExtent = minimumExtent * minimumExtent;

            }

            void FixedUpdate()

            {

               //have we moved more than our minimum extent?

               Vector3 movementThisStep = myRigidbody.position - previousPosition;

               float movementSqrMagnitude = movementThisStep.sqrMagnitude;   

               if (movementSqrMagnitude > sqrMinimumExtent)

                {

                  float movementMagnitude = Mathf.Sqrt(movementSqrMagnitude);

                  RaycastHit hitInfo;



                  //check for obstructions we might have missed

                  if (Physics.Raycast(previousPosition, movementThisStep, out hitInfo, movementMagnitude, layerMask.value))

                     myRigidbody.position = hitInfo.point - (movementThisStep/movementMagnitude)*partialExtent;

               }      

               previousPosition = myRigidbody.position;

            }

        }
于 2014-11-01T11:01:40.983 回答