Using raycast normal

I’d like to correct you on a radian being 180 degrees. π rad = 180°, which means 2π rad = 360°.
https://www.mathsisfun.com/geometry/radians.html

I need to use orientation to use with my other code, how can i see where the player is facing without lookvector or the orientation?

isnt that just 1 raidan being 180 and being 360?

Pi is not equal to 1, so 1 radian is not 180 degrees.

1 Like

Yes but the code youve given there is just what is already done but in a function, how can i get the current players rotation and put it into the math?

In the video you can see that the player isnt completely “grounded” at all times to the slope like how you are on the flat ground,

what im asking to be able to find out is how to get the rotation of the character and put it in cause as you can see in the console of the video the orientations arnt changing as im rotating

	local cframe = CFrame.lookAt(raypos, raypos + raynormal)
	cframe *= CFrame.fromOrientation(math.rad(90), math.rad(180),0) + Vector3.new()
	local radx, rady, radz = cframe:ToOrientation()
	if not db then
		print(math.deg(radx),math.deg(rady),math.deg(radz))
		db = true
		wait(1)
		db = false
	end
	local balanceForceZ = bpidZ:Calculate(math.deg(radz),hrp.Orientation.Z,dt)
	local balanceForceX = bpidX:Calculate(math.deg(radx),hrp.Orientation.X,dt)
	vf.Force = Vector3.new(-balanceForceZ, 0,balanceForceX)*100

Hey there. I had a similar issue with my character controller about a year ago. It took me almost 2 months to figure it out by myself.

The main problem is that for a CFrame you need a Position Vector AND 2 Unit Vectors that are perpendicular to each other.
https://gyazo.com/cdb8244478edd2d9d8128d495075c961

Raycast can only give you a Position and a Normal (UpVector) So to get the last Vector you can do 2 different things.

  1. Use the WorldSpace of the Objects you are moving. That way it probably the correct way to do it however, I couldn’t work out the math and how I was able to something like that. (It is kind of a theory so I don’t even know if it would work.)

  2. Construct the CFrame at 90 degree angle then rotate downwards. That is the solution Icame up with. You can also use the 3rd argument in the CFrame.lookAt function to show the way in which the final CFrame will face.

There was actually a better way to do this but since for my use case I had to have the target object on 90 degree angles AND upside down I didn’t finish that way of doing it.

If you want rotation then you can either rotate the final CFrame or rotate the 3rd Vector paralel to the normal. (You can use the quaternion rotation method as it works incredibly well.)

Rotation: https://gyazo.com/321379e490f618aedfefab7fa319799a

90 degree angles: https://gyazo.com/84a0a4948c3f91c30dff786788c33b91

Upside down: https://gyazo.com/33df5d44bfc7e68eff3c8c8c15edfe6e

Code:

SensorPosition = CentralRaycastResult.Position
SensorNormal = CentralRaycastResult.Normal
TargetPosition = SensorPosition + (SensorNormal * VindicroyController.WalkHeight)
			
TargetAttachment.WorldCFrame = CFrame.lookAt(
	TargetPosition,
	TargetPosition + SensorNormal,
    -PreviousCFrame.LookVector -- This is how I get that 2nd vector
) * CFrame.Angles(
	-RAD_90,rad(Rotation * VindicroyController.RotationSpeed) * 2,0
)			
4 Likes

I would think you could fairly nicely use cross product to turn a global direction vector facing where you wanna go, and convert that to a rotation matrix thingy

local directionVector = --the input, where you wanna face, preferably a unit vector

local right = directionVector:Cross(normal)
local cframe = CFrame.fromMatrix(pos, right, normal) --normal is upvector 

You’d probably want this direction vector to have a Y component of 0 too, so it’s completely flat

If it wasn’t completely flat its possible it might start glitching, rotating away from the intended rotation of the input vector, I’m not entirely sure though

1 Like