Jump to content

[Question] How to properly use math.cos and math.sin to set positions


kieran

Recommended Posts

I'll be straight up, took the following code straight off another topic from a while back where the guy was getting a ramp to spawn in front of the player, I modified it to move the player, but it moves them forwards and I want to move them backwards....  I am not the best at math but I am trying to learn from scripting, code to move player forwards is below.

local x, y, z = getElementPosition ( localPlayer ) 
local r = getPedRotation ( localPlayer ) 
x = x + math.sin ( math.rad ( r ) ) * 1.5 --What does this actually do?
y = y - math.cos ( math.rad ( r ) ) * 1.5 --And this? 
setElementPosition ( localPlayer, x, y, z ) 
setElementRotation ( localPlayer, 0, 0, r ) 

Can someone explain what's actually happening?  It works but my problem is dealing with: math.sin, math.cos and math.rad.  Any info would be greatly appreciated. :D

Link to comment
12 minutes ago, NeXuS™ said:

I'd rather start using OOP, as math.sin and math.cos are taught in school.

I've never touched OOP...  What would I need to know for this?  Only reason I ask is that I have made a script for a friend where the player climbs up a ladder, that part works fine, but when they go down the ladder (even if I spin  them 180 degrees) they glitch through walls and it's horrible. :/

Link to comment
  • Moderators

 

The value 1.5 represents the offset.

 

Math.sin and math.rad used on the rotation, represents a sort of vector direction of an axis. Which you can multiply by the offset to extend the range. (The original offset is 1 unit.)

 

You should start calculating from the ladder and not from the ped. Else indeed it might causes glitches like that.

Edited by IIYAMA
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...