How to make another ground spawn beneath itself
Hey there, I'm trying to make a infinite ball rolling game, where a ball lands on a slanted platform, triggering another one to spawn beneath it, then keeps going forever. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FloorSpawner : MonoBehaviour {
public GameObject Ground;
public Transform Location;
void OnTriggerEnter (Collider other) {
if (other.gameObject.CompareTag ("Player")) {
Instantiate (Ground, new Vector3 (0, -22.9f, 0), Location.rotation);
}
}
}
The "Y" coordinate, "-22.9f", I want to be in relation of the previous ground object (plane). Instead, it's in relation to the scene, placing it at 0, -22.9, 0. How do I make it in relation to the ground my player is touching?
This is what i want the game to look like: https://imgur.com/0eqXvnc
Answer by tormentoarmagedoom · May 22, 2018 at 09:58 PM
I don't get exactly you want, but you are asking how to position a object relative to another object. So you need this:
If i have Object A at position (AX, AY, AZ) And i want the Object B to be for example, BY units under object A
TheVector3PositionYouWant = (AX, AY-BY, AZ)
so, you need to have in a variable the position of the first platform. I supose this script we see is inside the first platform, so you need to do this:
Instantiate (Ground, new Vector3 (transform.position.y-0, transform.position.y-22.9f,transform.position.z-0), Quaternion.identity);
Aaaand just as a tip: Is a very good practice, (always if possible) to have round numbers coordinates, so when you check/read your code will be much more easy and faster to read. So put the object at 23, not 22.9 :D
Byeee :!D
Just for clarification, what does the "A" and "B" represent in the "AX, AY-BY, AZ"?