Why doesn't my asteroid field perfectly move with me?
So I have a script that creates a field of asteroids in the beginning of the game, then as they exit the certain area around the player they warp in front of the player and a new size is applied to them (more coming in the future), but there's one issue sometimes they just get left behind and don't warp, mostly happens when player is traveling with a very high speeds but does also sometimes happen with slow speeds as well. Script 1:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AsteroidManager : MonoBehaviour {
[SerializeField]
private GameObject[] asteroids;
[SerializeField]
private Transform locationWhereToBegin;
public float radius;
[SerializeField]
private float numberOfAsteroids;
void Start()
{
for (int i = 0; i < numberOfAsteroids; i++)
Instantiate(asteroids[Random.Range(0, asteroids.Length)], locationWhereToBegin.position + Random.insideUnitSphere * radius, Random.rotation);
}
}
Script 2:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Asteroid : MonoBehaviour {
AsteroidManager asteroidManager;
[Header("Asteroid Stats")]
public float maxSize;
public float minSize;
void Start()
{
asteroidManager = GameObject.FindGameObjectWithTag("Gamemaster").GetComponent<AsteroidManager>();
}
void OnTriggerExit()
{
float radius = asteroidManager.radius;
float size = Random.Range(minSize, maxSize);
Vector3 newSphereCenter = GameObject.FindGameObjectWithTag("Player").transform.position + GameObject.FindGameObjectWithTag("Player").transform.forward * radius;
Vector3 nextPos;
do
{
nextPos = newSphereCenter + Random.insideUnitSphere * radius;
} while (Vector3.Distance(nextPos, GameObject.FindGameObjectWithTag("Player").transform.position) < radius);
transform.localScale = new Vector3(size, size, size);
transform.position = nextPos;
}
}
Answer by no00ob · Aug 15, 2018 at 07:33 PM
Right now I'm using this as a temporary:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Asteroid : MonoBehaviour {
AsteroidManager asteroidManager;
[Header("Asteroid Stats")]
public float maxSize;
public float minSize;
public GameObject object1;
public GameObject object2;
public float distance;
public float speed = 1;
void Start()
{
asteroidManager = GameObject.FindGameObjectWithTag("Gamemaster").GetComponent<AsteroidManager>();
object1 = gameObject;
object2 = GameObject.FindGameObjectWithTag("Player");
}
void FixedUpdate()
{
distance = Vector3.Distance(object1.transform.position, object2.transform.position);
}
void Update()
{
if (distance > 10000)
{
float radius = asteroidManager.radius;
float size = Random.Range(minSize, maxSize);
Vector3 newSphereCenter = GameObject.FindGameObjectWithTag("Player").transform.position + GameObject.FindGameObjectWithTag("Player").transform.forward * radius;
Vector3 nextPos;
do
{
nextPos = newSphereCenter + Random.insideUnitSphere * radius;
} while (Vector3.Distance(nextPos, GameObject.FindGameObjectWithTag("Player").transform.position) < radius);
transform.localScale = new Vector3(size, size, size);
transform.position = nextPos;
}
}
void OnTriggerExit()
{
float radius = asteroidManager.radius;
float size = Random.Range(minSize, maxSize);
Vector3 newSphereCenter = GameObject.FindGameObjectWithTag("Player").transform.position + GameObject.FindGameObjectWithTag("Player").transform.forward * radius;
Vector3 nextPos;
do
{
nextPos = newSphereCenter + Random.insideUnitSphere * radius;
} while (Vector3.Distance(nextPos, GameObject.FindGameObjectWithTag("Player").transform.position) < radius);
transform.localScale = new Vector3(size, size, size);
transform.position = nextPos;
}
}
Your answer
Follow this Question
Related Questions
Detecting Distance from Object at any polint 1 Answer
Terrain height -> loosing Players GO childs 0 Answers
Applying torque to smoothly stop at a desired rotation - factoring Mass and a Max torque 1 Answer
How to calculate forward/Up/Right without transform, have only vector3? 0 Answers
I got Some Error about how to Transform Postiton Enemy where one point can spawn 2 enemies. 1 Answer