Help with Velocity script

Hey everyone!

Sorry if this is like super simple… But I’m trying to create a “blink” script.

Basically, there are two problems I’m running into.

Just for testing, I created a new vector with (0, -10000, 0), and then launch the player with self:GetEntity():Launch(vec)

The player “teleports” to the new location, but is immediately teleported back to the original location? Any ideas on why that happens?

The other issue is that the self:GetEntity():GetVelocity() always returns (0, 0, 0) even when the player is moving. Am I using the wrong function call?

Thanks! Any help is appreciated!

For the second Issue:
With self:GetEntity() you get the entity this script was added to. So my first guess would be if you added or connected the script correctly.

On the first Issue:
I would try self:GetEntity():SetPosition(self:GetEntity():GetPosition() + self.properties.vector)

I’ll try the set position!

As for the first, I’m almost positive it’s on the player/character object. (For example, the launch launches the character, I feel it wouldn’t do that if it was on the wrong entity). GetName() returns “dynamicPlayer#xxxx” which I assume is what happens when an entity is controlled by a player?

I guess I could also go the user route and do the getPlatwr to get the player entity.

I was playing around with trying to set the velocity of the player and saw the same issue as you, no matter what i do the velocity would would print out as 0,0,0, talking on discord to one of the devs they noted that the players physics may be handed different, Unsure yet if we can change the players velocity, playing around i was unable to but iv yet to find everything the game can do

Also a small note about creating a blink script by taking the position and adding to it, this does work but this will allow players to go through walls as nothing is checking if a wall is in the way, as i do not know the game you are making this may be what you want, but if you wish to avoid this you could when you wish to “blink” you create a raycast starting at PlayerPos and ending at PlayerPos + Vector, run that ray cast, if it hits nothing you are fine just move the player, if you find that ray has hit something, you can change the players blink target to be the position of the target you have hit, making it act as if they stopped due to blinking into the object

Thanks for the tip!

Just to make sure I’m understanding the physics right, the getVelocity theoretically would give me the total direction that the player is moving right? The intent is to blink in the direction I’m moving.

Velocity most times is used more for physics, so to apply gravity you would do velocity = velocity + gravity

I feel movement would be dealt with in another way. and may not be shown in the velocity, but iv not done enough testing to be fully sure.

If you wanted to blink in the direction the player is facing you could use, Entity:GetForward, so
blinkEndLocation = position + (GetForward() * distanceToMoveWhenBlinking)

distanceToMoveWhenBlinking in this case will want to be a number, so having 100 would move you 100 units forward,

(A single voxel in Crayta appears to be 25 units )

Off the top of my heads i don’t know the best way to get the players move direction, will need some digging in the api, :slight_smile:

Just for posterity and others that see this thread!

Adam O. From Unit 2 shared this script
E.g:

function someScript:OnTick(deltaTime)
self.velocity = Vector.Distance(self.lastPos, self:GetEntity():GetPosition()) / deltaTime
self.lastPos = self:GetEntity():GetPosition()
end

He said that right now the only way to get a players velocity is essentially by getting and storing a players position and getting the diff!

1 Like

Just to clear things up a bit, the GetVelocity function should work on players but currently doesn’t - thanks for finding it! The lastPos - currentPos diff is the best solution for the time being, but we’ll be looking to fix that original issue so that GetVelocity() will work on player entities

1 Like

Could i just add Character:SetVelocity appears to also do nothing, is this the same issue or is something we should not be able to call?

Something you shouldn’t be able to call since the player velocity is driven through their built-in input system. You can use the SetMoveOverride() function if you want to force player movement. As part of that same bug though we’re going to add a better error/warning message if you try to use SetVelocity on a character