- Home /
Object follows a moving ellipse
Hello everyone.
I'm currently making a program where player has to reconstruct solar system. For this, there are space objects following ellipses (except for sun) and ellipses surrounding spaces objects.
My problem is, placing earth on his ellipse is working well, but after that placing moon on the ellipse surrounding earth makes strange behaviours. Sometimes it will follow the ellipse well, and sometimes just move around the sun or around earth but not on the line.
Here is my class managing space objects :
using UnityEngine;
using System.Collections;
using System;
public class ObjectManager : MonoBehaviour {
public Ellipse ellipse;
private int posOnEllipse = 0;
private int speed = 2;
private Vector3 screenPoint;
private Vector3 offset;
public bool isPositioned;
public bool stop;
private string namePosition;
void Awake()
{
//Only sun doesn't move
if (ellipse != null)
namePosition = ellipse.name;
else
namePosition = "SunSpot";
}
void Update()
{
if (name != "Sun" && stop)
FollowEllipse();
}
void FollowEllipse()
{
float x;
float y;
float z = 0.0f;
//get the next position of ellipse collider
x = ellipse.GetPoints()[posOnEllipse].x;
y = ellipse.GetPoints()[posOnEllipse].y;
if (posOnEllipse < ellipse.GetPoints().Length - speed)
posOnEllipse += speed;
else
posOnEllipse = 0;
//set object on the next position of ellipse collider
transform.position = new Vector3(x, y, z);
}
void OnMouseDown()
{
if (!stop)
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
}
void OnMouseDrag()
{
if (!stop)
{
Vector3 cursorPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
transform.position = cursorPosition;
}
}
//Check if space object is on the ellipse
void OnMouseUp()
{
if (isPositioned)
{
stop = true;
if (ellipse != null)
transform.position = new Vector2(ellipse.GetPoints()[0].x, ellipse.GetPoints()[0].y);
}
}
void OnTriggerExit2D(Collider2D other)
{
isPositioned = false;
}
}
Here is my class managing ellipses :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Ellipse : MonoBehaviour
{
public ObjectManager spaceObject; //planet or moon that follow the eclipse
public ObjectManager center; // planet or sun that is the center of the ellipse
public int segments;
public float xradius;
public float yradius;
LineRenderer line;
private List<Vector2> points;
private EdgeCollider2D edgeCollider;
private bool isDraw;
public Vector2[] GetPoints() { return edgeCollider.points; }
void Start()
{
points = new List<Vector2>();
edgeCollider = GetComponent<EdgeCollider2D>();
line = gameObject.GetComponent<LineRenderer>();
}
void Update()
{
transform.position = center.transform.position;
if (!isDraw && center.stop) //display ellipse when center is set
{
InitEllipse();
isDraw = true;
}
}
void CreatePoints()
{
float x;
float y;
float angle = 20f;
for (int i = 0; i < (segments + 1); i++)
{
x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
y = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;
points.Add(new Vector2(x, y));
line.SetPosition(i, points[i]);
angle += (360f / segments);
}
}
void InitEllipse()
{
line.SetColors(Color.white, Color.white);
Material whiteDiffuseMat = new Material(Shader.Find("Unlit/Texture"));
line.material = whiteDiffuseMat;
line.SetVertexCount(segments + 1);
line.useWorldSpace = false;
CreatePoints();
edgeCollider.points = points.ToArray(); //Collider on the line
}
void OnTriggerStay2D(Collider2D other)
{
if (spaceObject.gameObject.Equals(other.gameObject) && !spaceObject.isPositioned)
spaceObject.isPositioned = true;
}
}
I don't get how can the space object don't follow the TriggerCollider whereas it detects collision perfectly.
Thanks for help, and sorry for my bad english :/
Your answer
![](https://koobas.hobune.stream/wayback/20220612073019im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# Reset EdgeCollider2D.Points.Length and LineRenderer.Positions.Length to 0 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Generate a mesh around a LineRenderer 2 Answers
Filling area under positions with Mesh 0 Answers