Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is a good approach to differentiate between similar quadrants based on torso position?

Asked by 8 years ago

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/90would 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.

0
I'm lost. What end result do you want--not in terms of quadrants and distances and angles, but just what do you want? You want a part to be offset from your character in the nearest cardinal-direction? BlueTaslem 18071 — 8y
0
Firstly to everyone, I apologize if there was any confusion; anyways,me rotating my character is completely separate from the part in the workspace. Say I have my character on a base plate, and there is a part at 0,0,0. When I click my mouse and I'm facing West at that instant, the part does not move. Houlardy642 28 — 8y
0
My end goal is that for example, when facing 45 deg SW, the part should move in the opposite direction versus when the character is facing 45 deg NW. Studio will read it as -45 regardless of NW/SW, hence will cause the part to move in the same direction with the same magnitude. Houlardy642 28 — 8y
0
If I have my character face perfectly N and then I activate a left mouse event, a part, ex. some random brick that spawned at the origin, (0,0,0), will move to (0.5,0,0). 45 deg NW? Then the part should move from (0,0,0) to (0.25,0,0). If I face 45 deg SW? Then, I would want the part to move from (0,0,0) to (-0.25,0,0) but the problem is Studio will still read it as (0.25,0,0). Houlardy642 28 — 8y
View all comments (5 more)
0
Okay, so basically I want to reverse the direction the part would move, but Studio reads it as the same direction whetherharacter torso is facing 45 deg NW or 45 deg SW. Houlardy642 28 — 8y
0
So you want a part to move in the direction of your mouse ... ? BlueTaslem 18071 — 8y
0
No, not in the direction of my mouse; it is just that when I click my mouse it acts like an event. Say I click my mouse, and the output reads "-45" because of Player.Character["Torso"].Rotation.Y, due to my character's torso happened to face in that direction at that instant of time. Houlardy642 28 — 8y
0
So a part moves based on the captured value of the y-axis angle of rotation of my character's torso. The mouse helps to get the value, not determine the magnitude/ direction of the value. Houlardy642 28 — 8y
0
Hmm, I wish i could elaborate Houlardy642 28 — 6y

Answer this question