Velocity Tweak question

Hi. I was fixing the boring ball problem in the method shown by Rick and Ben. But in the lecture they have take the variable myRigidBody and then accessed velocity through it. I, however, created a cached referenced Vector2 variable named myRigidBodyVelocity and it was equal to GetComponent<Rigidbody2D>().velocity;
Then in the the method that launches the ball when mouse is clicked, instead of GetComponent<Rigidbody2D>().velocity = new Vector2(xPush,yPush); I typed GetComponent<Rigidbody2D>().velocity = new Vector2(xPush,yPush);

But now when I clcik on the mouse the Ball breaks the connection with the paddle (as expected), but it isnt launched (i.e. its celocity doesnt change). I would be very happy to understand the reason of this bug.

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

public class Ball : MonoBehaviour
{
    
    [SerializeField] Paddle paddle1;
    [SerializeField] float xPush = 2f;
    [SerializeField] float yPush = 15f;
    [SerializeField] AudioClip[] ballSounds;
    [SerializeField] float randomFactor = 0.2f;



    Vector2 paddleToBall;
    bool hasStarted; 

    Vector2 myRigidBodyVelocity ;





    void Start()
    {
        paddleToBall = transform.position - paddle1.transform.position;  
        hasStarted = false;
        myRigidBodyVelocity = GetComponent<Rigidbody2D>().velocity;
    }

    // Update is called once per frame
    void Update()
    {
        if(hasStarted == false)
        {
            LockBallToPaddle();
        }
        LaunchBallOnClick();


    }

    private void LaunchBallOnClick()
    {
        if(Input.GetMouseButtonDown(0))
        {
           myRigidBodyVelocity = new Vector2(xPush,yPush);
            hasStarted = true;
        }

    }
    private void LockBallToPaddle()
    {
        Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
        transform.position = paddlePos + paddleToBall;
    }

    /*private void OnCollisionEnter2D(Collision2D collision)
    {
        if(hasStarted == true)
        {
            AudioClip clip = ballSounds[UnityEngine.Random.Range(0,ballSounds.Length)];
            GetComponent<AudioSource>().PlayOneShot(clip);
                                                          
        }
    }*/
}

Hi Maksimjeet,

I think the problem is the following: Vector2 is a value type. This means that myRigidBodyVelocity you directly access the object. Rigidbody2D is a reference type which means that GetComponent<Rigidbody2D>() returns a reference, a “link” to an object.

With myRigidBodyVelocity = GetComponent<Rigidbody2D>().velocity;, your code assigns a new Vector3 object to the Vector2 variable, not a reference to the same object which is assigned to velocity.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? If so, did you notice what Rick did in his code?


See also:

How are you getting on with this, @Maksimjeet_Chowdhary?


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms