Jump to content

How to get an object position from raycast?


Pilatus

Recommended Posts

How to get the position of the object returned by testPointToPoint?

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 object = mp.raycasting.testPointToPoint(position, farAway, [1, 16]); 

Got the code from here: https://wiki.rage.mp/index.php?title=Raycasting::testPointToPoint

Link to comment
Share on other sites

If the object is not undefined, you should get the object by its handle if its created by mp.objects.new.

let raycast = mp.raycasting.testPointToPoint(position, farAway, [1, 16]); // Your valid raycasted object
if (raycast && raycast.entity.handle) {
	let object = mp.objects.atHandle(raycast.entity.handle); // You'll get the object if you created it via mp.objects.new
}

Otherwise use its handle to use other object functions. In case you need its model you can do 

let raycast = mp.raycasting.testPointToPoint(position, farAway, [1, 16]); // Your valid raycasted object
if (raycast && raycast.entity.model) {
	let objectModel = raycast.entity.model; // Do what you want with it
}

 

Link to comment
Share on other sites

15 hours ago, SkyLake said:

If the object is not undefined, you should get the object by its handle if its created by mp.objects.new.


let raycast = mp.raycasting.testPointToPoint(position, farAway, [1, 16]); // Your valid raycasted object
if (raycast && raycast.entity.handle) {
	let object = mp.objects.atHandle(raycast.entity.handle); // You'll get the object if you created it via mp.objects.new
}

Otherwise use its handle to use other object functions. In case you need its model you can do 


let raycast = mp.raycasting.testPointToPoint(position, farAway, [1, 16]); // Your valid raycasted object
if (raycast && raycast.entity.model) {
	let objectModel = raycast.entity.model; // Do what you want with it
}

 

Isn't the handle returned by `raycast.entity` alone? No `.handle` property?

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...