- Home /
Spin a 2D object with mouse drag
Hello everyone,
I am trying to have a control like in the game in this youtube video and so far i am having issues with transform.rotate, i tried to look on similar questions and so far this is what i've got :
using UnityEngine;
using System.Collections;
public class DragRotateSlowDown : MonoBehaviour
{
private float rotationSpeed = 10.0F;
private float lerpSpeed = 1.0F;
private Vector3 theSpeed;
private Vector3 avgSpeed;
private bool isDragging = false;
void OnMouseDown ()
{
isDragging = true;
}
void Update ()
{
if (Input.GetMouseButton (0) && isDragging) {
theSpeed = new Vector3 (-Input.GetAxis ("Mouse X"), Input.GetAxis ("Mouse Y"), 0.0F);
avgSpeed = Vector3.Lerp (avgSpeed, theSpeed, Time.deltaTime * 5);
} else {
if (isDragging) {
theSpeed = avgSpeed;
isDragging = false;
}
float i = Time.deltaTime * lerpSpeed;
theSpeed = Vector3.Lerp (theSpeed, Vector3.zero, i);
}
transform.Rotate (Camera.main.transform.forward * theSpeed.x * rotationSpeed, Space.World);
transform.Rotate (Camera.main.transform.forward * theSpeed.y * rotationSpeed, Space.World);
}
}
but there is a lot of problems with this solution, mainly, the object doesn't continue to rotate in a certain moment, it "stop" following the mouse and instead it rotate in the opposite direction.
Any help will be heavily appreciate it, thank you all and have a great day.
Answer by Aladine · May 29, 2014 at 12:39 AM
thnx itsa for your help, here is the final code with the wanted effect :-)
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
public float deltaRotation;
public float deltaLimit;
public float deltaReduce ;
float previousRotation;
float currentRotation;
void Start ()
{
Screen.showCursor = false;
}
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
deltaRotation = 0f;
previousRotation = angleBetweenPoints (transform.position, Camera.main.ScreenToWorldPoint (Input.mousePosition));
} else if (Input.GetMouseButton (0)) {
currentRotation = angleBetweenPoints (transform.position, Camera.main.ScreenToWorldPoint (Input.mousePosition));
deltaRotation = Mathf.DeltaAngle (currentRotation, previousRotation);
if (Mathf.Abs (deltaRotation) > deltaLimit) {
deltaRotation = deltaLimit * Mathf.Sign (deltaRotation);
}
previousRotation = currentRotation;
transform.Rotate (Vector3.back * Time.deltaTime, deltaRotation);
} else {
transform.Rotate (Vector3.back * Time.deltaTime, deltaRotation);
deltaRotation = Mathf.Lerp (deltaRotation, 0, deltaReduce * Time.deltaTime);
}
}
float angleBetweenPoints (Vector2 position1, Vector2 position2)
{
Vector2 fromLine = position2 - position1;
Vector2 toLine = new Vector2 (1, 0);
float angle = Vector2.Angle (fromLine, toLine);
Vector3 cross = Vector3.Cross (fromLine, toLine);
// did we wrap around?
if (cross.z > 0) {
angle = 360f - angle;
}
return angle;
}
}
when i change deltarotation value in inspector work great but working with mouse or touch why?!!
Answer by itsa · May 27, 2014 at 09:34 PM
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
public Transform cube;
float deltaRotation;
float previousRotation;
float currentRotation;
void Start ()
{
}
void Update ()
{
if (Input.GetMouseButtonDown (0))
{
deltaRotation = 0f;
previousRotation = angleBetweenPoints( cube.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
else if(Input.GetMouseButton(0))
{
currentRotation = angleBetweenPoints( cube.position, Camera.main.ScreenToWorldPoint(Input.mousePosition) );
deltaRotation = Mathf.DeltaAngle( currentRotation, previousRotation );
previousRotation = currentRotation;
cube.Rotate( Vector3.back, deltaRotation );
}
}
float angleBetweenPoints( Vector2 position1, Vector2 position2 )
{
var fromLine = position2 - position1;
var toLine = new Vector2( 1, 0 );
var angle = Vector2.Angle( fromLine, toLine );
var cross = Vector3.Cross( fromLine, toLine );
// did we wrap around?
if( cross.z > 0 )
angle = 360f - angle;
return angle;
}
}
adapted from TouchKit - TKOneFingerRotationRecognizer
(sorry for the delay) yes it helped a lot :-) i will try to figure out myself how to add a more "realistic" feeling to it (keep rotating a little bit based on how fast the manual rotation was)
Your answer
Follow this Question
Related Questions
How Rotate Object To Resemble Spinning? 1 Answer
2D wheel spin 2 Answers
2D LookAt not working as intended 1 Answer
Pysics not working as expected 1 Answer
how to rotate 2d obj on android 1 Answer