Question by
Forbidden_Duck · Jan 29, 2017 at 02:36 AM ·
gameobjecttimeballtime.deltatimespeed up
Making someone happen after x amounts of seconds and only once
Hi, I am working on a game which is a remake of pong but after 20 seconds I want the ball to speed up. I have no idea how to do this BUT! This is the script for my Ball
using System; using System.IO; using UnityEngine; using UnityEngine.SceneManagement;
public class Ball : MonoBehaviour { public float speed = 12;
void Start()
{
// Initial Velocity
GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
}
float hitFactor(Vector2 ballPos, Vector2 racketPos,
float racketHeight)
{
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;
}
if (col.gameObject.name == "WallLeft")
{
SceneManager.LoadScene("RightSide Scene");
}
if (col.gameObject.name == "WallRight")
{
SceneManager.LoadScene("LeftSide Scene");
}
}
}
Comment