Camera::getDirection: Difference between revisions

From RAGE Multiplayer Wiki
(Created page with "==Description== Get camera direction ==Syntax== <syntaxhighlight lang="javascript">camera.getDirection();</syntaxhighlight> ===Return value=== *'''Vector3''' ==Example== <s...")
 
No edit summary
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{ClientsideJsFunction}}
==Description==
==Description==
Get camera direction
Get camera direction
Line 9: Line 11:


==Example==
==Example==
<syntaxhighlight lang="javascript">
This function returns undefined or a valid result, if you point with your camera on something.
// todo
 
</syntaxhighlight>
{{ClientsideCode|
<pre>
function pointingAt(distance) {
    const camera = mp.cameras.new("gameplay"); // gets the current gameplay camera
 
    let position = camera.getCoord(); // grab the position of the gameplay camera as Vector3
 
    let direction = camera.getDirection(); // get the forwarding vector of the direction you aim with the gameplay camera as Vector3
 
    let farAway = new mp.Vector3((direction.x * distance) + (position.x), (direction.y * distance) + (position.y), (direction.z * distance) + (position.z)); // calculate a random point, drawn on a invisible line between camera position and direction (* distance)
 
    let result = mp.raycasting.testPointToPoint(position, farAway, [1, 16]); // now test point to point
 
    return result; // and return the result ( undefined, if no hit )
}
</pre>
}}
 
==See also==
==See also==
{{Camera_function_c}}
{{Camera_definition_c}}
[[Category:Clientside API]]
[[Category:Clientside API]]
[[Category:TODO: Example]]

Latest revision as of 12:29, 21 December 2018

Client-Side
Function

 JavaScript



Description

Get camera direction

Syntax

camera.getDirection();

Return value

  • Vector3

Example

This function returns undefined or a valid result, if you point with your camera on something.

Client-Side
function pointingAt(distance) {
    const camera = mp.cameras.new("gameplay"); // gets the current gameplay camera

    let position = camera.getCoord(); // grab the position of the gameplay camera as Vector3

    let direction = camera.getDirection(); // get the forwarding vector of the direction you aim with the gameplay camera as Vector3

    let farAway = new mp.Vector3((direction.x * distance) + (position.x), (direction.y * distance) + (position.y), (direction.z * distance) + (position.z)); // calculate a random point, drawn on a invisible line between camera position and direction (* distance)

    let result = mp.raycasting.testPointToPoint(position, farAway, [1, 16]); // now test point to point

    return result; // and return the result ( undefined, if no hit )
}

See also