- Home /
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;
,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. [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.
Your answer
Follow this Question
Related Questions
Character Creeping Backwards 0 Answers