Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 VesmirPendel · Sep 09, 2015 at 07:06 PM · c#unity 5camerarigidbody

Rotating player relative to the camera (Unity C#)

So I've been thinking about this problem for a long time now. As the beginner that I am, I decided to make a game of my own in Unity, with simple controls like moving a cube, and rotating the camera around the player. Now my only problem is moving the character relative to the camera. I am using the MouseOrbitImproved script for the camera rotation. The player controls are as follows below:

 public float moveSpeed;
 public static float jumpHeight = 5f;
 public static Vector3 input;
 public static Vector3 movement;
 public static Rigidbody rigidBody;

 public static bool isFalling = false;

 void Start () {
     rigidBody = GetComponent<Rigidbody>();
 }
 
 void Update () {
     rigidBody.velocity = new Vector3 (Input.GetAxis ("Horizontal") * moveSpeed, rigidBody.velocity.y, Input.GetAxis ("Vertical") * moveSpeed);
     if (Input.GetKeyDown (KeyCode.Space) && isFalling == false) 
         rigidBody.velocity = new Vector3 (0, jumpHeight, 0);
     isFalling = true;
 }

 void OnCollisionStay()
 {
     isFalling = false;
 }

What I know is that I need to make the cube rotate in the y axis relative to the camera. I've already tried quaternions and other solutions like finding other scripts deep down in the Unity packages but I still don't have a definite answer.

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
1

Answer by Eno-Khaon · Sep 09, 2015 at 07:45 PM

In general, this can be handled by keeping a reference to your camera's transform available. Then, you can use that reference to modify your controls.

For example, for direct control over the character, you can do something like:

 public Transform camTransform;
 
 // ...
 
 Vector2 directionInput = new Vector2(Input.GetAxis("Horizontal") * moveSpeed, Input.GetAxis("Vertical") * moveSpeed);
 // Eliminate camera pitch and/or roll influence by reconstructing the forward and right vectors to align to world space
 Vector3 newForward = Vector3.Cross(camTransform.right, Vector3.up).normalized * directionInput.x;
 Vector3 newRight = Vector3.Cross(Vector3.up, newForward).normalized * directionInput.y;
 
 Vector3 newDirectionInput = newForward + newRight;

On a related note, I would recommend modifying your control schemes to be based around AddForce() rather than directly modifying velocity (especially for jumping input), but however you want to approach that is up to you.

Edit: It's also worth noting that rotations can be derived from this on the same basis. By deriving flattened rotations based on the camera's facing, your character can rotate to face in those directions as well.

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 VesmirPendel · Sep 10, 2015 at 02:00 AM 0
Share

The code almost worked actually! It's just that the strafe buttons (A and D keys) aren't working at all.

Also, why is there a Vector2 type with a Vector3 value?

avatar image VesmirPendel VesmirPendel · Sep 10, 2015 at 02:58 AM 0
Share

EDIT: Never$$anonymous$$d! I've solved it now! I just replaced:

 Vector3 newRight = Vector3.Cross(Vector3.up, newForward).normalized * directionInput.y;

With:

 Vector3 newRight = Vector3.Cross(Vector3.up, camTransform.forward).normalized * directionInput.y;

So everything is fine now!

avatar image Eno-Khaon · Sep 10, 2015 at 05:04 AM 0
Share

Whoops! You got me there! Nice catch.

I was jumping between a few ideas and forgot to change the value from a "new Vector3()" to a "new Vector2()" in the post.

Also, I'm a little confused at how there could be a difference between the means used to calculate a "new right" direction, since the Cross Product using either "forward" direction should be the same in 99% of circumstances, but as long as you have it working, I'm not about to complain.

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

how do i make first person character rotate left and right along with camera? 0 Answers

Problem with jumping in fps game 1 Answer

view bounds perspective camera 1 Answer

I cannot see button on camera preview 1 Answer

Scope Zoom Sensitivity 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges