Hello there. So I am trying to make a script that’s basically like this : There’s an object that waits a random amount of seconds (from 2-7) and then teleports around to a bunch of other objects (Let’s say 4). There’s also one specific object that if it touches, it will stop teleporting until that specific object is removed.
I have been trying for days now to make a script like this, but I suck at scripting. And I don’t mean just suck by this forum’s standards. There are some people here that claim they suck, but I think they’re really good, at least when compared to me. I mean I really, really suck. Can anyone tell me how to script something like this? Please treat me like an idiot if you want.
Half of your post is about how you suck. Might want to get some counselling, man.
Beyond that, do some programming tutorials.
Anyway:
using UnityEngine;
using UnityEngine.Extensions;
using System.Collections.Generic;
public class Teleporter : MonoBehaviour {
Timer teleport;
Target currentTarget;
List<Target> targets = new List<Target>();
void Start () {
teleport = new Timer(RandomValueBetween2And7());
targets.AddRange(GameObject.FindObjectsOfType<Target>());
}
float RandomValueBetween2And7() {
return Random.Range (2f,7f);
}
void Update () {
if (teleport.Expired && !currentTarget && targets.Count > 0) {
int nextTargetIndex = Random.Range(0, targets.Count - 1);
currentTarget = targets[nextTargetIndex];
transform.position = currentTarget.transform.position + Vector3.up;
targets.Remove(currentTarget);
teleport.Set(RandomValueBetween2And7());
}
}
}
And for the sake of example, the teleportation target, once the teleporter is near, slowly starts to get redder and redder until it’s destroyed. Then the teleporter can teleport to a new target until it runs out of targets:
using UnityEngine;
using UnityEngine.Extensions;
using System.Collections;
public class Target : MonoBehaviour {
static Teleporter teleporter;
Timer destructionTimer;
void Start() {
if (!teleporter)
teleporter = GameObject.FindObjectOfType<Teleporter>();
}
void Update () {
if (teleporter && Vector3.Distance(teleporter.transform.position, transform.position) < 2f && destructionTimer == null)
destructionTimer = new Timer(4f);
if (destructionTimer != null) {
if (destructionTimer.Expired)
Destroy (gameObject);
renderer.material.color = Color.Lerp (Color.white, Color.red, 1f - destructionTimer.PercentRemaining);
}
}
}
I didn’t comment it, but it should be pretty easy to follow. If you have any questions, let me know.
Note that these both use a Timer class from a custom namespace:
using UnityEngine;
using System.Collections;
namespace UnityEngine.Extensions {
public class Timer {
float targetTime;
float setTime;
public bool Expired {
get {
return (Time.time >= targetTime);
}
}
public float Remaining {
get {
if (Expired) {
return 0f;
} else {
return targetTime - Time.time;
}
}
}
public float PercentRemaining {
get{
return Remaining/setTime;
}
}
public float SetTime {
get {
return setTime;
}
}
public Timer (float amount) {
Set(amount);
}
public void Set(float amount) {
setTime = amount;
if (setTime <= 0) {
throw new System.Exception("Timers cannot be set with negative amounts.");
}
targetTime = Time.time + setTime;
}
public void Reset(){
Set (setTime);
}
public bool ExpireReset(){
if (Expired) {
Reset();
return true;
}
return false;
}
public bool ExpireSet(float amount){
if (Expired) {
Set (amount);
return true;
}
return false;
}
}
}