SetElementVelocity: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
Fernando187 (talk | contribs) No edit summary |
||
(17 intermediate revisions by 12 users not shown) | |||
Line 1: | Line 1: | ||
{{Server client function}} | |||
__NOTOC__ | __NOTOC__ | ||
This function sets the velocity (movement speeds) along each axis, for an element. | |||
This function sets the velocity (movement speeds) along each axis, | |||
This is not compatible with all elements. Only the following element types are compatible: | |||
* [[Ped]] | |||
* [[Vehicle]] | |||
* [[Object]] | |||
* [[Projectile]] | |||
{{Deprecated feature|3.0140|1.4| | |||
Objects and projectiles velocity can only be set clientside.}} | |||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua">bool setElementVelocity ( element theElement, float speedX, speedY, speedZ )</syntaxhighlight> | <syntaxhighlight lang="lua">bool setElementVelocity ( element theElement, float speedX, float speedY, float speedZ )</syntaxhighlight> | ||
{{OOP||[[element]]:setVelocity|velocity|getElementVelocity}} | |||
===Required Arguments=== | ===Required Arguments=== | ||
*'''theElement''' | *'''theElement:''' The [[element]] you wish to set the velocity of. | ||
*'''speedX''' | *'''speedX:''' A floating point value determining the speed along the X axis. | ||
*'''speedY''' | *'''speedY:''' A floating point value determining the speed along the Y axis. | ||
*'''speedZ''' | *'''speedZ:''' A floating point value determining the speed along the Z axis. | ||
===Returns=== | |||
Returns ''true'' if the speed was set successfully, ''false'' if a bad element was specified or other bad arguments. | |||
==Example== | ==Example== | ||
<section class="server" name="Server" show=true> | |||
This example adds a function which copies the speed of a random player to another random player. If there are less than 2 players it returns ''false''. | |||
<syntaxhighlight lang="lua"> | |||
function equalTwoRandomPlayersVelocity() | |||
if getPlayerCount() < 2 then -- If there's only one player (or no players) this doesn't make sense | |||
return false | |||
end | |||
local randomPlayer1, randomPlayer2 = getRandomPlayer(), getRandomPlayer() -- Get two random players | |||
while randomPlayer1 == randomPlayer2 do -- Make sure the two players are different | |||
randomPlayer2 = getRandomPlayer() | |||
end | |||
local speedx, speedy, speedz = getElementVelocity (randomPlayer1) -- Get the velocity of the first random player | |||
setElementVelocity(randomPlayer2, speedx, speedy, speedz) -- Copy that velocity to the second random player | |||
outputChatBox("Now " .. getPlayerName(randomPlayer2) .. " runs as fast as " .. getPlayerName(randomPlayer1) .. "!", root, 255, 128, 0) | |||
return true | |||
end | |||
</syntaxhighlight> | |||
</section> | |||
<section class="client" name="Client" show=true> | |||
This example lets players jump their vehicle into the air (if they are the driver). | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function initBind() | |||
bindKey("lshift", "down", jumpKey) | |||
end | |||
addEventHandler("onClientResourceStart", resourceRoot, initBind) | |||
function jumpKey() | |||
if not isPedInVehicle(localPlayer) then return end | |||
local vehicle = getPedOccupiedVehicle(localPlayer) | |||
if vehicle and getVehicleController(vehicle) == localPlayer then | |||
local vehType = getVehicleType(vehicle) | |||
if vehType == "Plane" or vehType == "Helicopter" then return end | |||
local sx, sy, sz = getElementVelocity(vehicle) | |||
setElementVelocity(vehicle, sx, sy, sz + 0.33) -- The jump effect is achieved by raising the Z axis (height) coordinate | |||
end | |||
end | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | |||
==See Also== | ==See Also== | ||
{{ | {{element functions}} |
Latest revision as of 11:44, 14 October 2024
This function sets the velocity (movement speeds) along each axis, for an element.
This is not compatible with all elements. Only the following element types are compatible:
Syntax
bool setElementVelocity ( element theElement, float speedX, float speedY, float speedZ )
OOP Syntax Help! I don't understand this!
- Method: element:setVelocity(...)
- Variable: .velocity
- Counterpart: getElementVelocity
Required Arguments
- theElement: The element you wish to set the velocity of.
- speedX: A floating point value determining the speed along the X axis.
- speedY: A floating point value determining the speed along the Y axis.
- speedZ: A floating point value determining the speed along the Z axis.
Returns
Returns true if the speed was set successfully, false if a bad element was specified or other bad arguments.
Example
Click to collapse [-]
ServerThis example adds a function which copies the speed of a random player to another random player. If there are less than 2 players it returns false.
function equalTwoRandomPlayersVelocity() if getPlayerCount() < 2 then -- If there's only one player (or no players) this doesn't make sense return false end local randomPlayer1, randomPlayer2 = getRandomPlayer(), getRandomPlayer() -- Get two random players while randomPlayer1 == randomPlayer2 do -- Make sure the two players are different randomPlayer2 = getRandomPlayer() end local speedx, speedy, speedz = getElementVelocity (randomPlayer1) -- Get the velocity of the first random player setElementVelocity(randomPlayer2, speedx, speedy, speedz) -- Copy that velocity to the second random player outputChatBox("Now " .. getPlayerName(randomPlayer2) .. " runs as fast as " .. getPlayerName(randomPlayer1) .. "!", root, 255, 128, 0) return true end
Click to collapse [-]
ClientThis example lets players jump their vehicle into the air (if they are the driver).
function initBind() bindKey("lshift", "down", jumpKey) end addEventHandler("onClientResourceStart", resourceRoot, initBind) function jumpKey() if not isPedInVehicle(localPlayer) then return end local vehicle = getPedOccupiedVehicle(localPlayer) if vehicle and getVehicleController(vehicle) == localPlayer then local vehType = getVehicleType(vehicle) if vehType == "Plane" or vehType == "Helicopter" then return end local sx, sy, sz = getElementVelocity(vehicle) setElementVelocity(vehicle, sx, sy, sz + 0.33) -- The jump effect is achieved by raising the Z axis (height) coordinate end end
See Also
- attachElements
- createElement
- destroyElement
- detachElements
- getAttachedElements
- getElementAlpha
- getElementAttachedOffsets
- getElementAttachedTo
- getElementByIndex
- getElementByID
- getElementChild
- getElementChildren
- getElementChildrenCount
- getElementCollisionsEnabled
- getElementColShape
- getElementData
- getAllElementData
- hasElementData
- getElementDimension
- getElementHealth
- getElementID
- getElementInterior
- getElementMatrix
- getElementModel
- getElementParent
- getElementPosition
- getElementRotation
- getElementsByType
- getElementsWithinColShape
- getElementsWithinRange
- getElementType
- getElementVelocity
- getLowLODElement
- getRootElement
- isElement
- isElementAttached
- isElementCallPropagationEnabled
- isElementDoubleSided
- isElementFrozen
- isElementInWater
- isElementLowLOD
- isElementWithinColShape
- isElementWithinMarker
- setElementAlpha
- setElementAngularVelocity
- getElementAngularVelocity
- setElementAttachedOffsets
- setElementCallPropagationEnabled
- setElementCollisionsEnabled
- setElementData
- setElementDimension
- setElementDoubleSided
- setElementFrozen
- setElementHealth
- setElementID
- setElementInterior
- setElementModel
- setElementParent
- setElementPosition
- setElementRotation
- setElementVelocity
- setLowLODElement
- getPedContactElement
- getResourceDynamicElementRoot
- getResourceRootElement