I am following the tutorials at Burgzergarcade.com and I just set up the movement. The script itself works but it seems to make me move incredibly fast compared to the tutorial video. Sorry if the code is formatted weird. It might be super fast because the tutorial was not using Unity 4.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class AdvancedMovement : MonoBehaviour {
public float walkSpeed = 2; //the speed our character walks at
public float runMultiplier = 12; //how fast the player runs compared to the walk speed
public float strafeSpeed = 2.5f; //the speed our character strafes at
public float rotateSpeed = 250; //the speed our character turns at
public float gravity = 20; //the setting for gravity
public float airTime = 0; //how long have we been in the air since the last time we touched the ground
public float fallTime = 0.5f; //the length of time we have to be falling before the system knows its a fall
public float jumpHeight = 0.5f; //the height we move when we are jumping
public float jumpTime = 1.5f;
private CollisionFlags _collisionFlags; //the collisionFlags we have from last frame
private Vector3 _moveDirection; //this is the direction our character is moving
private Transform _myTransform; //our cached transform
private CharacterController _controller; //our cached CharacterController
//Called before the script starts
public void Awake(){
_myTransform = transform;
_controller = GetComponent<CharacterController>();
}
// Use this for initialization
void Start () {
_moveDirection = Vector3.zero;
animation.Stop ();
animation.wrapMode = WrapMode.Loop;
animation["GoodWalk"].layer = 1; //this is for the jump animation. I dont have a jump animation. Replace GoodWalk with Jump
animation["GoodWalk"].wrapMode = WrapMode.Once; //this is for the jump animation. I dont have a jump animation. Replace Goodwalk with Jump
animation.Play("GoodIdle");
}
// Update is called once per frame
void Update () {
if(Input.GetButton("Horizontal")) {
_myTransform.Rotate(0, Input.GetAxis("Horizontal") * Time.deltaTime * rotateSpeed, 0);
}
if(_controller.isGrounded){
airTime = 0;
_moveDirection = new Vector3(0, 0, Input.GetAxis ("Vertical"));
_moveDirection = _myTransform.TransformDirection(_moveDirection).normalized;
_moveDirection *= walkSpeed;
if(Input.GetButton ("Vertical")) {
if(Input.GetButton("Run")) {
_moveDirection *= runMultiplier;
Run ();
}
else {
Walk ();
}
}
else {
Idle ();
}
if(Input.GetButton("Jump")) {
if(airTime < jumpTime) {
_moveDirection.y += jumpHeight;
Jump();
}
}
}
else{
if((_collisionFlags & CollisionFlags.CollidedBelow) == 0){
airTime += Time.deltaTime;
if(airTime > fallTime) {
Fall();
}
}
}
_moveDirection.y -= gravity * Time.deltaTime;
_collisionFlags = _controller.Move(_moveDirection);
}
public void Idle() {
animation.CrossFade("GoodIdle");
}
public void Walk() {
animation.CrossFade("GoodWalk");
}
public void Run() { //Need a running animation
animation["GoodWalk"].speed = 1.5f; //Need a running animation
animation.CrossFade("GoodWalk"); //Need a running animation
}
public void Jump() { //Need a jump animaiton
animation.CrossFade("AxeSlash"); //Need a jump animation
}
public void Strafe() { //Need a strafe animation
animation.CrossFade("NormIdle"); //Need a strafe animation
}
public void Fall() { //Need a falling animation
animation.CrossFade("CombatStance"); //Need a falling animation
}
}