• 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 ChrisSch · Oct 11, 2013 at 04:27 PM · cameraobjectmoverelativeto

Move Rigidbody Relative To Camera, quick Q (hopefuly)

Ok so I've been at this the entire day cause I'm feeling so out of the zone, even tho I should be working on a trailer for my other project which is complete.

So I cooked up this simple script to move the rigidbody relative to the orbiting camera. So when you press left it goes left relative to the camera, meaning always going to the left of the screen no matter the cam rotation.

However since camera isn't perfectly behind the player, its usually above a bit, when moving in any direction, lets say forward in this example, the force is pushing player forward and down since the camera is pointing down a bit too.

So how do I limit it to only add force on the Y axis of 0, or assume that the camera's Vector3 is (0,0,1) or I don't even know how to phrase the question. xD

Here's the entire player controller code, and the camera is the MouseOrbit script from Unify wiki:

 var speed : float = 10;
 var jump : float = 10;
 var inAir : boolean = true;
 
 function Update()
 {
     if (Input.GetButton("Up"))
     {
         var camDir = Camera.main.transform.TransformDirection(Vector3.forward);
         rigidbody.AddForce(camDir * speed * Time.deltaTime);
     }
     else if (Input.GetButton("Down"))
     {
         var camDir2 = Camera.main.transform.TransformDirection(Vector3.back);
         rigidbody.AddForce(camDir2 * speed * Time.deltaTime);
     }
     if (Input.GetButton("Left"))
     {
         var camDir3 = Camera.main.transform.TransformDirection(Vector3.left);
         rigidbody.AddForce(camDir3 * speed * Time.deltaTime);
     }
     else if (Input.GetButton("Right"))
     {
         var camDir4 = Camera.main.transform.TransformDirection(Vector3.right);
         rigidbody.AddForce(camDir4 * speed * Time.deltaTime);
     }
     if(Input.GetButton("Jump") && inAir == false)
     {
         inAir = true;
         rigidbody.AddForce(Vector3(0,1,0) * jump * Time.deltaTime);
     }
 }
 
 function OnCollisionStay()
 {
     inAir = false;
 }
 
 function OnCollisionExit()
 {
     inAir = true;
 }

EDIT: The entire code works flawlessly, except I only want it adding force in a horizontal world axis relative to the camera. Hope someone understand what i mean. xD

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
2
Best Answer

Answer by robertbu · Oct 11, 2013 at 04:48 PM

In reading through, I'm assuming you have a 2D, axes aligned game. You say, " I only want it adding force in a horizontal world axis," but since you implement Up/Down as well as Left/Right. I assume you mean horizontal and vertical axes.

If this is the case, the simplest solution is just to remove any 'Z' component from the force vector:

 var camDir = Camera.main.transform.TransformDirection(Vector3.forward);
 camDir.z = 0.0;
 rigidbody.AddForce(camDir.normalized * speed * Time.deltaTime);

You may also want to freeze 'z' movement in your Rigidbody.

Comment
Add comment · Show 1 · 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 ChrisSch · Oct 11, 2013 at 05:01 PM 0
Share

No but your answer did answer it! What I needed was camDir.y = 0.0 for every direction. Now it adds force to both X and Z relative to camera, without adding force on Y, if I understood it right.

I want a kind of free/orbiting cam where the controls depend on its direction, so arrow key up will always move the character forward relative to camera and left will always move the character to the left of the screen relative to camera and so on. I'm not sure but I think Zelda has that kind of 3rd person camera? Never played it my self but from what I've seen while googling thats the thing I want.

Now the next thing I want is for the player to rotate to the direction its moving but that's for another question later after I run out of ideas how to do it, since I don't wanna come here asking for done scripts. xD

Thank you robertbu, somehow I had a feeling you'd be the one to answer since from my exp searching through unity answers yours are amongst the ones I noticed most helpful. xD

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate object ON THE CAMERA UP AXIS 2 Answers

Click a cube to move player over to that position & rotate player to face fixed point 1 Answer

How to move object to the position of another object while clicked on it? 1 Answer

How to avoid camera going upwards when looking up 1 Answer

How to move an object around with the arrow keys (Javascript) 1 Answer

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