Question by
Forbidden_Duck · Jan 25, 2017 at 08:51 AM ·
scripting problemprefabballboxcollider2dwall collision
I need help when 'Ball' Hits 'WallLeft'
Hi,
I am trying to make it when my Ball hits the Left Wall that it changes the scene.
LEFT WALL SCRIPT
using UnityEngine; using UnityEngine.SceneManagement;
public class RightSideScene : MonoBehaviour { public bool startedLoad;
public void TriggerEvent (BoxCollider2D other)
{
if (other.gameObject.tag == "Ball")
{
SceneManager.LoadScene("RightSide Scene");
startedLoad = true;
}
}
}
THIS IS BALL SCRIPT
using System; using System.IO; using UnityEngine;
public class Ball : MonoBehaviour { public float speed = 30;
void Start()
{
// Initial Velocity
GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
}
private void TimeScale()
{
throw new NotImplementedException();
}
float hitFactor(Vector2 ballPos, Vector2 racketPos,
float racketHeight)
{
// ascii art:
// || 1 <- at the top of the racket
// ||
// || 0 <- at the middle of the racket
// ||
// || -1 <- at the bottom of the racket
return (ballPos.y - racketPos.y) / racketHeight;
}
void OnCollisionEnter2D(Collision2D col)
{
// Note: 'col' holds the collision information. If the
// Ball collided with a racket, then:
// col.gameObject is the racket
// col.transform.position is the racket's position
// col.collider is the racket's collider
// Hit the left Racket?
if (col.gameObject.name == "RacketLeft")
{
// Calculate hit Factor
float y = hitFactor(transform.position,
col.transform.position,
col.collider.bounds.size.y);
// Calculate direction, make length=1 via .normalized
Vector2 dir = new Vector2(1, y).normalized;
// Set Velocity with dir * speed
GetComponent<Rigidbody2D>().velocity = dir * speed;
}
// Hit the right Racket?
if (col.gameObject.name == "RacketRight")
{
// Calculate hit Factor
float y = hitFactor(transform.position,
col.transform.position,
col.collider.bounds.size.y);
// Calculate direction, make length=1 via .normalized
Vector2 dir = new Vector2(-1, y).normalized;
// Set Velocity with dir * speed
GetComponent<Rigidbody2D>().velocity = dir * speed;
}
}
}
What Do I need to put int he Ball Script to make the Scene Change when it hits the Left Wall
Comment