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 RandyMcMillen · Mar 05, 2014 at 01:30 AM · rigidbodyaddtorquerelative movement

How can I roll a sphere relative to the camera's Y axis but not X axis?

Hello, I've recently started using Unity, and have been using C# as my scripting language of preference. Just as the question says, I'm trying to roll a rigid body sphere in relation to the Y axis of the camera but not the X axis. I have searched both questions and the forums but have been unable to figure this out. The code I'm currently using works well going forward and backward, but unless the camera's X rotation is exactly 0.0f, it causes the sphere to roll offset by the X rotation of the camera. Here is a listing of my current code:

 void FixedUpdate () {
    if (Input.GetKey ("w"))
       rigidbody.AddTorque(Camera.main.transform.right * speed * Time.deltaTime);
    if (Input.GetKey ("a"))
       rigidbody.AddTorque(Camera.main.transform.forward * speed * Time.deltaTime);
    if (Input.GetKey ("s"))
       rigidbody.AddTorque(Camera.main.transform.right * -speed * Time.deltaTime);
    if (Input.GetKey ("d"))
       rigidbody.AddTorque(Camera.main.transform.forward * -speed * Time.deltaTime);
 }
 

I'm sure there's a fairly easy fix for this, but I've been bashing my head on it for a couple hours so I figured I would ask. Thank you in advance!

Comment
Add comment · Show 2
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 NoahConstable · Mar 05, 2014 at 10:15 AM 0
Share

I'm not exactly sure about your issue.

WhatDoYouWant

Which do you need? Is it something else? Respond when you feel like it. Just keep in $$anonymous$$d that if you tell me exactly what you need (a picture might help), I will help you. It's an easy fix, but I need to know what the outcome needs to be.

I'm glad to help you!

Noah.

whatdoyouwant.png (21.3 kB)
avatar image RandyMcMillen · Mar 05, 2014 at 04:28 PM 0
Share

Thanks for the reply. I have tried to create a diagram of what I'm wanting. I hope it explains the situation better than I can with words. For a little more information, I am using an orbiting camera, which is the reason I do not have a consistent X rotation of the camera.

alt text

EDIT: Probably should mention the diagram uses the vertical axis to represent Y, the horizontal axis represents the Z axis, and the unseen depth is the X Axis. The red lines represent the direction of rotation on the X axis.

1 Reply

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

Answer by whydoidoit · Mar 05, 2014 at 04:30 PM

You want to do this:

   var directionToMove = Quaternion.AngleAxis(Camera.main.transform.eulerAngles.y, Vector3.up) * Vector3.forward;
Comment
Add comment · Show 2 · 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 RandyMcMillen · Mar 05, 2014 at 04:57 PM 0
Share

Thank you very much. That works perfectly! I would vote you up, but unfortunately I have no rep yet, but it is exactly what I needed. Thank you so much!

avatar image scumola · Jul 25, 2019 at 09:54 PM 0
Share

Yup this did it for me. $$anonymous$$y situation, I had a camera moving around a lunar lander and I wanted the torque for different roll/pitch/yaw to be relative to the camera/user's view. Adding this did the trick. $$anonymous$$y code:

 if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) // left
 {
     m_Rigidbody.AddTorque(Quaternion.AngleAxis(Camera.main.transform.eulerAngles.y, Vector3.up) * new Vector3(0, 0, 5000 * Time.deltaTime));
 }
 if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) // right
 {
     m_Rigidbody.AddTorque(Quaternion.AngleAxis(Camera.main.transform.eulerAngles.y, Vector3.up) * new Vector3(0, 0, -5000 * Time.deltaTime));
 }

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

23 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

Related Questions

Giving a ball "english" (side spin) 1 Answer

How to have a cube to fall on fixed rotation? 1 Answer

If angle is greater than 5, do something, if less than -5, do something else [Answered] 2 Answers

Tank (Rigidbody) Pathfinding movement 1 Answer

Torque never goes down? 1 Answer


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