- Home /
How come my character is moving so fast?
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
}
}
Answer by kramcomposer · Mar 06, 2013 at 06:51 AM
try putting this in:
_moveDirection = runMultiplier Time.deltaTime;
Answer by Chaos Warp · Mar 06, 2013 at 07:33 AM
Nevermind! lol, I solved it, I added it to the end of
_moveDirection = _myTransform.TransformDirection(_moveDirection).normalized * Time.deltaTime;
Thanks for the advice, totally forgot about Time.deltaTime.
Your answer
Follow this Question
Related Questions
Character moves slower after build 3 Answers
Can't change the speed of a character. 2 Answers
My Character is moving fine horizontally and vertically, but moves way too fast diagonally 2 Answers
How to move object along Spline at constant speed? 0 Answers
Is mixing movement types a bad thing to do? (Force and Translate) 1 Answer