Question by
KennethfanofAkkek · Sep 02, 2017 at 09:27 AM ·
movement script3rd person controller
Third Person Controller That Faces The Direction He's Running
Ive been trying to figure out how to make it so that my main character faces the direction he happens to be running in
ive got his movement all worked out
but i cant work out how to make him face the direction he is running and make it so the animator and angle the model according to how he is turning
basically giving him a movement scheme much like mrio64 or legend of zelda wind waker
heres my script so far using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour {
//Player movement script Variables
public Transform myTransform;
public CharacterController CC;
public Transform myCamera;
public Animator Anim;
public Vector3 moveDirection;
public Vector3 cameraDirection;
public Quaternion orientation;
public float VectorZ = 0;
public float VectorX = 0;
public float JumpHeight;
public float MoveSpeed;
public float sprintBoost;
public float TurnSpeed;
public float Gravity;
public float YRot;
// Use this for initialization
void Start ()
{
myTransform = transform;
CC = myTransform.GetComponent<CharacterController>();
myCamera = transform.Find("Camera_Mount");
Anim = myTransform.GetComponent<Animator>();
moveDirection = new Vector3(0, 0, 0);
}
// Update is called once per frame
void Update ()
{
if(Input.GetButton("Fire2"))
{
LockedMovement();
}
else
{
UnlockedMovement();
}
}
void UnlockedMovement ()
{
VectorZ = Input.GetAxis("Vertical");
VectorX = Input.GetAxis("Horizontal");
CC.Move(moveDirection * Time.deltaTime);
if (CC.isGrounded)
{
//cameraDirection = Vector3.Scale(myCamera.forward, new Vector3(1, 0, 1)).normalized;
//moveDirection = VectorZ * cameraDirection * MoveSpeed + VectorX * myCamera.right * MoveSpeed;
//moveDirection = transform.TransformDirection(moveDirection);
Vector3 moveVectorX = cameraDirection * VectorZ;
Vector3 moveVectorY = cameraDirection * VectorX;
moveDirection = (moveVectorX + moveVectorY).normalized * MoveSpeed * Time.deltaTime;
myTransform.LookAt(myTransform.position + moveDirection);
Anim.SetBool("IsGrounded", true);
if (Input.GetButton("Jump"))
{
moveDirection.y += JumpHeight;
Anim.SetBool("IsGrounded", false);
Anim.SetTrigger("Jump");
}
}
else
{
moveDirection.y += Gravity;
Anim.SetBool("IsGrounded", false);
}
/*Vector3 newDirection = new Vector3(moveDirection.x, 0, moveDirection.z);
orientation = Quaternion.LookRotation(newDirection, Vector3.up);
Quaternion turnDirection = Quaternion.Lerp(myTransform.rotation, orientation, TurnSpeed * Time.deltaTime);
myTransform.rotation = turnDirection;*/
}
void LockedMovement ()
{
YRot = Input.GetAxis("Mouse X") * TurnSpeed;
CC.Move(moveDirection * Time.deltaTime);
Anim.SetFloat("Movement_Forward", VectorZ);
Anim.SetFloat("Movement_Right", VectorX);
myTransform.Rotate(0, YRot, 0);
Anim.SetBool("IsAttacking", Input.GetButton("Fire1"));
if (CC.isGrounded)
{
moveDirection = transform.right * VectorX + transform.forward * VectorZ;
Anim.SetBool("IsGrounded", true);
if (Input.GetButton("Jump"))
{
moveDirection.y += JumpHeight;
Anim.SetBool("IsGrounded", false);
Anim.SetTrigger("Jump");
}
}
else
{
moveDirection.y += Gravity;
Anim.SetBool("IsGrounded", false);
}
if (Input.GetButton("Sprint"))
{
VectorZ = MoveSpeed * sprintBoost * Input.GetAxis("Vertical");
}
else
{
VectorZ = MoveSpeed * Input.GetAxis("Vertical");
}
VectorX = MoveSpeed * Input.GetAxis("Horizontal");
}
}
I have tried a few things which will hopefully explain the mess
but most bits i have worked out
i just wanna make it so he turns to face the direction i have him moving in
any help would be great
By the way im using a character controller Not a rigidbody
Comment