- Home /
Third Person Controller Rotation
Heyyy so i wrote this code for 3d character with anims and i don't know how to make player 3d model rotate while moving someone can help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCharacterController : MonoBehaviour
{
float speed = 4;
float rotSpeed = 100;
float rot = 0f;
float gravity = 8;
bool walking = false;
Vector3 moveDir = Vector3.zero;
CharacterController controller;
Animator anim;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
Movement();
}
void Movement()
{
if (controller.isGrounded)
{
if (Input.GetKey(KeyCode.W))
{
{
anim.SetBool("running",true);
bool walking = true;
anim.SetInteger("condition", 1);
moveDir = new Vector3(0, 0, 1);
moveDir *= speed;
moveDir = transform.TransformDirection(moveDir);
}
}
if (Input.GetKeyUp(KeyCode.W))
{
anim.SetBool("running",false);
bool walking = false;
anim.SetInteger("condition", 0);
moveDir = new Vector3(0, 0, 0);
}
if (Input.GetKey(KeyCode.S))
{
{
anim.SetBool("running",true);
bool walking = true;
anim.SetInteger("condition", 1);
moveDir = new Vector3(0, 0, -1);
moveDir *= speed;
moveDir = transform.TransformDirection(moveDir);
}
}
if (Input.GetKeyUp(KeyCode.S))
{
anim.SetBool("running",false);
bool walking = false;
anim.SetInteger("condition", 0);
moveDir = new Vector3(0, 0, 0);
}
if (Input.GetKey(KeyCode.A))
{
{
anim.SetBool("running",true);
bool walking = true;
anim.SetInteger("condition", 1);
moveDir = new Vector3(-1, 0, 0);
moveDir *= speed;
moveDir = transform.TransformDirection(moveDir);
}
}
if (Input.GetKeyUp(KeyCode.A))
{
anim.SetBool("running",false);
bool walking = false;
anim.SetInteger("condition", 0);
moveDir = new Vector3(0, 0, 0);
}
if (Input.GetKey(KeyCode.D))
{
{
anim.SetBool("running",true);
bool walking = true;
anim.SetInteger("condition", 1);
moveDir = new Vector3(1, 0, 0);
moveDir *= speed;
moveDir = transform.TransformDirection(moveDir);
}
}
if (Input.GetKeyUp(KeyCode.D))
{
anim.SetBool("running",false);
bool walking = false;
anim.SetInteger("condition", 0);
moveDir = new Vector3(0, 0, 0);
}
}
moveDir.y -= gravity*
Time.deltaTime;
controller.Move(moveDir *Time.deltaTime);
void GetInput()
{
if(controller.isGrounded)
{
if(Input.GetMouseButtonDown (0))
{
if(anim.GetBool("running")== true)
{
anim.SetBool("running", false);
anim.SetInteger("condition", 0);
}
if(anim.GetBool("running")== false)
{
}
}
}
}
}
}
Comment
Hello, you can simply rotate the pivot of a camera and use the camera's local vectors to move the player or even rotate it. Your code won't allow this, because you're manually setting the velocity.