- Home /
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.