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 /
avatar image
0
Question by sba_stuff · Apr 13, 2018 at 11:53 AM · gameobjectcharactercontrollercharacter controllercharacter movementcharacter controlling

How to make character to face in specific direction.,How To Rotate Character When Arrow Key Is Pressed?

Hello There!! I was trying to develop a small game for my learning and i created a mini c# character controller script according to my own requirements. In this code a character moves in specific axis while showing animation. Here is the code.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class WajeehCharacterController : MonoBehaviour {
     public float speed = 0.5f;
     Animator animationObj;
     bool upkeydown =false;
     bool leftkeydown=false;
     bool rightkeydown=false;
     bool downkeydown=false;
 
     void Update () {
       animationObj = gameObject.GetComponentInChildren<Animator>();
       if (Input.GetKeyDown(KeyCode.UpArrow)) { upkeydown = true; }
       if (Input.GetKeyDown(KeyCode.DownArrow)) { downkeydown = true; }
       if (Input.GetKeyDown(KeyCode.LeftArrow)) { leftkeydown = true; }
       if (Input.GetKeyDown(KeyCode.RightArrow)) {rightkeydown=true;}
       if (Input.GetKeyUp(KeyCode.UpArrow)) { upkeydown = false; }
       if (Input.GetKeyUp(KeyCode.DownArrow)) { downkeydown = false; }
       if (Input.GetKeyUp(KeyCode.LeftArrow)) { leftkeydown = false; }
       if (Input.GetKeyUp(KeyCode.RightArrow)) { rightkeydown = false; }
       if(Input.GetKeyDown(KeyCode.LeftControl)) { animationObj.Play("Kick"); }
       if (upkeydown) { transform.Translate(-speed, 0, 0, Space.World); animationObj.Play("Run_Forward"); }
       if (downkeydown)
       { transform.Translate(speed, 0, 0, Space.World); animationObj.Play("Run_Forward"); }
       if (leftkeydown)
       { transform.Translate(0, 0,-speed, Space.World); animationObj.Play("Run_Forward"); }
       if (rightkeydown)
       {   transform.Translate(0, 0,speed,Space.World); animationObj.Play("Run_Forward"); }
       if (Input.GetKeyDown(KeyCode.Space)) { animationObj.Play("Jump"); }
     }
 }


But the problem is, character do not change its own direction. For example, when right key is pressed character not only moves to right but also his face would be right. Same for left, character moves left and his face should be left. Same procedure for top and bottom. I used rotation scenario but it will work for forward and backward (as i use 180 angles.). When up and down keys are pressed, it changed. I need to know how to face the character in specific direction via correct rotation.

I am learning Unity and proper guidance would be very appreciated. Advance Thanks.

Here is the sample of my game; My Image Demo

,Hello There! I created a small unity game for learning purposes. And I created a mini character controller according to my requirements here is the sample

using System.Collections; using System.Collections.Generic; using UnityEngine; public class WajeehCharacterController : MonoBehaviour { public float speed = 0.5f; Animator animationObj; bool upkeydown =false; bool leftkeydown=false; bool rightkeydown=false; bool downkeydown=false; void Update () { animationObj = gameObject.GetComponentInChildren<Animator>(); if (Input.GetKeyDown(KeyCode.UpArrow)) { upkeydown = true; } if (Input.GetKeyDown(KeyCode.DownArrow)) { downkeydown = true; } if (Input.GetKeyDown(KeyCode.LeftArrow)) { leftkeydown = true; } if (Input.GetKeyDown(KeyCode.RightArrow)) {rightkeydown=true;} if (Input.GetKeyUp(KeyCode.UpArrow)) { upkeydown = false; } if (Input.GetKeyUp(KeyCode.DownArrow)) { downkeydown = false; } if (Input.GetKeyUp(KeyCode.LeftArrow)) { leftkeydown = false; } if (Input.GetKeyUp(KeyCode.RightArrow)) { rightkeydown = false; } if(Input.GetKeyDown(KeyCode.LeftControl)) { animationObj.Play("Kick"); } if (upkeydown) { transform.Translate(-speed, 0, 0, Space.World); animationObj.Play("Run_Forward"); } if (downkeydown) { transform.Translate(speed, 0, 0, Space.World); animationObj.Play("Run_Forward"); } if (leftkeydown) { transform.Translate(0, 0,-speed, Space.World); animationObj.Play("Run_Forward"); } if (rightkeydown) { transform.Translate(0, 0,speed,Space.World); animationObj.Play("Run_Forward"); } if (Input.GetKeyDown(KeyCode.Space)) { animationObj.Play("Jump"); } } }

But the problem is i am unable to rotate character in specific direction. Suppose when right arrow is pressed character face must be at right side, left arrow is pressed character face must be left side. and same for up and down arrow. I use logic of rotation but it failed because it works good for left and right but when up and down keys pressed, character rotation is disturbed.

I need a bit help to rotate character face along the direction where arrow key is pressed.

Advance Thanks.

Here is the image of my work. alt text [1]: /storage/temp/114879-wajeehimage.png

I am noob in unity and learning for fun and hobby. Help and proper guide to change the character would be appreciated.

wajeehimage.png (498.0 kB)
wajeehimage.png (498.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

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

123 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

Related Questions

Character Creeping Backwards 0 Answers

How can I slide with a character controller 1 Answer

Can I Change Expression Of Player While Running, Getting Hurt Or Dead, Or Jumping, Idle, Walking, Or Sneaking 2 Answers

I need to know how to apply force to an object when using a character controller and I'm not that good at coding either any answers will be greatly appreciated. 0 Answers

Character Controller based 'dash' function moves strangely in one direction, despite values seemingly being correct. 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