Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tygrec · Mar 23, 2016 at 05:02 PM · c#collider2dlinerenderer

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 :/

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges