Jump to content

Problem rotating player/peds


Lpsd

Recommended Posts

  • Administrators

I'm making a basic system where players can sit down. When I set the player/ped animation, I can rotate the player fine using `setElementRotation`

I need the player to have no collisions at this point, otherwise he'll get stuck between the table/chairs and glitch out. However when I use `setElementCollisionsEnabled`, it appears as though `setElementRotation` stops having any effect.

setPedAnimation(hitElement, "ped", "seat_idle")
setElementCollisionsEnabled(hitElement, false)
setElementRotation(hitElement, 0, 0, -90)

(doesn't matter in which order these lines are executed)

Any thoughts?

 

Edited by LopSided_
Link to comment
  • Administrators

@Zango

 

This sets up the possible locations of chairs based on a table location (stored in poker_tables["init_loc"]), and creates a colshape which is used to trigger code in core_c.lua (seat_handler function)

--world_init_s:r1 (15/03/2017)
-----------------------------
--- serverside world initialization
-----------------------------

local max_seats = 6

poker_tables = {}

poker_tables["init_loc"] = {
{1123.143, -11.800, 1002.085}
}

local remove_worldObjectIds = { 2188, 1978 }

function poker_init(max)
	chairs = {}
	for i, v in ipairs(poker_tables["init_loc"]) do	
		poker_tables[i] = {}
		poker_tables[i]["seats"] = max
		poker_tables[i]["seat1"] = {v[1] - 1.243, v[2], v[3]}
		poker_tables[i]["seat2"] = {v[1] - 1.053, v[2] - 1, v[3]}
		poker_tables[i]["seat3"] = {v[1] - 0.503, v[2] - 1.45, v[3]}
		poker_tables[i]["enter_col"] = createColSphere(v[1], v[2], v[3], 1)
		setElementInterior(poker_tables[i]["enter_col"], 12)
		addEventHandler ( "onColShapeHit", poker_tables[i]["enter_col"], function(hitElement)
			seat_handler(i, hitElement)
		end)
	end
end

function start()
	poker_init(max_seats)
end
addEventHandler ( "onResourceStart", getRootElement(), start )

 

aaand the core_c.lua:

--poker/core_c:r1 (18/03/2017)
-----------------------------
--- clientside poker core
-----------------------------

addEventHandler( "onClientResourceStart", getRootElement( ),
    function ()
		setElementFrozen(localPlayer, false)
		setElementCollisionsEnabled(localPlayer, true)
	end
) -- this is here just to ensure they're not frozen etc, probably doesn't have any effect so ignore it.

function seat_handler(theTable, hitElement)
	if getElementType(hitElement) == "player" then
	
		--give lua's math.random a little help
		local nil_roll = math.random(3)
		local nil_roll = math.random(3)
		local random_seat = math.random(3)
		local pos = poker_tables[theTable]["seat"..tostring(random_seat)]
		--------------------------------------
		
		setElementPosition(hitElement, pos[1], pos[2], pos[3])
		setPedAnimation(hitElement, "ped", "seat_idle")
		setElementRotation(hitElement, 0, 0, 128)		
		setElementCollisionsEnabled(hitElement, false)
		
	end
end

 

Link to comment
  • Discord Moderators

seat_handler function must also have been serverside? Since it's called from the event handler for onColShapeHit which is a serverside event.

I can't reproduce your issue. The rotation is set correctly for me. Rotation is relative to the world, so a value of 128 will always have the ped facing south west. For each of your seat positions, you need to find a rotation value.

Not related to your issue, but you run math.random a few times to get a fresh value. math.random finds values from a seeded generator, so if you use the same seed (which is default) you'll get the same sequence of "random" numbers. Use math.randomseed (read about it here) at the start of your script to set a new seed. Use a new number as seed each time you run your script to get a new sequence. Use for example getRealTime().timestamp (seconds since 1970) as a seed.

  • Like 1
Link to comment
  • Administrators

In my meta.xml, core_c.lua was being loaded as a server script, not client as it should have been. Not sure how I missed that one, fixed it now and put the code into core_s.lua instead. However as it was being loaded as a server script, it shouldn't of made a difference with regards to the problem.

One thing I didn't mention that I was using this in an interior, however I've tried without and still doesn't work.

Well I'm stumped, I don't understand how this can be working fine for you, and not for me.

Also yes, I'll need different rotations for each seat position. I'll be putting that into poker_tables along with other info, just not got around to it yet.

 

 

 

Link to comment
  • Discord Moderators

I've tried a few times and sometimes I can reproduce the issue.

Try setting a 5th argument (after the coordinates) in setElementPosition to false. It moves the player instead of teleporting, which preserves the current animation, and apparently the rotation as well. It fixes the issue for me.

Link to comment

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