Last active
May 24, 2016 05:58
-
-
Save arunkarnann/b95982fe496bee605aa1 to your computer and use it in GitHub Desktop.
A simplest ever Third person controller for standalone Unity3d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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