Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Nov 16, 2017 at 02:43 AM by Cedric24 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Cedric24 · Oct 24, 2017 at 12:57 AM · rotationcharactercontrollermovement script

Rotating character to a direction (2.5d)

Hello,

First, I'd like to apologize because I know this question has been asked plenty of times. Unfortunately, even after reading through the dozens of topics, I am still unable to comprehend how to achieve what I want.

Basically, I am working on a 2.5D game, where the player may only move left or right. I would like the character to face the direction the character controller is moving in. For now, I've managed to achieve what I want somewhat using the following script on my character controller:

 float moveHorizontal = Input.GetAxis ("Horizontal");
         moveDirection = new Vector3 (moveHorizontal * speed, moveDirection.y, 0.0f);
 
         moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale);
         controller.Move (moveDirection * Time.deltaTime);
 
         if (moveHorizontal > 0)
         {
             transform.eulerAngles = new Vector3(0, 0, 0);
         }
         if (moveHorizontal < 0)
         {
             transform.eulerAngles = new Vector3(0, 180, 0);
         }
 

However, instead of automatically setting the rotation to a new Vector 3 (0, 180, 0) when moving left, I'd like the player to rotate to that direction . I've tried doing transform.rotation = Quaternion.LookRotation(moveDirection), but it makes my character move weirdly and look all over the place. I just need him to rotate on the Y axis. Alternatively, if someone could point me to a basic tutorial on quaternions (I already checked the Unity ones, I'm struggling to understand this concept) that would be of great help. Thanks.

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

  • Sort: 
avatar image
0
Best Answer

Answer by FabDynamic · Oct 24, 2017 at 01:35 AM

I think I see what you mean and that sounds cool! Instead of a Mario type game you want it to actually animate the rotation smoothly from left to right. That will end up cool!

What you want to do is called 'lerp-ing'. I think the word evolved from 'interpolate' or but it involves just the kind of smooth transition you are interested in.

Starting with the example here you'll want to change the Transforms from and to into specific rotations. Something like this:

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour {
     public Quaternion from = Quaternion.euler (0f, 0f, 0f);
     public Quaternion to = Quaternion.euler (0f, 180f, 0f);
     public float speed = 0.1F;
     void Update() {
         transform.rotation = Quaternion.Lerp(from, to, Time.time * speed);
     }
 }


Here are some more details: http://answers.unity3d.com/questions/643141/how-can-i-lerp-an-objects-rotation.html

Oh and wrt Quaternions: I don't know of any tutorials, but I also struggle to understand this concept however Im starting to realize that we don't need to understand it. This has worked for me so far: Just think of them as something not to mess with directly. In order to adjust rotations use the built in functions and anytime you need to specify the angles specifically use Euler angles exclusively. I hope that helps.

Let me know if for any reason that doesn't work and I'll see if I can prototype it.

UPDATE: I prototyped it and made a video to show it working: http://youtu.be/stXNSrXyF1k?hd=1

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 Cedric24 · Oct 24, 2017 at 11:39 AM 1
Share

Wow, thanks so much for all the effort you put into this! I'll check this out as soon as I can and provide an update on my situation. Thanks a lot for your help!

avatar image FabDynamic Cedric24 · Oct 26, 2017 at 12:13 AM 0
Share

You're welcome! I'm looking forward to the update and seeing how well it works for you and your purposes.

Follow this Question

Answers Answers and Comments

112 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

Related Questions

Character Controller - unknown rotation, flying 1 Answer

Character Controller slides sideways when it hits objects are angles different from 90 degrees 1 Answer

How do I add camera rotation while moving a player sideways? 2 Answers

Rotate CharacterController collider? 0 Answers

CharacterController.move doesn't seem to move me horizontally 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