【问题标题】:Why do i get this errors in unity?为什么我会在统一中得到这个错误?
【发布时间】:2017-03-20 12:47:10
【问题描述】:

所以我做了一个小游戏,我想通过单击带有名为“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;
                }
            }
        }
    }
}

现在...如果我运行游戏并尝试单击对撞机,我会收到此错误... -------------------------------------------------- ----------------------

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

【问题讨论】:

  • 请将错误/堆栈跟踪的实际文本复制并粘贴到您的问题中。这将使搜索该错误的其他人能够找到此问题及其答案。

标签: c# unity3d physics


【解决方案1】:

似乎pBTime 总是为零。所以pBTime = distance / pBTime; 使pBTime 变成float.Infinity。请参阅this 了解更多详情。

这个float.Infinity 会导致你遇到的各种错误。

【讨论】:

    猜你喜欢
    • 2016-11-25
    • 2017-09-18
    • 1970-01-01
    • 2019-01-25
    • 2018-10-06
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多