How to make object fall loop?
I have 2 game objects Ball 1 and Ball 2. Ball 1 is static and on the ground.Ball 2 is in the air due to greater y value and not in the camera view due to greater x value. What I want is when game starts Ball 2 should move along -x axis. Now if a button is pressed, Ball 2 should stop moving along x axis and start moving along -y axis i.e.it should fall.On fall if it collides with Ball 1 on the ground a new Ball 2 should instantiate on the same x, y axis and float value as the start of the game.Now the fallen Ball 2 should become Ball 1 so that if new instantiated Ball 2 collides with it on button press, another Ball 2 should instantiate and so on becoming a loop.
The problem I have is that when Ball 1 fells it does not fell straight down, so I made float value to be zero when button is clicked.Now new instantiated Ball 2 also has float value 0 and it does not move along y axis. I tried to fix this problem but couldn't. Once somehow I fixed a bit but it instantiated so many balls and crashed unity. I don't know much about prefabs should I create prefabs or not? My script to instantiate was on the Ball 2 but after crash I transferred it to Ball 1. What's the solution? My one and only script on Ball 1:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ballOne : MonoBehaviour
{
public GameObject balltwo;
public Rigidbody2D rigidBody;
public float speed;
// Use this for initialization
void Start()
{
speed = 5;
}
// Update is called once per frame
void Update()
{
balltwo.transform.Translate(-speed * Time.deltaTime, 0, 0);
if (Input.GetKey(KeyCode.DownArrow))
{
rigidBody.gravityScale = 1;
}
if (rigidBody.gravityScale == 1)
{
speed = 0;
}
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "BallOne")
{
Instantiate(balltwo, new Vector2(transform.position.x + 6, transform.position.y + 2), transform.rotation);
collision.gameObject.tag = "BallOne";
}
}
}
Your answer
Follow this Question
Related Questions
How to know when the player object falls between tiles in 2D game to take decesion of gameover 1 Answer
Destroying instantiated game objects from prefab when coliding with the ground 1 Answer
Unable to stop animation upon collision (2d, animation, rigidbody) 0 Answers
Unity Freezing after for loop 1 Answer