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 /
  • Help Room /
avatar image
0
Question by Wengsti · Sep 18, 2021 at 09:46 PM · rotationtiltflyingrolltilting

Tilt object based on movement and direction

Hi Together,

i am working on a Top-Down Game and i try to implement a kind of gliding functionality. If i jump off a cliff my character starts gliding and slowly sinking. The character moves automatically forward and he is flying in the direction of the joystick. When i do nothing the player is flying straight forward to the actual facingDirection. What i want is that the character tilts left or right if i change the direction. The stronger the direction change is the stronger the tilt. Like in a short hike (a game everyone should try ;) ).

But i just dont get it how i could do that. Maybe someone can give me a hint. Here my actual code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MovementController : MonoBehaviour
 {
     private float movementSpeed;
     public float baseSpeed = 200.0f;
     public float Sensitivity = 5.0f;
     public float rotationSpeed = 5.0f;
 
     [HideInInspector] public Vector3 movingDirection = Vector3.zero;
     private Vector3 horizontalDirection;
     private Vector3 verticalDirection;
     private Quaternion facingDirection;
 
     private Rigidbody rigidbodyPlayer;
     private Vector3 right, forward;
     public float multiplier = 10.0f;
 
     void Awake()
     {
         rigidbodyPlayer = GetComponent<Rigidbody>();
     }
 
     private void Start()
     {
         forward = Camera.main.transform.forward;
         forward.y = 0;
         forward = Vector3.Normalize(forward);
         right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
 
         facingDirection = transform.rotation;
     }
 
     void Update()
     {
         GetMovementDirection();
     }
 
     void FixedUpdate()
     {
         MoveCharacter();
     }
     
     //Get the Input for the Movement
     private void GetMovementDirection()
     {
             horizontalDirection = right * Input.GetAxis("Horizontal");
             verticalDirection = forward * Input.GetAxis("Vertical");
             
             movingDirection = Vector3.Normalize(horizontalDirection + verticalDirection);
 
             //Speed of movement multiplied by sensitivity for better control
             movementSpeed = Sensitivity * (Mathf.Clamp01(new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")).sqrMagnitude));
     }
     //Move the character according to GetMovementDirection
     private void MoveCharacter()
     {
         //Automatic movement and direction change
         rigidbodyPlayer.AddForce(transform.forward * baseSpeed);
         rigidbodyPlayer.MovePosition(rigidbodyPlayer.position + movingDirection * Time.fixedDeltaTime);
 
         
     
         //Smooth Rotation to moving direction
         if (movingDirection != Vector3.zero)
         {
             facingDirection = Quaternion.LookRotation(movingDirection);
         }
 
         transform.rotation = Quaternion.Slerp(transform.rotation, facingDirection, Time.deltaTime * movementSpeed);
     }
 }


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
0

Answer by Wengsti · Sep 21, 2021 at 07:19 PM

Any ideas on how to do that? Tried AddRelativeTorque, Vector3.cross and played with Quaternions but nothing was near to what i want.

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

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

210 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

Related Questions

Rotate Object by Tilting Android Device Any Angle 1 Answer

Character movement that ends with a specific facing 0 Answers

How to keep the same yaw(right/left) and pitch(up/down) controls rotation as I roll ? 0 Answers

My player spins when I enter play mode. What am I missing? 0 Answers

HoverBike rotation return Y axis to 0 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