• 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
1
Question by Ziemelis · May 27, 2021 at 10:37 AM · camerarotation3dplayer

How to set the player in front of the camera in any rotation

Hi, I make a game, but I don't know how to make it, that when a player falls from a higher place, the front side stays the same as the camera stands. (In this case, I use a rigid body.) Could use "freeze rotation", but I can't do that, because in this project everywhere except in this case it is necessary for the player to rotate. Sample-

alt text

alt text

I think it could be made that while a player falls from somewhere, it is allowed to rotate freely, but when it touches the ground, its front is set equal to the front of the camera.

The next problem could be, even if made so far, the player can touch the ground with a corner when he falls, or can't it be that the player sets the front until it isn't standing straight on the ground?

Code I use(there isn't camera movement)-

 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     public Rigidbody rigg;
     private Vector3 moveDirection;
     public float speed;
 
     
     void Start()
     {
         rigg = GetComponent<Rigidbody>();
         
     }
 
     
     void Update()
     {
         
         float hor = Input.GetAxisRaw("Horizontal");
         float ver = Input.GetAxisRaw("Vertical");
 
         moveDirection = transform.forward * ver + transform.right * hor;
 
         rigg.AddForce(moveDirection.normalized * speed, ForceMode.Acceleration);
         
 
     }
    
 }



Thanks!

2021-05-27-13-03-32-window.png (223.2 kB)
2021-05-27-13-09-29-window.png (53.0 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
0
Best Answer

Answer by DenisIsDenis · May 27, 2021 at 10:53 AM

This code can help (assign to the player's script):

 void OnCollisionStay(Collision collision)
     {
         Quaternion deltaRot = Quaternion.FromToRotation(transform.right, Camera.main.transform.right);
         transform.rotation = Quaternion.Lerp(transform.rotation, deltaRot * transform.rotation, 10f * Time.deltaTime);
     }
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 logicandchaos · May 27, 2021 at 01:28 PM 0
Share

OnCollisionStay is really heavy, there should be another way to do it.

avatar image DenisIsDenis logicandchaos · May 27, 2021 at 02:10 PM 0
Share

For optimization, you can use raycast:

     float counter;
     public float distanceToGround = 0.8f;
     Quaternion deltaRot = Quaternion.identity;
     void Update()
     {
         counter += Time.deltaTime;
         Ray ray = new Ray(transform.position, -Vector3.up);
         if (counter >= 0.25f) // check is performed 4 times per second
         {
             counter = 0;
             if (Physics.Raycast(ray, out RaycastHit hit, distanceToGround))
             {
                 deltaRot = Quaternion.FromToRotation(transform.right, Camera.main.transform.right);
             }
         }
         transform.rotation = Quaternion.Lerp(transform.rotation, deltaRot * transform.rotation, 5f * 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Locking world rotation of child 1 Answer

3d Sound when the player near and not the camera? 1 Answer

Need camera to follow player, but not the player's rotation. 13 Answers

Rotation of player based on camera direction and joystick direction in 3D world 1 Answer

How to make a camcorder show things that don't show normally 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