Sprite Orbiting Sprite with Mouse
I've had this issue for a while now, and I've tried different solutions but i cannot figure this out. I have a 2D game which i have two sprites. The first sprite is the player object and the second is an orb that is a child of the first, so when the player moves, the orb moves. I would like to have the orb rotate around the player according to where the mouse position is related to the player. Billboarding is not an option (or at least one I've found) because I have several other scripts set up which cause complications.
The first solution I tried is this:
public class LimbSlide2 : MonoBehaviour
{
public Camera mainCamera;
public Transform pivot;
public Transform orb;
public float radius;
// Use this for initialization
void Start()
{
pivot = this.transform;
//transform.position += Vector3.up * radius;
}
// Update is called once per frame
void Update()
{
Vector3 orbVector = Camera.main.WorldToScreenPoint(orb.position);
orbVector = Input.mousePosition - orbVector;
float angle = Mathf.Atan2(orbVector.y, orbVector.x) * Mathf.Rad2Deg;
pivot.position = orb.position;
pivot.rotation = Quaternion.AngleAxis(angle - 90, Vector3.up);
//or
//transform.rotation = Quaternion.AngleAxis(angle-90,Vector3.up);
}
which needs to be attached to a parent object and affects the orbs but also affects the parent (in this case the player) and resulted in this: https://imgur.com/a/gAcX0tm. The issue with this is it rotates the player sprite as well as the orb sprite, flattening them out and then expanding again.
Solution 2 was closer, however I am not able to control the direction of the rotation with the mouse, it just rotates at a constant speed.
public class Target : MonoBehaviour
{
public float Speed;
private float _angle;
public Transform orb;
public static Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Quaternion angle)
{
return angle * (point - pivot) + pivot;
}
private void Start()
{
}
private void Update()
{
Vector3 orbVector = Camera.main.WorldToScreenPoint(orb.position);
orbVector = Input.mousePosition - orbVector;
float _angle = Mathf.Atan2(orbVector.y, orbVector.x)* Mathf.Rad2Deg;
transform.position = RotatePointAroundPivot(transform.position, transform.parent.position, Quaternion.Euler(0,_angle,0));
}
}
Resulting in: https://imgur.com/a/bGlkR8I Which is closer to what I need because both sprites are facing the camera, however I can't control the rotation with the mouse. Any ideas?
Your answer
Follow this Question
Related Questions
How do I outline sprites hidden behind another sprite? 0 Answers
How to make specific 2D-sprites in unity, Trouble with sprites 0 Answers
[Help] Merging multiple sprites to create a long single background 0 Answers
Extend arrow sprite 0 Answers
Sprite shaking/ jittering/ jiggling/ wiggling/ vibrating when moving it using joystick (2D). 0 Answers