Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 DissonanceMask · Feb 13 at 12:25 PM · cameravector3angletranslatequaternions

How to get the forward vector normal to the camera's forward vector regardless of camera pitch

I hope I can explain this properly. Imagine a 3d world, in it is a plane as ground to stand on, and a character standing upright on top of it. Also, a camera, upright, elevated and pitched down to see the character.

I want to move {using Transform.Translate()} the character forward along the plane in reference to the camera's forward, but technically since the camera is pitched or angled downwards, the camera's forward is also at angle, and the character would sink into the ground while moving forward.

How do I get that forward direction vector relative to the direction the camera is facing? I am very lost with the solutions considering the difference between Quaternion and Vector3. Is there a way to convert Quaternions to Vector3 and vice versa? Ill post an image trying to illustrate the idea, and my code.alt text

my code: using UnityEngine; using UnityEngine.InputSystem;

 public class Character : MonoBehaviour
 {
     #region References
     Camera cam;
 
     #endregion References
 
     #region Components
 
     #endregion Components
 
     #region Movement
     bool canMove = true;
     Vector2 inputDirection;
     [SerializeField] float moveSpeed = 5.0f;
     #endregion Movement
 
 
     private void Start()
     {
         cam = Camera.main;
     }
 
     void Update()
     {
         Move(inputDirection);
     }
 
     public void OnMove(InputAction.CallbackContext context)
     {
         inputDirection = context.ReadValue<Vector2>();
     }
 
     private void Move(Vector2 direction)
     {
         if(!canMove) return;
         // Sideways Movement
         Vector3 moveSideways = cam.transform.right * direction.x;
 
         //Forward Movement
         Quaternion pitchAngle = new Quaternion(cam.transform.rotation.x - Quaternion.identity.x, 0, 0, 0);
         
         Vector3 moveForward = pitchAngle * cam.transform.forward * direction.y;
         Vector3 moveVector = moveSideways + moveForward;
 
         transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);
     }
 }
 
vectorillustration.png (11.5 kB)
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 Hellium · Feb 13 at 12:53 PM

  private void Move(Vector2 direction)
  {
      if(!canMove) return;

      Vector3 right = cam.transform.right;
      Vector3 forward = Vector3.ProjectOnPlane(cam.transform.forward, Vector3.up).normalized;
      Vector3 moveVector = forward * direction.y + right * direction.x;
 
      transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);
  }


OR


  private void Move(Vector2 direction)
  {
      if(!canMove) return;

      Vector3 right = cam.transform.right * direction.x;
      Vector3 forward = Vector3.Cross(cam.transform.right, Vector3.up);
      Vector3 moveVector = forward * direction.y + right * direction.x;
 
      transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);
  }


Note: won't work if the camera is aligned with the world's Y axis.

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 DissonanceMask · Feb 13 at 03:35 PM 0
Share

it works at intended! I wish I could learn how to compute it with Quaternions and Vectors, but ill take it!

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

228 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Project an object from a perspective camera at same screen position but different Y world position 0 Answers

Translate Point By Vector3 And Angle 1 Answer

Rotate object instead of camera around it? 0 Answers

Object keeps moving up even when StopCoroutine is called 2 Answers

Vector3.SmoothDamp() - smoothTime unit 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