Page 1 of 1

Push a player to a given point

Posted: Sat Dec 02, 2017 8:13 am
by Kami
Hey guys, I've been trying to recreate the old est_pushto effect but I got strange behavior.

This is what I tried:

Syntax: Select all

@ServerCommand('pushto')
def pushto(command):
userid = int(command[1])
x1 = float(command[2])
y1 = float(command[3])
z1 = float(command[4])
vec = Vector(x1,y1,z1)
player = Player(index_from_userid(userid))
player.set_property_vector('m_vecBaseVelocity', Vector(x1,y1,z1))


The problem I encounter is, that the point I'm getting pushed to seems to stay the same most of the time, only changing for reasons unknown to me.

Maybe you guys have a better insight what I'm doing wrong. Thank you :)

Re: Push a player to a given point

Posted: Sat Dec 02, 2017 8:36 am
by iPlayer
Hey, Kami

The main issue here is that your code doesn't push player towards any particular location. Instead it treats your coordinates as a vector to add to the player velocity.

See, if you use type pushto 2 0 0 1000 (2 is my userid), your code will just add the vector (0, 0, 1000) to player's current velocity. That means that if a player was standing still, he'd get pushed straightly upwards, and his initial speed will be 1000. The point of the map located at (0, 0, 1000) doesn't play any role in your implementation.

Try this:

Syntax: Select all

from commands.typed import TypedSayCommand
from mathlib import Vector
from players.entity import Player


@TypedSayCommand('!pushme')
def say_pushme(command_info, x:float, y:float, z:float):
player = Player(command_info.index)
player.teleport(None, None, Vector(x, y, z) - player.origin)


Usage:
!pushme 0 0 0 (chat command)

This will push the player towards point at (0, 0, 0). Depending on how far that point is from the player, pushing velocity will vary.

I use player.teleport to set velocity to players. Its arguments are new location, new angle and new velocity. I don't need to change player's location or angle, so I just pass None.

But be aware that player.base_velocity (that's a shortcut for what you use in your code) will add the velocity; player.teleport sets the new velocity. So in my code it doesn't matter what the player velocity was before they typed the command.

If you need their old velocity to be taken into account, you can do it this way:

Syntax: Select all

player.teleport(None, None, player.velocity + Vector(x, y, z) - player.origin)

Re: Push a player to a given point

Posted: Sat Dec 02, 2017 8:54 am
by Kami
This works just as I want it to, thank you very much!

Re: Push a player to a given point

Posted: Sat Dec 02, 2017 8:59 am
by Ayuto
I prefer calling the teleport function like this:

Syntax: Select all

player.teleport(velocity=Vector(x, y, z) - player.origin)

It's a little bit easier to read.

Re: Push a player to a given point

Posted: Sat Dec 02, 2017 9:01 am
by iPlayer
Oh yeah, I forgot that it's wrapped now.

Re: Push a player to a given point

Posted: Sat Dec 02, 2017 9:33 am
by L'In20Cible
I would rather recommend using Player.base_velocity which applies the given Vector and doesn't requires a dynamic call.