Xeno Posted January 2, 2012 Share Posted January 2, 2012 How can I get the position infront of the player? I mean like, if I was moving an object, how would I make it so that it moves FORWARD in the direction the player is facing. And how would I see if the object hit a wall? So if the object was to hit a wall ,it would stop. Thanks in advance, Xeno. Link to comment
Castillo Posted January 2, 2012 Share Posted January 2, 2012 Maybe this can help: https://wiki.multitheftauto.com/wiki/FindRotation Link to comment
Xeno Posted January 2, 2012 Author Share Posted January 2, 2012 This has totally confused me? I gave it ago... function predBomb(player) local x,y,z = getElementPosition(player) bomb = createObject(1337,x+1,y,z,findRotation(x,y,tx,ty) setTimer(setElementPosition,50,1,bomb,x+1,y,z) setTimer(setElementPosition,150,1,bomb,x+3,y,z) setTimer(setElementPosition,250,1,bomb,x+6,y,z) setTimer(setElementPosition,350,1,bomb,x+9,y,z) local xo,yo,zo = getElementPosition(bomb) setTimer(createExplosion,2000,1,xo,yo,zo, 0) end addCommandHandler("bomb", predBomb) Link to comment
Castillo Posted January 2, 2012 Share Posted January 2, 2012 There's an example to face another player: addCommandHandler("facePlayer", function (player, cmd, targetName) local target = getPlayerFromNick(targetName); if not target then outputChatBox("Invalid player", player); end local x,y,z = getElementPosition(player); local tx,ty,tz = getElementPosition(target); setPedRotation(player, findRotation(x,y,tx,ty) ); end ); Link to comment
Xeno Posted January 2, 2012 Author Share Posted January 2, 2012 I know, I just dont understand how im meant to make it move in that direction. Link to comment
JR10 Posted January 2, 2012 Share Posted January 2, 2012 function getPositionInfrontOfElement ( element , meters ) if not element or not isElement ( element ) then return false end if not meters then meters = 3 end local posX , posY , posZ = getElementPosition ( element ) local _ , _ , rotation = getElementRotation ( element ) posX = posX - math.sin ( math.rad ( rotation ) ) * meters posY = posY + math.cos ( math.rad ( rotation ) ) * meters return posX , posY , posZ end Link to comment
Xeno Posted January 2, 2012 Author Share Posted January 2, 2012 So that script will move the Element 3 meters ahead of the person? Link to comment
JR10 Posted January 2, 2012 Share Posted January 2, 2012 No that script will get you the position 3 meters ahead. You then need to use the returned values with setElementPosition. Link to comment
Xeno Posted January 2, 2012 Author Share Posted January 2, 2012 Sorry for my noobyness, but I've never done this before.. Something like this? function predBomb(player, element, meters) if not element or not isElement ( element ) then return false end if not meters then meters = 3 end local posX , posY , posZ = getElementPosition ( element ) local _ , _ , rotation = getElementRotation ( element ) posX = posX - math.sin ( math.rad ( rotation ) ) * meters posY = posY + math.cos ( math.rad ( rotation ) ) * meters return posX , posY , posZ bomb = createObject(1337,posX+1,posY,posZ) setTimer(setElementPosition,50,1,bomb,posX+1,posY,posZ) setTimer(setElementPosition,150,1,bomb,posX+3,posY,posZ) setTimer(setElementPosition,250,1,bomb,posX+6,posY,posZ) setTimer(setElementPosition,350,1,bomb,posX+9,posY,posZ) local xo,yo,zo = getElementPosition(bomb) setTimer(createExplosion,2000,1,xo,yo,zo, 0) end end end addCommandHandler("bomb", predBomb) Link to comment
JR10 Posted January 2, 2012 Share Posted January 2, 2012 function predBomb(player, _,name, meters) local target = getPlayerFromName (name) if not target or not isElement ( target) then return end meters = tonumber(meters) if not meters then meters = 3 end local posX , posY , posZ = getElementPosition ( target ) local rotation = getPedRotation( target ) posX = posX - math.sin ( math.rad ( rotation ) ) * meters posY = posY + math.cos ( math.rad ( rotation ) ) * meters bomb = createObject(1337,posX+1,posY,posZ) setTimer(setElementPosition,50,1,bomb,posX+1,posY,posZ) setTimer(setElementPosition,150,1,bomb,posX+3,posY,posZ) setTimer(setElementPosition,250,1,bomb,posX+6,posY,posZ) setTimer(setElementPosition,350,1,bomb,posX+9,posY,posZ) local xo,yo,zo = getElementPosition(bomb) setTimer(createExplosion,2000,1,xo,yo,zo, 0) end addCommandHandler("bomb", predBomb) Link to comment
Xeno Posted January 2, 2012 Author Share Posted January 2, 2012 Thanks, Only one problem, I get a bad arugement with getPlayerName. Link to comment
JR10 Posted January 2, 2012 Share Posted January 2, 2012 function predBomb(player, _,name, meters) local target = getPlayerFromPartialName (name,player) if not target or not isElement ( target) then return end meters = tonumber(meters) if not meters then meters = 3 end local posX , posY , posZ = getElementPosition ( target ) local rotation = getPedRotation( target ) posX = posX - math.sin ( math.rad ( rotation ) ) * meters posY = posY + math.cos ( math.rad ( rotation ) ) * meters bomb = createObject(1337,posX+1,posY,posZ) setTimer(setElementPosition,50,1,bomb,posX+1,posY,posZ) setTimer(setElementPosition,150,1,bomb,posX+3,posY,posZ) setTimer(setElementPosition,250,1,bomb,posX+6,posY,posZ) setTimer(setElementPosition,350,1,bomb,posX+9,posY,posZ) local xo,yo,zo = getElementPosition(bomb) setTimer(createExplosion,2000,1,xo,yo,zo, 0) end addCommandHandler("bomb", predBomb) function getPlayerFromPartialName ( name , player ) if name then local matches = { } for index , _player in ipairs ( getElementsByType ( "player" ) ) do if getPlayerName ( _player ) == name then return _player end if getPlayerName ( _player ) : gsub ( "#%x%x%x%x%x%x" , "" ) : lower ( ) : find ( name : lower ( ) ) then table.insert ( matches , _player ) end end if #matches == 1 then return matches [ 1 ] else if player then outputChatBox ( "Found " .. #matches .. " matches" , player , 255 , 0 , 0 ) end end end end Link to comment
Xeno Posted January 2, 2012 Author Share Posted January 2, 2012 No errors, it just doenst spawn the object. Link to comment
Kenix Posted January 3, 2012 Share Posted January 3, 2012 Check this: function predBomb( player, _,name, meters ) local target = getPlayerFromPartialName ( name,player ) if not isElement ( target ) then return outputChatBox( "can't find player" ) end local meters = tonumber( meters ) if not meters then meters = 3 end local posX , posY , posZ = getElementPosition ( target ) local rotation = getPedRotation( target ) posX = posX - math.sin ( math.rad ( rotation ) ) * meters posY = posY + math.cos ( math.rad ( rotation ) ) * meters local bomb = createObject( 1337,posX+1,posY,posZ ) outputChatBox( "Bomb return: "..tostring( bomb ) ) setTimer( setElementPosition,50,1,bomb,posX+1,posY,posZ ) setTimer( setElementPosition,150,1,bomb,posX+3,posY,posZ ) setTimer( setElementPosition,250,1,bomb,posX+6,posY,posZ ) setTimer( setElementPosition,350,1,bomb,posX+9,posY,posZ ) local xo,yo,zo = getElementPosition( bomb ) setTimer( createExplosion,2000,1,xo,yo,zo, 0 ) end addCommandHandler("bomb", predBomb) function getPlayerFromPartialName ( name , player ) if name then local matches = { } for index , _player in ipairs ( getElementsByType ( "player" ) ) do if getPlayerName ( _player ) == name then return _player end if getPlayerName ( _player ) : gsub ( "#%x%x%x%x%x%x" , "" ) : lower ( ) : find ( name : lower ( ) ) then table.insert ( matches , _player ) end end if #matches == 1 then return matches [ 1 ] else if player then outputChatBox ( "Found " .. #matches .. " matches" , player , 255 , 0 , 0 ) end end end end Link to comment
Xeno Posted January 3, 2012 Author Share Posted January 3, 2012 That outputs into the chat box: can't find player Link to comment
Kenix Posted January 3, 2012 Share Posted January 3, 2012 I test it and it working! In chat: /bomb SomeGuy Link to comment
Xeno Posted January 3, 2012 Author Share Posted January 3, 2012 OOH! I didn't think it would work like that.... I think you guys mis-understood what I am trying to do.. I was just trying to make it so when I type /bomb, an object shoots from the front of the player and goes in the direction of where there facing... So for example if I was facing north, the object would go north, if I was facing south, the object would go south, do you get it? Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now