• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by garrei · Jun 17, 2015 at 03:20 AM · cameramovementrigidbodycamera-movementlook

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.

Kula World Level 1 - 15

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image garrei · Jun 17, 2015 at 04:17 PM 0
Share

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.

avatar image duck ♦♦ · Jun 18, 2015 at 09:55 AM 1
Share

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)

avatar image garrei · Jun 24, 2015 at 11:08 AM 0
Share

Vector3 movement = cam.forward moveVertical + cam.right moveHorizontal;

Life saver

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity trail render attached to rigidbody is jittery 0 Answers

Want to apply "height" to my DeadSpace Camera --> Physics.Linecast gives me trouble 1 Answer

Moving a camera with AddFoce, or setting velocity, need help 1 Answer

Movement feels like it's always going slightly off to the sides 1 Answer

Regarding transform.position in the roll a ball tutorial 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges