Jump to content

In front of a player


niemi

Recommended Posts

Okay, i want to spawn an object in front of a player, how can i do that? If i try this:

function spawnObj( source ) 
     local x,y,z = getElementPosition( source ) 
     createObject( 3267, x + 5, y, z ) 
end 
addCommandHandler( "samsite", spawnObj ) 

Sometimes it spawn the object next to him, not front, how can i do it?

Link to post
  
function spawnObj( source ) 
    local x,y,z = getElementPosition( source ) 
    local ang = getPlayerRotation( source ) 
    createObject( 3267, x+math.sin(math.rad(-ang)), y+math.cos(math.rad(-ang)), z ) 
end 
addCommandHandler( "samsite", spawnObj )  

Didn't test, but if I didn't make a mistake, it should work.

Link to post

Thx, it works, but it gets too close, if i'm trying to do this:

function pickup(player) 
    local x,y,z = getElementPosition(player) 
    local ang = getPlayerRotation(player) 
    createPickup(x+math.sin(math.rad(-ang)), y+math.cos(math.rad(-ang)), z, 0, 100) 
end 
addCommandHandler( "heal", pickup ) 

If i spawn it, it spawn it in front of me, but too close, because no one else can take it, because i take it automatically because it's too close, how can i get it a bit farther?

Link to post

Try this function: http://50p.pastebin.com/f419f3719

The code would be like this

function pickup(player) 
    local _,_,z = getElementPosition(player) 
    local x, y = getXYInFrontOfPlayer( player, 5.0 ) -- 5.0 is distance in front of player 
    local ang = getPlayerRotation(player) 
    createPickup(x, y, z, 0, 100) 
end 
addCommandHandler( "heal", pickup ) 

PS: Don't make 2 same topics, please.

Link to post

Is it strange that this guy gets a few replies within a couple of hours and I don't get a single example in any of the replies in my thread of a similar problem. Since people are in this thread I will post my problem.

Using Sin and Cos, X and Y, How do you position an object relative to a person/object. Any help would be appreciated.

Link to post
Thx, it works, but it gets too close, if i'm trying to do this:

function pickup(player) 
    local x,y,z = getElementPosition(player) 
    local ang = getPlayerRotation(player) 
    createPickup(x+math.sin(math.rad(-ang)), y+math.cos(math.rad(-ang)), z, 0, 100) 
end 
addCommandHandler( "heal", pickup ) 

If i spawn it, it spawn it in front of me, but too close, because no one else can take it, because i take it automatically because it's too close, how can i get it a bit farther?

createPickup(x+math.sin(math.rad(-ang))*5, y+math.cos(math.rad(-ang))*5, z, 0, 100)

As you can see, you only have to multiply values math.sin and math.cos return.

Link to post
  • MTA Team
Is it strange that this guy gets a few replies within a couple of hours and I don't get a single example in any of the replies in my thread of a similar problem. Since people are in this thread I will post my problem.

Using Sin and Cos, X and Y, How do you position an object relative to a person/object. Any help would be appreciated.

No, its not strange. Yours involves much more complicated math. If anyone knew it properly perhaps they'd help.

Link to post
Is it strange that this guy gets a few replies within a couple of hours and I don't get a single example in any of the replies in my thread of a similar problem. Since people are in this thread I will post my problem.

Using Sin and Cos, X and Y, How do you position an object relative to a person/object. Any help would be appreciated.

In what relative way?

Link to post
Is it strange that this guy gets a few replies within a couple of hours and I don't get a single example in any of the replies in my thread of a similar problem. Since people are in this thread I will post my problem.

Using Sin and Cos, X and Y, How do you position an object relative to a person/object. Any help would be appreciated.

No, its not strange. Yours involves much more complicated math. If anyone knew it properly perhaps they'd help.

How is it more complicated?

Perhaps I don't understand the complexity of it.

What level of maths are we talking A Level? Degree?

Link to post
Is it strange that this guy gets a few replies within a couple of hours and I don't get a single example in any of the replies in my thread of a similar problem. Since people are in this thread I will post my problem.

Using Sin and Cos, X and Y, How do you position an object relative to a person/object. Any help would be appreciated.

No, its not strange. Yours involves much more complicated math. If anyone knew it properly perhaps they'd help.

How is it more complicated?

Perhaps I don't understand the complexity of it.

What level of maths are we talking A Level? Degree?

Explain a bit more.

I don't really know what you're trying to do but if I understand well enough, you want to set object's position but the "starting point" is eg. player's position? So, let's say, when you create object its "starting point" is center of San Andreas, right? And you want that point to be position of player/vehicle? If that's what you want then level of maths required for this is E ¬¬ you only need to know what "+" does.

function setObjectPositionRelatively( object, relatedTo, x, y, z ) 
    local xx, yy, zz = getElementPosition( relatedTo ); 
    setElementPosition( object, xx + x, yy + y, zz + z ); 
end 
  
-- and client-side example of use: 
obj = createObject( 1111, 0, 0, 0 ); 
setObjectPositionRelatively( obj, getLocalPlayer( ), 0, 0, 10 ); 

This will set object 10 units above player. Doesn't matter where player is.

I didn't really understand your problem so I can't help you... try to explain a bit more next time ¬¬

Link to post

If what I posted isn't what you're looking for then I can't help you because I don't understand your problem...¬¬ I'm sorry but what I read in your topic is same as what I read here and that's exactly how I understood your problem... Talidan gave you another possible answer in his first reply about attachElementToElement. Maybe painting in Paint will help, if not, well, good luck then...

Link to post

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...