|
I am trying to simply character controller without using a rigidbody (since I don't need any physics.) The results are ok, but for some reason my gameObject moves faster when moving diagonally than when just heading in a single direction. Here is my code:
If anyone has any suggestions as to how I can get around this problem or perhaps even improve the above code, I'd really appreciate it!
(comments are locked)
|
|
Change the first three lines to this:
Since Input goes from -1 -> 1, you'll want to handle the case where x and y are both 1. The length of that is sqrt(2) if you do the trig. You want to just clamp it to 1. Thanks a lot, this works great. I also found this code on the forums which has much the same effect... I don't think one is any faster than the other but others may find this useful: float xPos = Input.GetAxis("Horizontal") * _player.speed * Time.deltaTime; float yPos = Input.GetAxis("Vertical") * _player.speed * Time.deltaTime; float inputModifyFactor = (xPos != 0.0f && yPos != 0.0f) ? .7071f : 1.0f; Vector3 newPos = new Vector3(xPos*inputModifyFactor, 0, yPos*inputModifyFactor);
Jun 20 '10 at 05:59 PM
Disaster
That will still have the same problem, except instead you'll max out at speed instead of sqrt(2) * speed.
Jun 20 '10 at 06:06 PM
Tetrad
(comments are locked)
|
|
Diagonally u mean when u press 2 keys like w + d in the same time? if yes, u can make a check and if it matches then to decrease the speed by 2.
(comments are locked)
|
