Need help please,
Had no issue with the previous lectures, but I’m getting a bug on this one… or I’m doing something wrong… and I don’t know how to fix it. I also went back to follow Rick’s steps and refactored my code to look more like his… but it’s still happening.
The issue is that, when I implemented the Ball following the Paddle - with the “position + offset” it’s nicely following the Paddle… for a couple seconds… after which it triggers the Game Over collider trigger… although the Ball never goes below the Paddle. So I can’t really figure our how it can trigger something it never touched. Please help.
I’m sending my scripts and screenshot in which you can see Ball, Trigger Collider, and Debug Message. I noticed this since it would keep sending me into the Game Over scene… that’s why it’s commented out currently with the Debug.Log messege added.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Paddle : MonoBehaviour
{
[SerializeField]
private float screenWidthInUnits = 11.7f;
void Update()
{
UpdatePaddlePosition();
}
public void UpdatePaddlePosition() {
float mousePositionInUnits = Input.mousePosition.x / Screen.width * screenWidthInUnits;
Vector2 currentPosition = new Vector2(transform.position.x, transform.position.y);
currentPosition.x = Mathf.Clamp(mousePositionInUnits, 0f, screenWidthInUnits);
transform.position = currentPosition;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoseCollider : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision) {
Debug.Log("GameOver");
Debug.Log(collision);
//SceneManager.LoadScene("GameOver");
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallBehaviour : MonoBehaviour
{
[SerializeField] Paddle paddle1;
private Vector2 paddleToBallVector;
private void Start() {
CalculateOffsetToPaddle();
}
private void Update() {
FollowPaddle();
}
private void CalculateOffsetToPaddle() {
paddleToBallVector = transform.position - paddle1.transform.position;
}
private void FollowPaddle() {
Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
transform.position = paddlePos + paddleToBallVector;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneLoader : MonoBehaviour
{
public void LoadNextScene () {
int sceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(sceneIndex + 1);
}
public void LoadMenuScene() {
SceneManager.LoadScene(0);
}
public void QuitApplication() {
Application.Quit();
}
}