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 rogmidd · Oct 02, 2014 at 10:24 AM · c#rotationcharactercontroller

How to rotate a character controller by exactly 90 degrees with one key

I've set up a first person character controller to stick to a grid. Forward/back (omitted for now) move the controller by set amounts, and left/right rotate it by exactly 90 degrees.

Now, because i plan on using 3D audio sources, i'm trying to get the controller to smoothly rotate by 90 degrees, rather than instantly jumping 90 degrees. Below is the code that i've come up with. Right is the smooth rotation, but left is still the instant rotation. My problem is that the smooth rotation is always off by a couple of degrees, which varies each time. Also, is there a way to make this rotation take less than a second? This code only seems to accept ints.

 // Update is called once per frame
     void Update () {
 
         //ROTATION
         float rotLeftRight = Input.GetAxis ("Horizontal");
         
         if (Input.GetButtonDown ("Horizontal")) {
             
                 if (rotLeftRight > 0)
                     StartCoroutine (TurnRight (Vector3.up * 90, 1));
             
 
                 if (rotLeftRight < 0)
                     transform.Rotate (0, 270, 0);
 
                 }
             }
             
     IEnumerator TurnRight(Vector3 byAngles, float inTime) {
         var fromAngle = transform.rotation;
         var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
         for(var t = 0f; t < 1; t += Time.deltaTime/inTime) {
             transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t);
             yield return null;
         }
     }

I'm still at the beginning of my C# journey, and would appreciate any help on this. I've spent a few days searching for my own solution, but to no avail. Maybe i should try a different approach?

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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Mehul-Rughani · Oct 04, 2014 at 05:41 AM

Hope You Are Looking For This..using UnityEngine; using System.Collections;

public class Try : MonoBehaviour {

 float temp;
 bool isRotating;
 int horizontalDirection,verticalDirection;
 // Use this for initialization
 void Start () {
 }
 // Update is called once per frame
 void Update () {

             if (Input.GetKeyDown (KeyCode.RightArrow) && !isRotating) {
                     isRotating = true;
                     horizontalDirection = 1;
                     verticalDirection = 0;
                     temp = 0;
             }
             if (Input.GetKeyDown (KeyCode.LeftArrow) && !isRotating) {
                     isRotating = true;
                     horizontalDirection = -1;
                     verticalDirection = 0;
                     temp = 0;
             }
             if (Input.GetKeyDown (KeyCode.UpArrow) && !isRotating) {
                     isRotating = true;
                     verticalDirection = 1;
                     horizontalDirection = 0;
                     temp = 0;
             }
             if (Input.GetKeyDown (KeyCode.DownArrow) && !isRotating) {
                     isRotating = true;
                     verticalDirection = -1;
                     horizontalDirection = 0;
                     temp = 0;
             }
             transform.Rotate (Vector3.up * 90 * Time.fixedDeltaTime * horizontalDirection, Space.World);
             transform.Rotate (Vector3.right * 90 * Time.fixedDeltaTime * verticalDirection, Space.World);
             temp += 90 * Time.fixedDeltaTime;
             if (temp >= 90) {
                     temp = 0;
                     horizontalDirection = 0;
                     verticalDirection = 0;
                     isRotating = false;
             }
     }

}

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 rogmidd · Oct 04, 2014 at 07:40 AM 0
Share

This is perfect, thank you!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Make 3D Player look exactly at mouse with ScreenToWorldPoint? (maths Question) 2 Answers

How can I make the keys randomly change after each keypress? 1 Answer

A node in a childnode? 1 Answer

Rotating ray origin. 2 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