# Solved Player speed slows when player looks down - Vectors

Discussion in 'Spigot Plugin Development' started by ProSl3nderMan, May 12, 2016.

1. ### ProSl3nderMan

Hello, today I got a problem with using vectors on players. What I am trying to do is send the player forward on a sheep. This works, but when I look upwards or downwards, I slow down. Can anyone give me a solution for this? This is my code:

Code (Text):
Vector dir = p.getLocation().getDirection();
Vector vec = new Vector(dir.getX() * 0.8D, 0.0D, dir.getZ() * 0.8D);
m.setVelocity(vec);

2. ### MrBlobman

You need to normalize the look vector before multiplying it by your scaling factor.

• Agree x 1
3. ### xMrPoi

The direction vector is normalized by default.

When the player is looking down, the X and Z vectors are both 0 so you're basically setting the Player's speed to 0.

4. ### BillyGalbreath

Code (Text):

double pitch = (90 * Math.PI) / 180; // keep pitch locked on horizon
double yaw  = ((player_location.getYaw() + 90)  * Math.PI) / 180;

x = Math.sin(pitch) * Math.cos(yaw);
y = Math.sin(pitch) * Math.sin(yaw);
z = Math.cos(pitch);

Vector vector = new Vector(x, z, y);

Use that vector for your velocity. It's pitch is locked on on the horizon so looking up/down will not slow you down.

• Informative x 1
• Useful x 1
5. ### Yamakaja

Math.sin(pitch) is a really complicated way of saying "1" ... Really, it's just unnecessary computation.

Also you can make that much simpler:
Code (Text):

• Agree x 2
• Informative x 1
6. ### BillyGalbreath

While I agree, I must point out that I left as is to show the math and stick to the sourced material as much as possible. (Plus I just didn't think about shortening it )

7. ### MakingTheMagic

If this has been solved, don't forget to mark it as solved!

8. ### ProSl3nderMan

Yes, sorry, I'm not so much of a nub, just a busy coder Thanks everyone for the help!