- Home /
Other, I solved it.
2D When rotating an object around a pivot, when mouse click, how do I prevent it from moving to the mouse position immediately?
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;
}
Answer by graiderzs · Oct 11, 2015 at 11:21 AM
Okay, great news! I got it to work, after countless hours.
This is the code, for anyone who wants to do the same as me. ^.^
using UnityEngine; using System.Collections;
public class playerController : MonoBehaviour {
public Transform target;
public float fRadius = 3.0f;
public bool canStart;
public bool canPlay;
public float deltaAngle;
public float startRotationZ;
public Quaternion endRotation;
Vector3 touchPoint;
public float ang;
public float angleDifference;
public float lastAngledifference;
public Vector3 v;
private float dx;
private float dy;
private Vector3 pt;
void Start ()
{
startRotationZ = transform.eulerAngles.z;
}
void Update () {
if(Input.GetMouseButtonDown(0))
{
angleDifference = 0;
}
//Controls
//If never touch, stop moving
if (!Input.GetMouseButton(0))
{
canPlay = false;
}
//If touch, can move
if (Input.GetMouseButton(0))
{
canPlay = true;
}
if (Input.GetMouseButtonUp(0))
{
endRotation.z = transform.eulerAngles.z;
lastAngledifference = lastAngledifference + angleDifference;
}
//The rotation
if (canPlay)
{
if ( (Input.GetMouseButtonDown(0)) )
{
//mouse position at start, and calculate angle
touchPoint = Camera.main.WorldToScreenPoint (target.position);
touchPoint = Input.mousePosition - touchPoint;
deltaAngle = Mathf.Atan2(touchPoint.x,touchPoint.y) * Mathf.Rad2Deg;
}
if( ( (Input.GetAxis("Mouse X")<0) || (Input.GetAxis("Mouse X")>0) ) )
{
//mouse position when moving, and calculate angle
pt = Camera.main.WorldToScreenPoint (target.position);
pt = Input.mousePosition - pt;
ang = Mathf.Atan2(pt.x,pt.y) * Mathf.Rad2Deg;
angleDifference = deltaAngle - ang;
v = Quaternion.AngleAxis (lastAngledifference + angleDifference, Vector3.forward) * (Vector3.right * fRadius);
transform.position = target.position - v;
}
}
}
}
If you have a solution you should close the question. Also, dont post it as a new answer to your question. You should convert it to a comment.
Follow this Question
Related Questions
C# Rotate GameObject at Other Point other than Center 3 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Rotate Object 180º OnClick 3 Answers
C# Rotate More than Two GameObjects 1 Answer