为什么我会在统一中得到这个错误?

时间:2017-03-20 12:47:10

标签: c# unity3d physics

所以我制作了一个小游戏,我希望通过点击具有名为" RichPoint" ...的特定标签的点对手,让一个立方体从一个点跳到另一个点......我有这个代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {

    public float jumpMaxDistance;
    public float jumpSpeed;
    private float distance;

    private bool firstFrame = false;
    private Vector3 richPoint;
    public GameObject player;
    private Animation jump;
    private float pBTime;

    void Start () {
        richPoint = transform.position;
        jump = player.GetComponent<Animation> ();

    }
    // Update is called once per frame
    void Update (){
        //Moves the player to the next richPoint.
        if (firstFrame == true){
            if (transform.position != richPoint) {
                transform.position = Vector3.MoveTowards (transform.position, richPoint, Time.deltaTime * jumpSpeed);


            }//executes if the left click is pressed.
        }
        if (Input.GetMouseButtonDown (0)) {

            RaycastHit hit;

            //Get Ray from mouse position.
            Ray rayCast = Camera.main.ScreenPointToRay (Input.mousePosition);

            //Raycast and check if any object is hit.
            if (Physics.Raycast (rayCast, out hit, jumpMaxDistance)) 
                {
                //Raycast and check if any object is hit.
                if (hit.collider.CompareTag ("RichPoint"))
                {
                    richPoint = hit.collider.transform.position;

                    //This finds the distance between the player and richPoint.
                    distance = Vector3.Distance(transform.position,richPoint);
                    pBTime = distance / pBTime;

                    //This plays the Animation depending on tha distance between transform.position and richPoint.
                    jump ["PlayerJump"].time = pBTime;
                    jump.Play ();

                    firstFrame = true;
                }
            }
        }
    }
}

现在......如果我运行游戏并尝试点击碰撞器我会收到此错误... -------------------------------------------------- ---------------------- enter image description here

为什么呢?我做错了什么,我该怎么做才能解决它?

1 个答案:

答案 0 :(得分:2)

似乎pBTime始终为零。因此pBTime = distance / pBTime;会使pBTime成为float.Infinity。有关更多信息,请参阅this

float.Infinity会导致各种错误,例如您遇到的错误。