- Home /
rotation in dragging
I'm planning some applications. In that, I want to drag a gameObject and heading it forward. I uploaded just sample below. http://www015.upp.so-net.ne.jp/kira-kira/test01.html
1.firstly, Click the button on the left top "sub screen". 2.then, drag the red colored object in the small screen on the right bottom.
The red object follows to be heading forward. however, It seems like snapped every 45 degrees.
I want to rotate it more smoothly.
Please help me.
Below is the script on the red object.
var state:boolean = false; private var mainObj; private var orgPos:Vector3; private var noCollision:boolean = true;
function Start(){ mainObj = GameObject.Find("Cylinder01"); }
function OnMouseDown(){ state = true; }
function OnMouseUp(){ state = false; }
function OnTriggerEnter(collision : Collider) { noCollision = false; }
function OnTriggerExit(collision : Collider) { noCollision = true; }
function FixedUpdate () { if(state) { if(noCollision) orgPos = transform.position;
var mainCamera = Camera.allCameras[1];
var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
mainObj.transform.position.x = ray.GetPoint(10).x; //.GetPoint(0.2)
mainObj.transform.position.z = ray.GetPoint(10).z;
if(!noCollision) transform.position = orgPos;
var nowPos = transform.position;
relativePos = nowPos - orgPos;
Debug.Log(relativePos);
transform.rotation.eulerAngles.z = Vector3.Slerp(transform.forward, relativePos, Time.deltaTime).z;
if (Vector3.Distance(relativePos, Vector3(0,0,0)) > 0 )
{
transform.LookAt(transform.position - relativePos);
}
}
}
Thanks a lot.
Answer by Bampf · Sep 14, 2010 at 01:18 PM
Some suggestions.
1) The Slerp is probably doing you no good, because the next FixedUpdate you are just going to overwrite the rotation again.
In fact it might part of the problem, because Time.deltaTime is always going to be close to zero, so you are basically just going to set the rotation to something close to the current forward vector.
Try setting the rotation to the final rotation you desire directly, without any Slerp. Once that's correct you can work on making the transition look nice.
2) This is very minor, but note that the following line:
if (Vector3.Distance(relativePos, Vector3(0,0,0)) > 0 )
is equivalent to the following but not as clear:
if (relativePos.magnitude > 0)
If this was being used many times per frame you could replace that with
if (relativePos.sqrMagnitude > 0) // more efficient since you don't need the actual distance
or for that matter
if ( nowPos != orgPos ) // probably the most efficient
3) I'm wondering if the LookAt call and the setting of the Euler z rotation are interfering with each other. Try just one or the other.
4) General rotation tips:
- Learn quaternions; they are generally superior to Euler angles. You don't have to understand how they work, just treat them as a black box representation of your object's rotation.
- Gizmos.DrawRay and Gizmos.DrawLine are useful for visualizing vectors that you are trying to debug.
Answer by satoshi · Sep 14, 2010 at 03:51 PM
Thank you for your reply.
I have some mistake on my code.
so I simplyfied my code like below.
=========================================================
function FixedUpdate () {
if(state)
{ if(noCollision) orgPos = transform.position;
var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
transform.position.x = ray.origin.x;
transform.position.z = ray.origin.z;
if(!noCollision) transform.position = orgPos;
var nowPos = transform.position;
if (nowPos != orgPos)
{
transform.LookAt(orgPos);
}
}
}
=========================================================
But, I have still same problems of rotation. Please check my latest sample.
http://www015.upp.so-net.ne.jp/kira-kira/test02.html
I think... the position from "ScreenPointToRay (Input.mousePosition)" causes this problem??
Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073205im_/https://answers.unity.com/themes/thub/images/avi.jpg)