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.
– HuacanachaI 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?
– HuacanachaWhy 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
– AR_Rizvi@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.
– Darmouth