0

I was testing out scripts and there was a bug, that output 0 as its Rigidbody.velocity.y only if it goes up (so when the random chance it goes down it doesn't break), until it becomes negative (collision with resitution 1 or mirror bouncy) which then output a nonzero integer, so working as normal. Edit: The temporary solution is just to remove the if statement in if (rb.velocity.y != 0) but i wanna know why does it output 0 Edit2: Maybe its a Unity bug, and i am in Unity 2019.4, so updating might be the solution? So if the "bug" dissapears, im sorry for wasting your time

First code determines the ball, while the second determines the ai

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

public class BallMovement : MonoBehaviour
{
    public float speed;
    public Rigidbody2D rb;
    public Vector3 startPosition;
    public float rampSpeed;
    private float startSpeed;
    public float tempSpeed;
    // Start is called before the first frame update
    void Start()
    {
        startPosition = transform.position;
        startSpeed = speed;
        Launch();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void Launch()
    {
        float x = Random.Range(0,2) == 0 ? -1 : 1;
        float y = Random.Range(0,2) == 0 ? -1 : 1;
        rb.velocity = new Vector2(speed * x, speed * y);
    }
    private void OnCollisionEnter2D ()
    {
        tempSpeed = speed;
        speed += rampSpeed;
        rb.velocity = new Vector2(rb.velocity.x*speed/tempSpeed,rb.velocity.y*speed/tempSpeed);
        GetComponent<AudioBall>().SoundCollision();
    }
    public void Reset()
    {
        rb.velocity = Vector2.zero;
        transform.position = startPosition;
        speed = startSpeed;
        Launch();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public bool isPlayer1;
    public float speed;
    public float moveInput;
    private Rigidbody2D rb;
    public Vector3 startPosition;
    //AI
    [Header("Ball")]
    public GameObject ball;
    private Rigidbody2D rbBall;
    private float sign;
    public bool ActivateAI;
    public bool TurnOnOveride;
    // Start is called before the first frame update
    void Start()
    {
        if (TurnOnOveride == false)
        {
            ActivateAI = PlayMenu.ActivateAI;
        }
        else
        {
            ActivateAI = true;
        }
        startPosition = transform.position;
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if(ActivateAI == false || isPlayer1 == true)
        {
            //this part is just human control. dont mind this
            if (isPlayer1 == true)
            {
                moveInput = Input.GetAxisRaw("Vertical");
            }
            else
            {
                moveInput = Input.GetAxisRaw("Vertical2");
            }
        }
        //AI
        else
        {
            rbBall = ball.GetComponent<Rigidbody2D>();
            sign = Mathf.Sign(rbBall.velocity.y);
            Debug.Log("sign:"+sign);
            if (sign == -1)
            {
                moveInput = -1;
            }
            else
            {
                Debug.Log("rb.y:"+rb.velocity.y);
                if (rb.velocity.y != 0)
                {
                    moveInput = 1;
                }
            }
        }
        
        rb.velocity = new Vector2(rb.velocity.x, moveInput * speed);
    }
    public void Reset()
    {
        rb.velocity = Vector2.zero;
        transform.position = startPosition;
    }
}
Shade
  • 1
  • 3
  • Which one logs `0`, the `sign` or the `rb.velocity.y`? One would expect both I guess – derHugo Dec 30 '20 at 11:28
  • sign is 1, meanwhile rb.velocity.y is 0, sign apprently doesnt make 0 inputs to just result 1 :/ – Shade Dec 30 '20 at 13:12
  • I guess your velocity is something like `0.0001` so the log crops it down to `0` while it is not really exactly `0`. Also the check for `!= 0` or `==0` is dangerous for float values .. see [Flaoting point comparison](https://stackoverflow.com/questions/3874627/floating-point-comparison-functions-for-c-sharp) – derHugo Dec 30 '20 at 13:22
  • oh right yeah i should watch out for == or != 0 statements, but i can see the ball game object, move up = to its horizontal speed, but managing to be 0, its multiplied by a speed variable (5 is the base), so there is no way its +/- 0.000000something – Shade Dec 30 '20 at 14:03
  • @Shade to your latest question, installing a newer major .NET runtime replaces the older one. A newer .NET 4.x runtime replaces any older version. Same for .NET Core 2.x, 3.x, 5.x. Only major versions can be installed side-by-side. – Panagiotis Kanavos Mar 03 '21 at 11:36

0 Answers0