How can I avoid the "transform.position assign attempt for 'Ball' is not valid. Input position is { NaN, NaN, NaN }."?

huangapple go评论77阅读模式
英文:

How can I avoid the "transform.position assign attempt for 'Ball' is not valid. Input position is { NaN, NaN, NaN }."?

问题

I'm here to help with the translation. Please provide the text you'd like me to translate.

英文:

I have been trying to code a replica of Atari Breakout in Unity (the game where you bounce a moving ball on screen with a controllable paddle at the bottom to break blocks). In my script for the ball, I added some code to control the angle at which the ball bounced off the paddle based on where the ball was in relation to the paddle when it collided with it. Here is the code:

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

public class Ball : MonoBehaviour
{
    public float speed = 5;
    float deltaX;
    float deltaY;
    double angleConst = 7 * System.Math.PI / 8 / 1.8;

    // Start is called before the first frame update
    void Start()
    {
        transform.position = new Vector2(Random.Range(-6, 6), -1);
        deltaX = Random.Range(-1 * (float)System.Math.Sqrt(speed), (float)System.Math.Sqrt(speed));
        deltaY = (float)System.Math.Sqrt(speed - System.Math.Pow(deltaX, 2));
    }

    void OnTriggerEnter2D(Collider2D col)
    {
        if(col.gameObject.tag == "Player")
        {
            // ball's horizontal position on the paddle
            double positionDiff = GameObject.Find("Paddle").transform.position.x - transform.position.x + 0.9;

            deltaX = (float)System.Math.Sqrt(speed * System.Math.Cos(positionDiff * angleConst));
            deltaY = (float)System.Math.Sqrt(-1 * speed * System.Math.Sin(positionDiff * angleConst));
        }
        else if(col.gameObject.tag == "Horizontal")
        {
            deltaY = -1 * deltaY;
        }
        else if(col.gameObject.tag == "Vertical")
        {
            deltaX = -1 * deltaX;
        }
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        transform.Translate(Vector2.right * deltaX * Time.fixedDeltaTime);
        transform.Translate(Vector2.down * deltaY * Time.fixedDeltaTime);
    }
}

Once I added the Math.Sqrt functions in the reassignments of deltaX and deltaY if the object collided with was the "Paddle," the following error would continuously appear over and over again until the program is stopped once the ball came in contact with the paddle: "transform.position assign attempt for 'Ball' is not valid. Input position is { NaN, NaN, NaN }."

Before I added the square roots, the script worked and no errors showed up. I've read up on the error and came across a lot of people saying it could be a dividing by zero error, but I don't see any potential for that in my script. What is causing these inputs to return as NaN?

答案1

得分: 0

一个快速的修复方法是简单地将传递给Mathf.Sqrt()的值替换为其绝对值,因为如果传递一个负数给它,结果会是NaN,如果我没有记错的话。
然而,我建议,为了未来的开发,研究Unity内置的Vector2Vector3方法和函数,因为你所尝试做的很多事情可以通过现有的方法来简化。Unity Vector2文档Unity Vector3文档

英文:

A quick patch to this would be to simply replace the value passed to Mathf.Sqrt() with its absolute value as giving it a negative number outputs NaN if I am not mistaken.
However I would advise, for future development, looking into Unity's built in Vector2 and Vector3 methods and functions as much of what you are trying to do can be simplified with an a pre-existing method. Unity Vector2 documentation, Unity Vector3 documentation

huangapple
  • 本文由 发表于 2023年7月13日 11:01:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675617.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定