Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 joemane22 · Dec 20, 2014 at 03:45 PM · rotationvector3quaterniondirectionvector

Rotating a direction Vector3 by Quaternion

I've done research but every solution I've found gives me the same result when trying to just move forward in relation to the camera. C#

 [RequireComponent(typeof(Rigidbody))]
 public class RollerController : MonoBehaviour {
     public float BasicSpeed = 2f;
     public float BoostAmount = 10f;
     CameraController playerCamera;
     // Use this for initialization
     void Start () {
         playerCamera = GetComponent<CameraController>();
     }
     
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKey(KeyCode.W))
             rigidbody.AddForce((camera.transform.rotation * playerCamera.transform.forward) * BasicSpeed);
 
         
     }

this code seems like it should work right? My game object just moves in circles when I hold the w key down.. What is going on?!?

edit:: sorry been long day it actually just doesn't rotate at and continues along the forward direction?

edit:: Thought maybe my camera controller was messing with the rotation of the camera when passing the values?? just a thought here's the camera controller I am using. Its not complete was just getting it working first.

 public class CameraController : MonoBehaviour {
     public Transform CameraTransform;
 
     public float DistanceUp = 10;
     public float DistanceBack = 5;
     public float RotationSpeed = 3;
 
     public float total = 0;
     // Use this for initialization
     void Start()
     {
 
     }
     
     // Update is called once per frame
     void Update()
     {
         total += Input.GetAxis("Mouse X") * RotationSpeed;
         CameraTransform.rotation = Quaternion.Euler(new Vector3(40, 0, 13));
         CameraTransform.position = transform.position + new Vector3(0, DistanceUp, -DistanceBack);
         CameraTransform.RotateAround(transform.position, Vector3.up, total);
     }
 }
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

2 Replies

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

Answer by joemane22 · Dec 21, 2014 at 12:58 PM

The solution to this is to use the Unity Matrix4x4 struct. You can easily rotate a vector point after creating the appropriate Matrix by calling Matrix4x4.TRS(Vector3 translation, Vector3 rotation,Vector3 scale). If you pass a zero vector into translation and a one vector into scale and just set the rotation to the desired rotations like this

 Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(new Vector3(xRotation, yRotation, zRotation)), Vector3.one);

You can easily rotate a direction Vector by calling

 matrix.MultiplyPoint(directionVector);

Hope this helps anyone that may run into this same problem!

Comment
Add comment · 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
0

Answer by tanoshimi · Dec 21, 2014 at 10:32 AM

Don't you just want camera.transform.forward ?

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 joemane22 · Dec 21, 2014 at 10:36 AM 0
Share

this is just a simple version actually I want to isolate the Y rotation since the camera is looking down on the object a bit. This version is here because I have tried every possible way I could think of just to get it to work slightly and build on that.

avatar image joemane22 · Dec 21, 2014 at 10:38 AM 0
Share

Actually just tried Camera.transform.forward for the sake of it and it is the version that goes in circles.. strange

avatar image joemane22 · Dec 21, 2014 at 11:09 AM 0
Share

I honestly don't understand this.. I have made 100s of cameras in xna using matrices and never had a problem. I know how cameras work and how to get them to move and the player to.. I think I am just going to do the math the hard way... using Vector math.. seems easier

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

26 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

Related Questions

Rotate object based on hit normal + matching camera direction 0 Answers

Get rotation from a vector direction. 1 Answer

C# Raycast from Object direction (z-axis) + another Vector3 1 Answer

3D nested Turret Prefab Rotation 1 Answer

How to make Y-Axis face away from a position? 2 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