我正在使用Unity进行平台游戏,并使用C#编程。我有一个Ball控制脚本来处理连续弹跳背后的输入和物理。我还有一个BounceTrigger脚本,可以让球停止然后让它再次反弹。我试图与玩家实施重生,应该在最后一个平台上重生,而不是可以破坏他已经反弹的平台。
控球脚本 公共浮动速度= 2;
bool alreadyBounced;
bool boost;
float boostMultiplier;
Vector3 boostVelocityAdd;
int fallY = 10;
BounceTrigger platform;
// Update is called once per frame
void Update ()
{
alreadyBounced = false;
float appliedVelocity = velocity * (boost ? boostMultiplier : 1);
Vector3 direction = Vector3.zero;
if(Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A)) { direction = Vector3.left; rigidbody.AddForce(direction * appliedVelocity, ForceMode.VelocityChange);}
if(Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D)) { direction = Vector3.right; rigidbody.AddForce(direction * appliedVelocity, ForceMode.VelocityChange);}
if(Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W)) { direction = Vector3.forward; rigidbody.AddForce(direction * appliedVelocity, ForceMode.VelocityChange);}
if(Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S)) { direction = -Vector3.forward; rigidbody.AddForce(direction * appliedVelocity, ForceMode.VelocityChange); }
if(Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.A)) { direction = -Vector3.left; rigidbody.AddForce(direction * appliedVelocity, ForceMode.VelocityChange); }
if(Input.GetKeyUp(KeyCode.RightArrow) || Input.GetKeyUp(KeyCode.D)){ direction = -Vector3.right; rigidbody.AddForce(direction * appliedVelocity, ForceMode.VelocityChange); }
if(Input.GetKeyUp(KeyCode.UpArrow) || Input.GetKeyUp(KeyCode.W)) { direction = -Vector3.forward; rigidbody.AddForce(direction * appliedVelocity, ForceMode.VelocityChange); }
if(Input.GetKeyUp(KeyCode.DownArrow) || Input.GetKeyUp(KeyCode.S)) { direction = Vector3.forward; rigidbody.AddForce(direction * appliedVelocity, ForceMode.VelocityChange); }
CheckFalling ();
}
public void CheckFalling(){
if(this.transform.position.y < fallY){
Respawn();
}
}
public void Respawn(){
this.transform.position = lastPlatform.transform.position + Vector3.up;
}
public void Bounce(BounceTrigger platform, float upVelocity) {
var reboteGO = (GameObject) GameObject.FindWithTag ("TextoRebote");
var reboteComp = reboteGO.GetComponent<BounceCounter>();
if(!alreadyBounced)
{
if(!platform.isDestructible){
lastPlatform = platform;
}
Debug.Log("Bounce");
alreadyBounced = true;
reboteComp.AumentarRebote();
float downVelocity = rigidbody.velocity.y;
rigidbody.AddForce(Vector3.up * (-downVelocity + upVelocity), ForceMode.VelocityChange);
ResetBoost();
}
}
public void Boost(float multiplier){
if(!boost){
Debug.Log("Boost");
StartCoroutine(BoostCoroutine(multiplier));
}
}
public void ResetBoost(){
if(boost){
Debug.Log("Reset boost");
boost = false;
Vector3 velocityAdd = rigidbody.velocity;
velocityAdd.x = velocityAdd.x / boostMultiplier * (boostMultiplier - 1);
velocityAdd.z = velocityAdd.z / boostMultiplier * (boostMultiplier - 1);
velocityAdd.y = 0;
rigidbody.AddForce(-velocityAdd, ForceMode.VelocityChange);
}
}
public void RestarVida(){
var vidaGO = (GameObject) GameObject.FindWithTag ("TextoVida");
var vidaComp = vidaGO.GetComponent<LifeCounter>();
vidaComp.RestarVida ();
}
IEnumerator BoostCoroutine(float multiplier){
yield return 0;
yield return new WaitForFixedUpdate();
boost = true;
boostMultiplier = multiplier;
Vector3 velocityAdd = rigidbody.velocity;
Debug.Log(velocityAdd);
velocityAdd.x = velocityAdd.x * (boostMultiplier - 1);
velocityAdd.z = velocityAdd.z * (boostMultiplier - 1);
velocityAdd.y = 0;
rigidbody.AddForce(velocityAdd, ForceMode.VelocityChange);
Debug.Log(velocityAdd);
}
弹跳触发器脚本: ` public float upVelocity = 10;
public bool isDestructible = false;
public virtual void OnTriggerEnter(Collider collider){
collider.GetComponent<BallControl>().Bounce(this, upVelocity);
}
当我编译它时,我得到一个nullReferenceException `
答案 0 :(得分:0)
您有一个全局变量BounceTrigger platform;
而不是BounceTrigger lastPlatform;
,因此代码中的任何位置都不存在lastPlatform
。