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 ErisArcana · Apr 25, 2021 at 12:08 AM · scripting problemrigidbody2.5d

How to rotate player to face moving direction?

Ok, so the game I'm working on is a 2.5D platformer, you move left and right, and movement is set up to move the character along the local z axis. I have it set up this way because there will be paths that the player can travel along that curve, so I need to be able to rotate the player while they are moving forward to make them turn.

I made several attempts at having the character rotate properly, or use LookAt to make them face the way they are moving, but each time I do it, it messes with the movement. I can move fine in one direction, but if I switch directions the character freaks out and doesn't move properly at all.

 void Start()
     {
         player_rb = GetComponent<Rigidbody>();
         col = GetComponent<CapsuleCollider>();
     }
 
     void Update()
     {
         
         Vector3 fwd = transform.forward * Input.GetAxisRaw("Horizontal") * Speed;
         inputVector = new Vector3(fwd.x, player_rb.velocity.y - 2, fwd.z);
         transform.LookAt(transform.position + new Vector3(0, 0, fwd.z));
 //lookat is what is causing issues
 
     }
 
     void FixedUpdate()
     {
         player_rb.velocity = inputVector;
     }



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

Answer by disuperguy · Apr 25, 2021 at 12:27 AM

try replacing lines 10-12 with this:

 transform.RotateAroundLocal(Vector3.up, Input.GetAxisRaw("Horizontal") * Speed);
 inputVector = new Vector3(transform.forward.x, player_rb.velocity.y - 2, transform.forward.z);
 //Im not sure if this works but it should look something like this




Comment
Add comment · Show 1 · 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 ErisArcana · Apr 25, 2021 at 10:05 AM 0
Share

Doing so makes the character spin around really fast when a movement key is pressed xD

I'm not sure but I had some thoughts on this, and I think it is not working because when the character is rotated, the move direction is change and then it goes back and forth repeatedly.. I'm not sure how to explain it, but I'm wondering if I need to find another kind of solution, like just visually rotating the character, through animation and not mess with the rigid body, idk :x

avatar image
0

Answer by ErisArcana · Apr 25, 2021 at 10:36 AM

Well, I got some results, but it kinda confirms what I was thinking, that I should rotate the character with just animation, because if I move back, the character moves backwards and forwards moves in the direction the character is facing.. Idk this is kinda confusing, but with this code I got the character to flip direction.. but now I can only move forward.

         Vector3 fwd = transform.forward * Input.GetAxisRaw("Horizontal") * Speed;
         inputVector = new Vector3(fwd.x, player_rb.velocity.y - 2, fwd.z);
 
 
         if (MoveRight == false && Input.GetKeyDown(KeyCode.D))
         {
             MoveRight = true;
             MoveLeft = false;
             transform.RotateAroundLocal(Vector3.up, 110);
         }
 
         if (MoveLeft == false && Input.GetKeyDown(KeyCode.A))
         {
             MoveLeft = true;
             MoveRight = false;
             transform.RotateAroundLocal(Vector3.up, -110);
         }

Also for some reason, 180 wasn't actually 180 in game, so I found a number that gave the closest result, and putting in 110 makes the character rotate to -177...

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

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

Spawning a Ball going forward every 5 seconds error, help! 2 Answers

Making a connection point system like robocraft 0 Answers

How to trigger a BoxCollider with a RigidBody? 0 Answers

Avoid overlapping drag & drop game objects in 3d space 0 Answers

Bunch of instantiated RBs blows away 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