- Home /
Question by
UniluckStudios · Aug 10, 2017 at 10:30 PM ·
transformtriggerrandomrandom.rangeteleport
Transform to a random height
How would I make an object teleport to a set x position but a random y position? I have a code but I don't know how to make unity generate another random number whenever the gameobject collides with another gameobject. In my script, the objects would get a random position, but when they collide again, they stay at that position instead of going to another random position. Here is code::
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Teleport : MonoBehaviour {
public GameObject teleportto;
float x;
float y;
float z;
Vector2 pos;
// Use this for initialization
void Start () {
x = -5;
y = Random.Range(0, 7);
pos = new Vector2(x, y);
}
// Update is called once per frame
void Update () {
Debug.Log (this.transform.position);
}
void OnTriggerEnter2D (Collider2D col) {
if (col.gameObject.tag == "teleport") {
this.transform.position = pos;
}
}
}
Comment