- Home /
Rotating Rigidbodies
Hi, I'm currently creating a gravity switching puzzle game (or atleast starting to create) and I have gotten to the point where I have my player able to change gravity but I want to be able walk around on the surface the gravity changed to. All I need to do is just rotate to a specific quarternion/quarternion.euler but after multiple days looking around and testing I have reached no where. I attempted various methods and so far have ended up with this
function Update ()
{
if (Input.GetMouseButton(0))
{
var hit : RaycastHit;
var ray : Ray = playerCamera.camera.ViewportPointToRay (Vector3(0.5,0.5,0));
if (Physics.Raycast (ray,hit))
{
var incomingVec = hit.point - transform.position;
var refectVec = Vector3.Reflect(incomingVec, hit.normal);
hitPoint = hit.point;
hitNormal = hit.normal;
targetPos = hit.transform.rotation;
GravityNormal (hitNormal);
GravityRotate ();
}
}
}
function GravityRotate ()
{
var orginRot = transform.rotation;
var targRot = targetPos;
rigidbody.isKinematic = true;
for (var t: float = 0.0; t < 1.0; ){
t += Time.deltaTime;
transform.rotation = Quaternion.Slerp(orginRot, targRot, t);
yield; // return here next frame
}
rigidbody.isKinematic = false;
}
There is a few things in there from other parts of the script (it also has all of my movement and gravity stuff) but this is copy pasted everything to do with my rotating part minus a few debug.log's and comments. It does rotate however once its finished rotating it goes straight back to its original point. As a note I'm using the unity 4.6 beta 17
What is dstRot? After rotating your object using for loop you are setting your transform's rotation to be this dstRot which might be the issue.
Also, does the "yield" statement even work? I'm not into JavaScript that much but shouldn't you be using "StartCoroutine(GravityRotate())" ?
@khenkel: No, that's the correct syntax in UnityScript. In UnityScript Unity automatically uses StartCoroutine when it's an IEnumerator. Also the C# "yield return null;" is just "yield;" in US.
dstRot is the target rotation, I put that there as an attempt to maybe get it to set to that instantly after but that didn't work either. I removed it so others can avoid that confusion (even if it is defined within the script >_<)
Answer by -GenericPath- · Oct 01, 2014 at 01:48 AM
Well the issue was completely opposite to what I thought it was. Firstly I found about Quaternion.LookRotation. And that seems more fitting/easier to use, Secondly I realized the overlying issue was my mousescript was interfering with the rotating. All i do now is just disable the mouse script while it is rotating which was causing the issues with any and all types of rotation. I had it so my player would rotate but the mouselook script was acting strangely so i disabled it and rotation worked perfectly. Here is a simple piece of code for anyone else with the same issue.
mouseScript.enabled = false;
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetTransform.rotation, turnSpeed * Time.deltaTime);
Answer by KEELAN · Sep 30, 2014 at 11:54 AM
You are setting the rotation to dstRot after the loop. This is probably (depending on what dstRot is) causing your problems, the fact that it rotates then gets set back. Just remove that line and it should work.
That line was added there in an attempt to fix the problem, I was thinking that since it rotated to dstRot in the slerp but instantly went back to original position i could just add that and it would fixed it in a roundabout way. But even that didn't fix it. Removing it doesn't change anything. I removed it so others can avoid that confusion (even if it is defined within the script >_<)
Your answer
Follow this Question
Related Questions
make projectiles always hit target 1 Answer
Use Lerp to rotate object 2 Answers
Rotation approximation issues using Quaternion.LookRotation 0 Answers
Basic AI Locked Axis 1 Answer
Player character X rotation going haywire on game start 1 Answer