- Home /
super messed up speed
I have been working on this new script for my new controller for my fps Game. Right now I believe it will technically how I want it but its now working very fast, too fast!
The problem begins with the basic movement forwards and backwards I wanted to make the controller to be able to still have a little control while falling. Well I had some problems with collision (any object that I walked into I would just go through other than the ground) so I had to change the way my player moves. This is were my problems started. I fixed the collision problem but now my player is moving super fast (I've turned down the speed but I'd like it fixed before future issues) the gravity is having the same issue but the worst part is the jumping. No matter how low I turn it down it will either not go that far or transport above in about 1 or 2 frames. Can someone please find and fix these errors?
using UnityEngine;
using System.Collections;
public class basicmove : MonoBehaviour {
public bool NotClimbing = true;
public bool HasJumped = false;
public float speed = 10.0F;
public float sidestepSpeed = 6.0F;
public float gravity = 12.0F;
public float jumpSpeed = 8.0F;
public Vector3 Wall = Vector3.zero;
// this variable used to meen somthing else but I've neglected to change the name
//it does seem unessisary
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
moveDirection.y -= gravity * Time.deltaTime;// applying gravity
controller.Move(moveDirection);
if (controller.isGrounded)
{
if(NotClimbing){//you are walking
////////moving axis/////////
moveDirection = new Vector3
(Input.GetAxis("Horizontal")* sidestepSpeed, 0,
Input.GetAxis("Vertical")* speed);
moveDirection = transform.TransformDirection(moveDirection);
if (Input.GetButtonDown("Jump"))
{
HasJumped = true;
}
}
////////moving axis/////////
else //you are on the ground facing a wall
{
moveDirection = new Vector3
(Input.GetAxis("Horizontal")* sidestepSpeed, Input.GetAxis("Vertical")* speed,0);
moveDirection = transform.TransformDirection(moveDirection);
}
}
else{
////////moving axis/////////
HasJumped = false;
if(NotClimbing){
moveDirection = new Vector3
(Input.GetAxis("Horizontal")*(sidestepSpeed*2), 0,
Input.GetAxis("Vertical")* ((speed/2)-(speed/4)));
moveDirection = transform.TransformDirection(moveDirection);
}
else
{
////////moving axis/////////
moveDirection = new Vector3
(Input.GetAxis("Horizontal")* sidestepSpeed, Input.GetAxis("Vertical")* speed,0);
moveDirection = transform.TransformDirection(moveDirection);
} } }
void LateUpdate()
{
if(HasJumped){
if (NotClimbing){
moveDirection.y = jumpSpeed;}// jump movement
else{
moveDirection.z = (jumpSpeed * -1); //jump movement when climbing
}
}
}
}
Answer by Benproductions1 · Nov 10, 2012 at 07:18 AM
The problem is your not accounting for framerate. In your script your speed will always vary because Update is called every frame, but framerate isn't constant. To fix this there are 2 solutions:
Change Update to FixedUpdate, so that you fix the update rate to 50 times per second. The problem with this is, that at higher framerates the movement feels sluggish, your input axis and jump keys will be less accurate and might not call at all and it has a higher performance impact than Update.
The other solution is to incorporate Time.deltaTime into your movement calculations. Time.deltaTime is the time from now to the last frame in seconds. This means your speed will be set in seconds and will adjust to framerate.
Hope this helped, Benproductions1
this fixed half my problem but how do I apply this to the jumping.