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 ThatKrazyGuy · Jan 14, 2012 at 08:24 AM · c#rotationvector3quaternion

Making my GambeObject rotate to face the direction of movement, smoothly.

Oh hai thar~

Me and a couple of pals have been working on designing a 3rd Person Hack and Slash(I have worked through the process of making a game a couple of times, they weren't great, but they were a nice learning experience.) Though I've come across a little bit of trouble. :/

I decided that it would be a great idea(Oh god!) to make the selected character face what ever direction the currently pressed WASD keys represented(So degrees on the Y-axis such as 0, 45, 90, 135, 180, etcetera...)

After working with this for about a day or two, I came up with this:

         using UnityEngine;
     using System.Collections;
     
     public class Character_Movement : MonoBehaviour {
         static public int WALK_Speed = 8;
         public int RUN_Speed = 25;
         public int TURN_Speed = 20;
         public int JUMP_Speed = 10;
         public int Character_ChangeSpeed = 45;
     
     
         private bool isRunning = false;
         private bool isJumping = false;
         public bool isTakingDamage = false;
         public bool PositiveRotate;
         public bool NegativeRotate;
     
     
         public Transform Character_Object;
         private Transform This_Object;
     
     
     
         // Use this for initialization
         void Start () {
     
     
     
             This_Object = transform;
     
         }
     
         // Update is called once per frame
         void Update () {
     
             Physics.gravity = new Vector3(0, -10, 0);
     
             //Motor Skillz! Oh boi!
             //
             //
             //
     
             if(Input.GetKey("w")){
     
                  This_Object.Translate(Vector3.forward * WALK_Speed * Time.deltaTime);
     
             Character_Object.eulerAngles = new Vector3(0, 0, 0);
     
             }
             if(Input.GetKey("s")){
     
                 This_Object.Translate(Vector3.forward * -WALK_Speed * Time.deltaTime);
     
             Character_Object.eulerAngles = new Vector3(0, 180, 0);
     
             }
             if(Input.GetKey("a")){
     
                 This_Object.Translate(Vector3.right * -WALK_Speed * Time.deltaTime);
     
             Character_Object.eulerAngles = new Vector3(0, 270, 0);
     
             }
             if(Input.GetKey("d")){
     
                 This_Object.Translate(Vector3.right * WALK_Speed * Time.deltaTime);
     
             Character_Object.eulerAngles = new Vector3(0, 90, 0);
     
     
             }
             // Non-directional(What?) rotations (I.E. 45, 135, 225, 315 degrees)
     
             if(Input.GetKey("w") && Input.GetKey("d")){
     
             Character_Object.eulerAngles = new Vector3(0, 45, 0);
     
             }
     
             if(Input.GetKey("w") && Input.GetKey("a")){
     
             Character_Object.eulerAngles = new Vector3(0, 315, 0);
     
             }
             if(Input.GetKey("s") && Input.GetKey("d")){
     
             Character_Object.eulerAngles = new Vector3(0, 135, 0);
     
             }
             if(Input.GetKey("s") && Input.GetKey("a")){
     
             Character_Object.eulerAngles = new Vector3(0, 225, 0);
     
             }
     
             //Jumping! :D
             if(Input.GetKey("space")){
     
             This_Object.Translate(Vector3.up * JUMP_Speed * Time.deltaTime);
     
             }
             //
             //
             //
             //No moar motor skillz! D:<
     
     
     
     
     
         }
     
     
         }
 

All the jumping and crap isn't important, the part I should be paying attention to is this(After fumbling around with the Code Formatting sigh):

     if(Input.GetKey("w")){
                     
                      This_Object.Translate(Vector3.forward * WALK_Speed * Time.deltaTime);
                     
                 Character_Object.eulerAngles = new Vector3(0, 0, 0);
                     
                 }

Now this code is all fine and dandy, but I can't seem to wrap my mind around being able to make the Character_Object smoothly rotate to face the directions, any help is welcome, even if it means yelling at me.

I don't intend to have people right code for me(Though if you feel obligated to, please at least explain what each piece of the code does) I'd just like to get a good baring on what I should be trying to do with the code.

Thanks for your time,

      That KrazyGuy that no one knows.
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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

calculating looking angle between 2 transforms 0 Answers

Rotate player (rigidbody) towards his movement 2 Answers

Instantiate GameObject towards player 0 Answers

How can i rotate a object more than once (90 degrees)? 1 Answer


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