- Home /
Question by
Skynet2012 · Jan 08, 2013 at 03:37 AM ·
rotationcharactercontroller
how to rotate character controller?
I got a character controller and it's moving fine but if i press left or right i want the character to rotate. It always moves forward, backward, left or right. Can someone please help me out. Sorry if this is a noob question but i am very new to c# scripting. Thanks!
This is what i got so far:
using UnityEngine;
using System.Collections;
public class MoveScript : MonoBehaviour
{
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public float jumpCount = 0.0f;
public float turningSpeed = 60;
float maxJump = 2.0f;
private Vector3 moveDirection = Vector3.zero;
// Update is called once per frame
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
controller.Move(moveDirection * Time.deltaTime);
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
if (!controller.isGrounded)
{
moveDirection.x = Input.GetAxis("Horizontal") * speed;
}
else
{
moveDirection = new Vector3(Input.GetAxis("Horizontal") * speed, 0.0f, Input.GetAxis("Vertical") * speed);
jumpCount = 0.0f;
}
if (jumpCount < maxJump)
{
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpSpeed;
jumpCount++;
}
}
if (controller.collisionFlags == CollisionFlags.Sides)
{
jumpCount = 0.0f;
}
}
}
Comment