Hello Everyone,
In my game, I am rotating an object around a pivot.
When my mouse move clockwise, the gameobject will also move clockwise.
However, when I mouse click and move, the object immediately move to the mouse position.
What I want is,
when I mouse click and move, the object does not move immediately to the mouse position,
and will move clockwise or anti-clockwise, from that spot.
This is the code,
using UnityEngine;
using System.Collections;
public class playerController : MonoBehaviour {
public Transform target;
public float fRadius = 3.0f;
public float deltaAngle;
public Vector3 startTransform;
public Quaternion endRotation;
public Vector3 touchPoint;
public float ang;
public float angleDifference;
public Vector3 v;
void Update () {
if (Input.GetMouseButtonUp(0))
{
endRotation.z = transform.eulerAngles.z;
}
if (Input.GetMouseButtonDown(0))
{
touchPoint = Camera.main.WorldToScreenPoint (target.position);
touchPoint = Input.mousePosition - touchPoint;
float dx = touchPoint.x - target.position.x;
float dy = touchPoint.y - target.position.y;
deltaAngle = Mathf.Atan2(dy,dx) * Mathf.Rad2Deg;;
startTransform = transform.position;
}
if( (Input.GetAxis("Mouse X")<0) || (Input.GetAxis("Mouse X")>0) )
{
Vector3 pt = Camera.main.WorldToScreenPoint (target.position);
pt = Input.mousePosition - pt;
float dx = pt.x - target.position.x;
float dy = pt.y - target.position.y;
ang = Mathf.Atan2(dy,dx) * Mathf.Rad2Deg;;
angleDifference = deltaAngle - ang;
transform.rotation = Quaternion.Euler (0,0,endRotation.z - angleDifference);
v = Quaternion.AngleAxis (-angleDifference, Vector3.forward) * (Vector3.right * fRadius);
transform.position = target.position - v;
}