我有一个玩家可以在“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()