- Home /
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. $$anonymous$$ake 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 $$anonymous$$ultiplyng
moveDirection *= speed; here and then
controller.$$anonymous$$ove (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.
Answer by Starwalker · Nov 08, 2013 at 08:51 PM
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.
As for the first part, that shouldn't matter if I set speed to something crazy like 10,000, but it still moves the same speed as if I set it to 10. As for the second part, I freely admit that I don't fully understand how that code works, however I understand it enough that the modifications I've made work for what I need it to do. The variable moveVector has nothing to do with the actual movement of the controller, so it doesn't really matter what I put there anyway, does it?
EDIT: after playing around with that bit of code, and removing it, my model still does what I want it to, so Idk why it was there in the first place.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Movement of increased certain speed type not registering. 0 Answers
Bullet not moving from script 3 Answers
Can't change the speed of a character. 2 Answers
Smooth Movement with set distance 1 Answer