I have been working on developing a script that allows parts to move based on the direction that the player is facing. After experimenting different body parts of the player, I found that I could get values of the position of the torso most efficiently. I determined this by printing the rotation of the player every so often, and chose to work with the rotation of the torso about the y-axis; so, I developed a compass based on the torso rotation. Setting up a reference compass, I played in Studio and made the character face in different directions along with their outputted rotation values with print(Player.Character["Torso"].Rotation)
where local Player = game.Players.LocalPlayer
.
I found out that facing North (arbitrary), the angle was at 0 degrees. As I turned West, the angle decreased and the value exactly West was -90 degrees (Quadrant II). For NE (Quadrant I), the angle increased from 0 to +90 degrees. Going from West to South (Quadrant III), the angle decreased from -90 to 0 degrees, and East to South (Quadrant IV) the angle decreased from +90 degrees to 0 degrees. However, the problem is that it can be hard to differentiate between the second and third quadrants or the first and fourth quadrants because they are within the same range (for example, 0 to 90 for two and 0 to -90 for two, not 0 to 360) so it can be confusing to tell if I need to use something similar to this system (you can draw this compass out to better visualize if needed). Following is my attempt and code, respectively, to apply this idea.
local Dir1= Player.Character["Torso"].Rotation.Y wait(0) local Dir2= Player.Character["Torso"].Rotation.Y if Dir1> Dir2 then xFinal= -xFinal elseif Dir1< Dir2 then xFinal= xFinal end
The code above represents rotation of the torso at different points in time, as I was trying to detect an increase or decrease in the y-axis torso angle; if the angle decreased then the distance that the part would move would be negative, and vice versa, if there is a dispute between which quadrant should be applied.
--Quadrant II local xOriginal= 0.5--player facing perfectly west is the ideal position local xAngle= Player.Character["Torso"].Rotation.Y--angle of the torso about torso y-axis local xIncr= 0.5/90--the increment of the distance based on the torso angle local xFinal if xAngle> -90 and xAngle< 0 then xFinal= (90-math.abs(xAngle))*xIncr end workspace.PartP.Position= workspace.PartP.Position+ Vector3.new(xFinal,0,0)
The code above is the main script, a sample for the NW direction. Say that West is the ideal direction, which means that I do not intend to move the object (0.5 is 0 here, so nothing would be moved). I would then determine, that for every bit of angle that was rotated towards the North direction of the character torso, the part would move more and more, so local xIncr= 0.5/90
would determine each increment. if xAngle> -90 and xAngle< 0
makes sure the torso is facing Quadrant II, and xFinal= (90-math.abs(xAngle))*xIncr
means that the increment would be multiplied by the angle to get the final distance to shift the part to its desired location: workspace.PartP.Position= workspace.PartP.Position+ Vector3.new(xFinal,0,0)
. Just note that the distance for Quadrant III should be different than the distance for Quadrant II, if I decide to face in the SW direction. Also, you can just think of it as a mouse event, where if I do a left mouse-click while facing a particular direction, the part would move each time. I am not really sure on this one.