Question by
m_Thesaurusrex · Nov 16, 2015 at 12:21 AM ·
c#2dmousepositiontopdownorbit
Orbit position controlled by mouse 2D (C#)
I have a script that makes an object orbit perfectly the way I want it to in a top-down 2D game. However, I want the position the object is in to be controlled by the mouse without leaving the orbit-track instead of constantly moving the way it is now. I feel like there is just one thing I need to change and it would work perfectly but I can't seem to figure out what that is. I tried changing deiredPosition to Input.mouseposition but that didn't do anything. I'm just not seeing the obvious here I think but it is driving me crazy.
Here is the code I slightly edited from a script I found on here
void Start ()
{
Player = GameObject.Find ("Player");
OrbitCenter = Player.transform;
OrbitAxis = Vector3.forward;
transform.position = (transform.position - OrbitCenter.position).normalized * OrbitRadius + OrbitCenter.position;
OrbitRadius = 2.0f;
}
void Update ()
{
transform.RotateAround (OrbitCenter.position, OrbitAxis, OrbitSpeed * Time.deltaTime);
desiredPosition = (transform.position - OrbitCenter.position).normalized * OrbitRadius + OrbitCenter.position;
transform.position = Vector3.MoveTowards(transform.position, desiredPosition, Time.deltaTime * radiusSpeed);
}
Comment