Skip to content

Instantly share code, notes, and snippets.

@arunkarnann
Last active May 24, 2016 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arunkarnann/b95982fe496bee605aa1 to your computer and use it in GitHub Desktop.
Save arunkarnann/b95982fe496bee605aa1 to your computer and use it in GitHub Desktop.
A simplest ever Third person controller for standalone Unity3d
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CapsuleCollider))]
[RequireComponent(typeof(CharacterController))]
public class GZThirdPersonSimplified : MonoBehaviour {
[SerializeField] float m_TurnSpeed = 20;
[SerializeField] float m_TurnAmount;
Vector3 RotationVector;
[SerializeField] Vector3 moveDirection = Vector3.zero;
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal") , 0, Input.GetAxis("Vertical") );
moveDirection = transform.TransformDirection(moveDirection);
RotationVector = moveDirection.x *Vector3.left + moveDirection.z*Vector3.forward;
if(moveDirection != Vector3.zero){
moveDirection = transform.forward ;
}
GetComponent<Animator>().SetBool("Walk",true);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
m_TurnAmount = Mathf.Atan2(RotationVector.x,RotationVector.z);
transform.Rotate(0, m_TurnAmount * m_TurnSpeed * Time.deltaTime, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment