Question by
MikeFParkin · May 14, 2020 at 08:42 AM ·
vector3vectorrotating
Moving a rotating gameObject along its current angle using mouse (Unity2D)
Hi all,
I'm trying to work on a part of my game where an object revolves around another object, depending on where my mouse is. I got that part working 100 percent, but now I've run into a problem with my next part... Not only do I want it to rotate around my object with my mouse, but when I click and drag I want to move the object away in a straight line (relative to the middle object). So basically, I think I want to move it along an angle. If that solution would work, I'm struggling to find the angle it is relative to the main object, and then make sure that when I click and drag it, it only moves along that one angle line...
Here's what I've got so far, it's only for rotating the second object around the first object.
using UnityEngine;
public class PaddleController : MonoBehaviour
{
public Transform orb;
public float radius;
private Transform pivot;
void Start()
{
pivot = orb.transform;
transform.parent = pivot;
transform.position += Vector3.up * radius;
}
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.forward);
}
}
Comment