- Home /
Question by
haimmoshe · Aug 30, 2017 at 04:45 PM ·
c#script.charactercontrollercharacter controller
How can i rotate my character smooth using keys ?
I'm using the CharacterController to move it but i want it to rotate also and not just move to the sides. The character is third person and have Character Controller component attached and the script attached too:
Also the character is above ground a bit in the air and there is no any collider component attached and no Rigidbody. Do i need to add this components too ?
I tried using the rotation with the line:
transform.rotation = Quaternion.LookRotation(moveDirection);
And the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CC : MonoBehaviour
{
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
public float t = 0f;
private Vector3 moveDirection = Vector3.zero;
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
transform.rotation = Quaternion.LookRotation(moveDirection);
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Comment