Home > Mobile >  GameObject reflecting off another gameObject error
GameObject reflecting off another gameObject error

Time:06-18

I am new at Unity/c# and I wanted to make a pong game. I made this by watching a tutorial on youtube. There is no "error" except the ball doesn't move after touching the player.

This is ball code

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

public class BallCode : MonoBehaviour
{
    private Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();

            
            
    }

    // Update is called once per frame
    void Update()
    {
        Vector2 position = transform.position;
        position.x = position.x - 5.8f * Time.deltaTime;
        transform.position = position;
    }
}

This is ball bounce code:

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

public class BallBounce : MonoBehaviour
{
    private Rigidbody2D rb;
    Vector3 lastVelocity;
    // Start is called before the first frame update
    void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        lastVelocity = rb.velocity;
    }
    private void OnCollisionEnter2D(Collision2D coll)
    {
        var speed = lastVelocity.magnitude;
        var direction = Vector3.Reflect(lastVelocity.normalized, coll.contacts[0].normal);

        
    }

}

And this is player code:

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

public class Player : MonoBehaviour
{
    public float moveSpeed = 4.5f;





    // Start is called before the first frame update
    void Start()
    {
        
    }

   

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

        float vertical = Input.GetAxis("Vertical");
        Vector2 position = transform.position;
        position.y = position.y   moveSpeed * vertical * Time.deltaTime;
        transform.position = position;
    }

   
}

When I play the game the ball will collide with player, but it won't ricochet.

CodePudding user response:

It sounds like the event isn't being called then. Make sure there is a collider on the ball object and make sure that it isn't a "trigger". Trigger colliders use a different event to non-trigger colliders.

Also check that the Ball object actually has the script on it. If the script is not added as a Component on the object (drag it onto it to add it) then it will not be run.

To test my theory, add a Debug.Log("Ball collision registered"); into your OnCollisionEnter2D method in your BallBounce script. If you see it in the console, it is being registered. If not, it isn't and you need to check your collider settings.

CodePudding user response:

Your OnCollisionEnter2D method is not doing anything except set local variables that are quickly discarded. You need to make speed and direction variables of the BallCode or BallBounce class, then set up the BallCode class to use those variables in Update() when determining the motions it makes.

  • Related