The problem is that i cant set the rockets (player) position to the closest Planet when it should landing like in this little flashgame: Run From the Sun - Play on Armor Games
How could I calculate the nearest Planet in help of Distance/Collission?
Here is something that I´v tried but it isnt working:
void Update()
{
Distance = Vector3.Distance(GameObject.Find("Rakete").transform.position, GameObject.Find("Planet(Clone)").transform.position);
if (Aktivierunsvariable == true)
{
if (Distance < 10)
{
GameObject.Find("Rakete").transform.parent = GameObject.Find("Planet(Clone)").transform;//setchild
Flugerlaubt = false;
Aktivierunsvariable = false;
}
if (Distance > 10)
{
Aktivierunsvariable = true;
}
}
//KEYBOARD SPACE (setzt Flugerlaubnis auf true)
if (Input.GetKeyDown("space"))
{
GameObject.Find("Rakete").transform.parent = null;//unchild
Flugerlaubt = true;
}
//Flugvariable (Rakete fliegt los)
if (Flugerlaubt == true)
{
//Rakete fliegt vorwärts
GameObject.Find("Rakete").transform.Translate(Vector3.up * 1 * Time.deltaTime);
}
}
using System.Collections.Generic;
using UnityEngine;
public class FindNearestPlanet : MonoBehaviour
{
// You need to store a list of all of the planets
// If you add them procedurally, you need to update this list each time to add or remove a planet
private List<Transform> planetTransforms;
private Transform targetPlanetTransform; // The current planet we are moving to if we find one
private Transform playerTransform;
private float minDistance = 10f;
private float flyingSpeed = 5f;
// State of player
private bool onPlanet = false;
private bool flying = false;
private bool foundPlanet = false;
private void Update()
{
if ( onPlanet )
{
// You would put the code to rotate the player with the planet in here, ok?
// If you are setting the planet as parent of ship, the ship SHOULD rotate with the planet,
// If the planet is rotating
if ( Input.GetKeyDown( KeyCode.Space ) )
{
// Change state of player
onPlanet = false;
flying = true;
// Detach from the planet
playerTransform.parent = null;
}
}
else if ( flying )
{
if ( !foundPlanet )
{
// If the player is flying but not found a planet, move the ship
// Again, you would have to calculate the x and y speeds based on the angle of your ship when it left the planet
// So this line is just example
playerTransform.Translate( new Vector3( flyingSpeed , flyingSpeed , 0 ) );
// Then we check for the nearest planet
int closestIndex = 0; // Keep track of index in the list of planetTransforms
float shortestDistance = 100; // Used to compare each distance, I put it at 100 to start for simplicity
float currentDistance = 0;
// Loop through the list
for ( int i = 0; i < planetTransforms.Count; i++ )
{
currentDistance = Vector3.Distance( playerTransform.position , planetTransforms[ i ].position );
if ( currentDistance < minDistance && currentDistance < shortestDistance )
{
// if the distance to the current planet in the list is closer than the previous shortestDistance, store the list info
closestIndex = i;
shortestDistance = currentDistance;
// So once we find it we arent checking distance every frame
foundPlanet = true;
}
}
// Once we are done looping, we have the index of the transform with the clostest distance, so we set that
// So our ship can move to that location (targetPlanetTransform)
targetPlanetTransform = planetTransforms[ closestIndex ];
}
else // Otherwise if we found a planet, move the player towards it
{
playerTransform.position = Vector3.Lerp( playerTransform.position , targetPlanetTransform.position , 0.125f );
float distanceToPlanet = Vector3.Distance( playerTransform.position , targetPlanetTransform.position );
// Or whetever distance u like,
// You may have to do a little calculation if you want the ship to go right on the edge of the planet
if ( distanceToPlanet < 0.1f )
{
Vector3 direction = Vector3.Normalize( playerTransform.position - targetPlanetTransform.position );
float radius = targetPlanetTransform.localScale.x / 2;
Vector3 edgeOfCircle = targetPlanetTransform.position + ( direction * radius );
playerTransform.position = edgeOfCircle;
playerTransform.SetParent( targetPlanetTransform );
// Set state
flying = false;
onPlanet = true;
}
}
}
}
}
Thank you very very much, but i’m too inexperienced to adjust the script (to my scene). I’m really new at Unity. I´ve uploudet the project in a zip file: http://www.mediafire.com/file/d5kp6bsy636g3oo/SUNRUN.zip/file.
best regards