# Solved Vector + Entity right from Player

Discussion in 'Spigot Plugin Development' started by Preodym, Jun 29, 2021.

1. ### Preodym

Hi Guys,

Have a question again: D I have never worked with vectors therefore I need your help: D

I want to have an animal on the left side of the player. The animal should always be a block far away, left from the player. Does this work?

If so, someone would have a code or a code snippet for me?

2. ### cricri_21

You will have to detect when the player moves, so you can tp the mob on his left side -> Maybe PlayerMoveEvent, or a runnable running while the mob is alive (or during the timer is there is a timer).

To get the location on the left of the player, you will have to use an equivalent of local coordinates. If you are familiar with local coordinates, you will better understand, it's like "~ ~ ~" in game but it's ^ instead of ~.
Actually ~x ~y ~z is the classic coordinate system, you shift the X value (on the X axis) by x, same for Y and Z.
But ^x ^y ^z means the values depend on the eyes. The first, x, will shift location on the left, y will move up (so if the head is 90° lift, the y vector will be horizontal) and the third value, z, is just above the player.

(On the image, the first does not depend on the player eyes direction !)

If you want to fully understand the mecanic, open a solo world, give you a repeat command block and set the command execute at @p run particle flame ^ ^ ^1 0 0 0 0 1 force, then change "^ ^ ^1" to "^ ^1 ^", and finally to "^1 ^ ^" and see what happens with the particle.

What you want is exactly "^1 ^ ^".

Well, the code now. It's some manipulations with vectors. I think this is good to simulate a general approach of local coordinates :
Code (Java):
public static Location getLocalCoord(double x, double y, double z, Location origin) {
Location arrival = origin.clone();

org.bukkit.util.Vector dirX = new Location(arrival.getWorld(), 0, 0, 0, Location.normalizeYaw(arrival.getYaw()-90),
arrival.getPitch()).getDirection().normalize();
org.bukkit.util.Vector dirY = new Location(arrival.getWorld(), 0, 0, 0, arrival.getYaw(),
arrival.getPitch()-90).getDirection().normalize();
Vector dirZ = arrival.getDirection().normalize();

}
Call this method with [/ICODE]getLocalCoord(1, 0, 0, player.getEyesLocation());[/ICODE]. You can obviously simplify to avoid useless operations if it's always ^1 ^0 ^0.

Now, when you have your mob, you can get the coordinates on the left of the player with this method then teleport the mob. Repeat this while the player moves or while your system is running, and you will be good I think !

• Like x 1
• Informative x 1
3. ### DMan16

I suggest you use the player's own location (i.e. the body's) instead of the eye location (more realistic I think).
Other than that @cricri_21 gave an amazing answer!

4. ### cricri_21

Is there an easy way to get the body orientation and not the yaw / pitch of the head ?

5. ### DMan16

player.getLocation() will give the player's body info - coordinates (of feet - the block the lower part is in), and the yaw+pitch of the body

• Useful x 1
6. ### Preodym

thank you guy! Now i will try to understand that