如何制作一个你不携带的刚体对象?

时间:2014-11-01 07:36:34

标签: unity3d unityscript rigid-bodies

我有一名玩家能够携带枪支物品进入" 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()

1 个答案:

答案 0 :(得分:1)

对撞机添加到 gameObjects 会阻止对象相互移动,如果它已经有对撞机并且正在通过检查您的设置,如下所示
Rigidbody 是Kinamatic :False,是Triggger :false
将刚体的碰撞检测设置为连续连续动态
有时对于快速移动的游戏对象,在这些场景中你不能检测到碰撞,你可以使用 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;

            }

        }