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 ThomasAqtl · Dec 18, 2020 at 05:04 PM · rotationtilttilting

Tilt an object when rotating and set it back to normal afterwards

Hi everyone,

Here is my problem. I have an object (basically, a moto) and so far I just make it go on a plane. I can make it rotate so it goes left or right whenever I want. Now, as motos do when they turn, I want my moto to tilt when turning. Here is my Movements and ControllerManager class so you can see what i'm working with.

 public class ControllerManager : MonoBehaviour
 {
     public KeyCode forwardKey, leftKey, rightKey;
     public float tilt;
     private float _fwd, _lateralDirection, _right, _left;
     public float Fwd{   
         get { return _fwd;}
     }
     public float LateralDirection{
         get { return _lateralDirection; }
     }
 
     public void Start(){
         _fwd = 0;
         _right = 0;
         _left = 0;
     }
 
     public void FixedUpdate(){
         // _fwd and _lateralDirection kind of simulates default horizontal and vertical axis values
         _fwd += Input.GetKey(forwardKey) ? (_fwd < 1 ? 0.1f : 0) : (_fwd > 0 ? (_fwd < 0.01f ? -_fwd : -0.1f) : 0);
         
         // moto can't turn if not moving forward
         _right += Input.GetKey(rightKey) & _fwd != 0 ? (_right < 1 ? 0.1f : 0) : (_right > 0 ? (_right < 0.1f ? -_right : -0.1f) : -_right);
 
         _left += Input.GetKey(leftKey) & _fwd != 0  ? (_left > -1 ? -0.1f : 0) : (_left < 0 ? (_left > 0.1f ? -_left : 0.1f) : -_left);
 
         _lateralDirection = _left + _right;
     }
 }



 public class Movements : MonoBehaviour
 {
     public float speed;
     public float sensibility;
     private ControllerManager _controller;
     // Start is called before the first frame update
     void Start()
     {
         _controller = GetComponent<ControllerManager>();
     }
 
     // Update is called once per frame
     void LateUpdate()
     {
         transform.Translate(Vector3.right * _controller.Fwd * speed * Time.deltaTime, Space.Self);
         transform.Rotate(Vector3.up * _controller.LateralDirection * sensibility * Time.deltaTime, Space.World);
     }
 }


Any ideas ? I have tried to use Animations but I can't seem to make it work : I created two animations TiltRight and TiltLeft. Each of them simply rotates the moto (~ 25 degrees) around fwd axis, respectively right and left. But I'd need to sort of pause the state of the object while turning key are pressed. And I did not achieve that.

Regards.

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 Jaxcap · Dec 19, 2020 at 08:49 AM 0
Share

Is this example in the Unity documentation similar to what you want? $$anonymous$$aybe it can help you: https://docs.unity3d.com/ScriptReference/Transform-rotation.html

avatar image ThomasAqtl Jaxcap · Dec 20, 2020 at 08:06 PM 0
Share

It seems to be, but I cannot get it work. Because I cannot rotate the moto anymore. It justs goes back to its inital direction when i stop turning. So basically it goes only along the X axis.

0 Replies

· Add your reply
  • Sort: 

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

151 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

Related Questions

How to get moving player to tilt when turning 0 Answers

EulerAngles conversion Quaternion problem 2 Answers

Flip the gameobject when tilting 2D C# 1 Answer

Rotate Camera to a position and rotate back 1 Answer

rotating/tilting the bike through accelerometer in unity. 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