- Home /
rotating to the current facing position
I am trying to write a script for a hover board.
The problem I'm having is rotating the board to the position the player is facing, and ,
(the board is hovering so there is no drag) moving the board in the correct direction.
Currently the character turns but not to the direction of travel.
var moveX : float = 0.0;
var turnForce : float = 10.0;
function Update () {
moveX = Input.GetAxis ("Horizontal");
rigidbody.AddTorque(Vector3.up * (moveX * turnForce * Time.deltaTime));
// turning but 'sliding' , add relative force to 'push' in new direction
rigidbody.AddRelativeForce(Vector3.right * (moveX * turnForce * Time.deltaTime));
}
Even if I make a vector which I want the object to face, I don't know how to rotate or add torque to face that point.
(this is more of a general comment rather than a question) :
I'm having great difficulty understanding how to rotate my characters in any scripts, with either method (transform or rigidbody).
I just cannot get my head around local space and world space in relation to any of the transform rotation Quaternian Slerp stuff, or the physics rigidbody addTorque stuff.
here is the project : http://www.alucardj.net16.net/unityquestions/hoverboard%201-1a.html
here is the full script : http://www.alucardj.net16.net/unityquestions/ScriptHover1-1a.js
(note: the textures and objects are basic =] , I am just using them to write the script , and for now the rigidbody constraints are on for rotation X and Z)
So I guess the short version of this question would be : how do I turn a rigidbody using torque , to turn and face a certain vector , then stop ?
I have seen a few questions on this 'site appear after I asked this with rotation problems. Is this area equivalent to the 'dark arts' , or is it a case of 'if you don't know and can't figure it out I'm not telling you'. I have seen some smart cookies on this 'site so I am surprised there is no link to a previous decent explanation on : How to rotate a rigid body with addTorque to a specific angle , then stop. : How to rotate a gameObject with transform.rotate to a specific angle , then stop. This is co$$anonymous$$g across as whiny, sry but am just desparate for that 'ahhh' moment when understanding how to rotate objects falls into place. $$anonymous$$any thanks to the smart cookies on this 'site, I have learned alot just by reading through the questions daily :)
Do you really want to do this with physics (torque) or do you want it to just follow the player?
this question is for the Player. I am using a rigidbody for the player so using torque to rotate to angle, then stop turning , so yes please !
I would like to know the transform.rotation method for gameObjects withOut rigidbody's also, but for now and this project, rigidbody and force/torque .
this qn is the movement and player rotation component to a bigger project : http://answers.unity3d.com/questions/230216/why-am-i-getting-a-fruity-effect-from-my-raycast.html the webplayer publish on this shows the Player moving on it's own to match the board angle to the terrain angle. This question adds Player input to movement.
Answer by AlucardJay · Apr 15, 2012 at 08:12 AM
I have sorted the issues related to this old question. The problem I was having was understanding the difference between a transform.right and a Vector3.right
http://unity3d.com/support/documentation/ScriptReference/Vector3-right.html
http://unity3d.com/support/documentation/ScriptReference/Transform-right.html
But the actual issue was more to do with Controlling the rotation accurately using Force and Torque.
Quaternion in general remains a mystery : http://answers.unity3d.com/questions/230505/am-i-using-degrees-radians-rotation-or-quaternion-.html
Answer by DaveA · Mar 22, 2012 at 02:05 AM
Why not just set the transform of the board to match the player? Something like (script on the hover board):
function Update()
{
transform.rotation = player.transform.rotation;
}
Move the board by, for example,
transform.Translate (transform.forward * speed * Time.deltaTime);
With physics (untested)
rigidbody.MoveRotation(player.transform.rotation);
thanks, that shows me a command that can rotate over time to an angle. I shall play with that in some test scripts.
$$anonymous$$y basic understanding was : to get real physics effect (colliding with other objects) I had to use rigidbody's , and manipulate them with force and torque in a FixedUpdate loop. Also using addForce And transform was wrong, and could lead to strange consequences with the physics engine.
I have come across a method using dot products, am trying to absorb and understand that (while answering a post and testing for another question. Lots of help today, much appreciated).
dot product info I am looking at : http://forum.unity3d.com/threads/31420-Left-Right-test-function . this doesn't answer my qn, more chooses a direction to rotate in.
Yeah, I've heard that too, and a lot of people complain there are no apparent ways of tightly controlling when things stop etc. One way would be to hit Physics 101 text books and do the math (eeek). Another is to 'monitor' position and rotation and tweak it, analogous to firing 'retro rockets' to slow and stop things. Looking again at http://unity3d.com/support/documentation/ScriptReference/Rigidbody.$$anonymous$$oveRotation.html I think by 'friction' they mean it applies whatever forces are needed to achieve this rotation. So give that one a try. Editing answer to illustrate.
while replying, just wanted to say thanks. and yes, with the board hovering and there being no drag, I do also need to add 'retro rockets' . That's what I was trying to achieve with the AddRelativeForce statement. Reading the script reference for $$anonymous$$oveRotation .
I found that $$anonymous$$oveRotation 'snaps' to the Quaternion it is given. So I tried to make a target var that is changed with the horizontal input axis over time, but it is giving strange results :
var targetRotAngle : Quaternion = Quaternion.Euler (0, 0, 0);
var turnSpeed : float = 10.0;
function FixedUpdate () {
targetRotAngle = Quaternion.Euler (0, transform.eulerAngles.y, 0);
targetRotAngle.y += Input.GetAxis("Horizontal") * Time.deltaTime * turnSpeed;
rigidbody.$$anonymous$$oveRotation(targetRotAngle);
}
I am messing up and getting more confused with Quaternian .rotation and euler angles .