Movement speed doesn't change with variable C#

Hey, I’m making a 2.5D puzzle platformer, and I have my character movement code working, with the exception of a variable speed. This is my code in C#:

using UnityEngine;
using System.Collections;

public class CharacterMovement : MonoBehaviour {

//Variables
public GameObject player;
public float turnspeed=180f;
public float speed = 6.0F;
public float jumpSpeed = 8.0F; 
public float gravity = 20.0F;
private Vector3 moveDirection, moveVector = Vector3.zero;


void Update() {
    CharacterController controller = GetComponent<CharacterController>();
    // is the controller on the ground?
    if (controller.isGrounded) {
        //Feed moveDirection with input.
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        moveVector = transform.TransformDirection(moveDirection);
        //Multiply it by speed.
        moveDirection *= speed;
		if (moveVector != Vector3.zero){
			if(Vector3.Angle (transform.forward, moveVector) > 179){
				moveVector = transform.TransformDirection (new Vector3(.01f, 0, -1));
			}
			player.transform.rotation = Quaternion.RotateTowards(player.transform.rotation, Quaternion.LookRotation (moveVector), turnspeed * Time.deltaTime);
		}
		//Jumping
        if (Input.GetButton("Jump"))
            moveDirection.y = jumpSpeed;
    }
    //Applying gravity to the controller
    moveDirection.y -= gravity * Time.deltaTime;
    //Making the character move
    controller.Move (moveDirection * Time.deltaTime);

}
}

The only difference in movement speed is whether or not the speed variable is =< 1. at any speed >1, it moves the same amount, regardless of if it’s 10 or 10,000. What am I doing wrong?

EDIT: There’s also a latency issue, where the character continues moving for a moment after releasing the movement button. Any help on that too would be greatly appreciated.

Half of your code didn't format... difficult to read the meaning is.

–

I can't see anything obvious... just start throwing Debug.Logs around all the places you use speed and update moveDirection. Make sure the values are what you expect them to be at each step. I assume you mean lateral (x,y) movement is limited, not the jumping?

–

Why u r Multiplyng moveDirection *= speed; here and then controller.Move (moveDirection * Time.deltaTime); here again as i am thinking its making the speed multiply at first and then reducing it again by multiplying it with Time.deltaTime

–

@Huacanacha, yeah, the (x,z) movement is limited, not the jumping. I'll try throwing some Debug.Logs. @AR_Rizvi, I know that it's reducing it, but even so, if I set the speed to 10000, I should still see a difference in speed than if I set speed to 10, and I don't. Even when I remove the "* Time.deltaTime," it doesn't make a difference.

–

1 Answer

1

moveDirection *= speed;

You apply speed here, ie 6. (above)

 controller.Move (moveDirection * Time.deltaTime);

You remove your speed from 6 to 0.25 or something less (in .Move), Time.deltaTime is the time in secs it took the last frame to complete, and its a small value, so your multiplying 6 * 0.1 (example) = 0.6, and deltaTime will be usually in that range (unless new stuff gets added), so you need to add the result after you multiply it.

Like so:
moveDirection += moveDirection * Time.deltaTime;

You have copy-pasted the example code from Unityscripts, controller, don’t modify something you don’t fully understand event wise. It will break.

if (moveVector != Vector3.zero){
         if(Vector3.Angle (transform.forward, moveVector) > 179){
          moveVector = transform.TransformDirection (new Vector3(.01f, 0, -1));
         }

This if block will nullify your moveVector’s X value to 0.01, which I have no clue, why you have put it in the same code block. Thats your next problem.