- Home /
How to rotate a Rigidbody without rotating the gameObject?
Alright so lets keep this short n sweet.
I need to do something like the ball movement in the game Kula World, shown below (but with free mouse X,Y look movement) So when I look to the right, that automatically becomes the new "forward" direction.
I'm still pretty new to Unity, C# and JS in general but if anyone knows how I can edit the stock import scripts for the camera movement or any other easier method to do this, the help would be much appreciated :)
Answer by duck · Jun 17, 2015 at 08:40 AM
This depends if you want the movement to be locked to one of the 6 axis directions (like kula world), or want truly free movement in any direction.
For free movement, you can use the camera's transform.forward property, and move the ball along that. If you're using physics, you could either apply a force in that direction, or if you want to apply torque you'd need to calculate the perpendicular vector to apply torque around. To do this, you'd use Vector3.Cross and feed in the camera forward vector and the relative "up" direction based on the current direction of gravity.
For movement locked to the 6 axes, you'd have to examine the values of the camera's transform.forward property and see which of the x,y,z values have the largest absolute value (Using Mathf.Abs), then convert this into a normalized vector where the largest value is given a value of 1, and the others are zeroed.
Hope this helps!
Thanks for the reply! I need free movement as I require the object to move around an open world but I cannot allow the player object to rotate when I rotate the camera. eg. "W" key to roll forward, look right with mouse, "W" key to roll sideways if that makes sense.. Are you saying I need to control the player movement via the camera controller? Because I have this currently as my player controller: using UnityEngine; using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour {
private Rigidbody playerRb;
public float moveSpeed;
void Start ()
{
playerRb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
playerRb.AddForce (movement * moveSpeed);
}
}
And I have the standard import scripts for the camera controller, using just SmoothLookAt.js and $$anonymous$$ouseOrbit.js (I haven't used much JavaScript at all so I'm not sure at all how to code it into the standard scripts) :/
Hopefully its not too much to ask how I could do it? Thanks.
You'll need to calculate "movement" like this:
Vector3 movement = cam.forward * moveVertical + cam.right * moveHorizontal;
(where "cam" is a reference to the camera's transform)
Vector3 movement = cam.forward moveVertical + cam.right moveHorizontal;
Life saver
Your answer
Follow this Question
Related Questions
Very jerky camera movement with rigidbody 1 Answer
Move RigidBody character relative to camera. 2 Answers
Rigidbody speed based on direction relative to camera 0 Answers
Moving a camera with AddFoce, or setting velocity, need help 1 Answer
How to make camera move like it does in the scene viewport? 0 Answers