I’m creating a 3d unity project that allows the user to change the values of the planets of the solar system, and observe the different planets. I’m planning to use a dropdown that has the 8 planets, and I’ve already got a camera script that changes the view of the camera to focus on whatever planet the user wants to focus on.
How do i make it so that the value of the selected dropdown can be linked to the target object in the camera? e.g., if Mars is selected in the dropdown, the camera knows to focus on the planet Mars.
Code for the physics:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OrbitSimulator : MonoBehaviour
{
readonly float G = 10f;//Artificial value of G is used to speed up orbit simulation.
GameObject[] Planets;
// Start is called before the first frame update
void Start()
{
Planets = GameObject.FindGameObjectsWithTag("Planet");//Identifies every object that has the tag "Planet", and applies calculations to those bodies.
InitialVelocity();//Applies InitialVelocity function of the code. This is re-calculated and reuploaded once a frame.
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
Gravity();//Applies Gravity function of the code. This is re-calculated and reuploaded once a frame.
}
void Gravity()
{
foreach (GameObject a in Planets)
{
foreach (GameObject b in Planets)
{
if (!a.Equals(b))
{
float Mass1 = a.GetComponent<Rigidbody>().mass;
float Mass2 = b.GetComponent<Rigidbody>().mass;
float Distance = Vector3.Distance(a.transform.position, b.transform.position);//Uses set radius of planets to calculate distance between all the planets and the Sun.
a.GetComponent<Rigidbody>().AddForce((b.transform.position - a.transform.position).normalized * (G * (Mass1 * Mass2) / (Distance * Distance)));
//Uses F = GM1M2 / r^2 to calculate force between each planet on each other and the Sun
}
}
}
}
void InitialVelocity()
{
foreach (GameObject a in Planets)
{
foreach (GameObject b in Planets)
{
if (!a.Equals(b))
{
float Mass2 = b.GetComponent<Rigidbody>().mass;//Calls mass of planet
float Distance = Vector3.Distance(a.transform.position, b.transform.position);//Uses set radius of planets to calculate distance between all the planets and the Sun.
a.transform.LookAt(b.transform);
a.GetComponent<Rigidbody>().velocity += a.transform.right * Mathf.Sqrt((G * Mass2) / Distance);
// Uses InitialVelocity equation to calculate velocity of planets around each other and the Sun.
//After each calculation, the new velocity is added to the old velocity.
}
}
}
}
}
Code for camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollowPlanet : MonoBehaviour
{
public Transform targetObject;
public Vector3 cameraOffset;
public bool lootAtPlanet = true;
// Start is called before the first frame update
void Start()
{
cameraOffset = transform.position - targetObject.transform.position;
}
void LateUpdate()
{
Vector3 newPosition = targetObject.position + cameraOffset;
transform.position = newPosition;
if ( lootAtPlanet )
{
transform.LookAt(targetObject);
}
}
}
Code for dropdown:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class DropdownValue : MonoBehaviour
{
[SerializeField] private TMP_Dropdown dropdown;
[SerializeField] private List<TMP_Dropdown.OptionData> options = new List<TMP_Dropdown.OptionData>();
public void GetDropdownValue()
{
int pickedEntryIndex = dropdown.value;
string selectedOption = dropdown.options[pickedEntryIndex].text;
Debug.Log(selectedOption);
}
[ContextMenu("Add New Planet")]
private void AddNewPlanet()
{
dropdown.options.Add(new TMP_Dropdown.OptionData("Mercury"));
dropdown.options.Add(new TMP_Dropdown.OptionData("Venus"));
dropdown.options.Add(new TMP_Dropdown.OptionData("Earth"));
dropdown.options.Add(new TMP_Dropdown.OptionData("Mars"));
dropdown.options.Add(new TMP_Dropdown.OptionData("Jupiter"));
dropdown.options.Add(new TMP_Dropdown.OptionData("Saturn"));
dropdown.options.Add(new TMP_Dropdown.OptionData("Uranus"));
dropdown.options.Add(new TMP_Dropdown.OptionData("Neptune"));
dropdown.AddOptions(options);
dropdown.RefreshShownValue();
}
}