Vehicle move script snippet

 avatar
unknown
lua
2 years ago
5.8 kB
9
Indexable
----	
--Get Surface Normal
----
	local CurrentPlayerPos = propMotorcycle:GetWorldPosition()

	--Offset the raycast startpoint and endpoint, relative to the vehicles current VectorUP. ( subtract 600 units from the Z position )
	local VUP = propMotorcycle:GetWorldRotation() * Vector3.UP	
		
	--ignore the players roll  and update the offset calculation based on this-- The roll represents the vehicle leaning from side to side when turning and is mostly visual. 
	local Lean_From_Inputs = 
							Rotation.New(
							--get x rotation local to player foward based on inputs
								CoreMath.Lerp (
								propMotorcycle:GetWorldRotation ().x, (propLeanAngle*Steering_Inputs) 				
								+ (propLeanAngle*Steering_Inputs*velocity_normalized) 								
								+ (-20*Left_Airbrake)*lab_Magnitude
								+ ( 20*Right_Airbrake)*rab_Magnitude												
								+ wobbling,
								deltaTime* steering_speed),
								0,0)
														
							VUP = (propMotorcycle:GetWorldRotation()-Lean_From_Inputs) * Vector3.UP
	

	
	local direction = -VUP -- get the opposite direction
	local distance = 600
	local offset = direction * distance -- get the offset vector
	
	local UpOffset = -direction *100
	local Raystart = CurrentPlayerPos + UpOffset
	local targetPos = CurrentPlayerPos + offset -- calculate the target position
	
	
	local Road = World.Raycast(Raystart, targetPos, {shouldDebugRender = true, ignorePlayers = true , debugRenderDuration = 2, debugRenderThickness = 5})
	
	if Road then 

		if Road.other.name ~= "Road" then
			
			local RoadAll = World.RaycastAll(Raystart, targetPos, {shouldDebugRender = true, debugRenderDuration = 2, debugRenderThickness = 5, ignorePlayers = true})
			local threshold = 1
			
			for index, HitResult in pairs(RoadAll) do
				if HitResult.other.name == "Road" then
				Road = HitResult
				threshold = threshold - 1
				end
			end
			
			if threshold == 1 then 
			Road = nil
			end
			
		end
	end
----
--use surface normal to align vehicle to surface
----	
	if Road then 
	surfaceNormal = Road:GetImpactNormal()	
	end

	if Road and surfaceNormal then

		local VForward = propMotorcycle:GetWorldRotation()* Vector3.FORWARD
		 surfaceRotation = Quaternion.New(VUP, surfaceNormal)

	    -- apply the surface rotation to the player's orientation
	    targetOrientation = surfaceRotation * Quaternion.New(Vector3.FORWARD, VForward)

		--surfaceRotation = Rotation.New(targetOrientation:GetRotation().x,targetOrientation:GetRotation().y,propMotorcycle:GetWorldRotation().z)
		surfaceRotation = Rotation.New(targetOrientation:GetRotation().x,targetOrientation:GetRotation().y,targetOrientation:GetRotation().z)
	else
	-- lerp back to current gravity vector instead of 0,0,0 if in the air -- TODO
		surfaceRotation = Rotation.New(
								CoreMath.Lerp(surfaceRotation.x, 0, deltaTime),
								CoreMath.Lerp(surfaceRotation.y, 0, deltaTime),
								CoreMath.Lerp(surfaceRotation.z, 0, deltaTime)
								)
	end
	
----
--ROTATE THE VEHICLE
----

	Motorcycle_Rotation = propMotorcycle:GetRotation()
	
	--Roll
	local x_Value = 			
	CoreMath.Lerp (
				propMotorcycle:GetWorldRotation ().x, (propLeanAngle*Steering_Inputs) 				
				+ (propLeanAngle*Steering_Inputs*velocity_normalized) 								
				+ (-20*Left_Airbrake)*lab_Magnitude
				+ ( 20*Right_Airbrake)*rab_Magnitude												
				+ wobbling,
				deltaTime * steering_speed)	
	--Pitch			
	local y_Value = 0
	--Yaw
	local z_Value =   Motorcycle_Rotation.z +Steering_Inputs - (Left_Airbrake*velocity_normalized*lab_Magnitude) + (Right_Airbrake*velocity_normalized*rab_Magnitude)
	
	local InputRotations = Rotation.New(x_Value, y_Value, z_Value)
	
	propMotorcycle:SetRotation (
		Rotation.New (
			CoreMath.Lerp (
			propMotorcycle:GetWorldRotation ().x, (propLeanAngle*Steering_Inputs) 				-- base lean value when steering
		    + (propLeanAngle*Steering_Inputs*velocity_normalized) 								-- add more lean when going faster
			+ (-20*Left_Airbrake)*lab_Magnitude
			+ ( 20*Right_Airbrake)*rab_Magnitude												-- add more lean when airbraking but can still cancel eachother out
			+ wobbling,
			deltaTime * steering_speed),	
			CoreMath.Lerp ( propMotorcycle:GetWorldRotation ().y,surfaceRotation.y, deltaTime*20), 
			Motorcycle_Rotation.z + Steering_Inputs - (Left_Airbrake*velocity_normalized*lab_Magnitude) + (Right_Airbrake*velocity_normalized*rab_Magnitude)
			)

		)
		
----	
--Move the Vehicle	
----	
	
	if Road then
	local roadPosition = Road:GetImpactPosition()
	local distanceToRoad = (CurrentPlayerPos-roadPosition).size
					

	local distanceNormalized = CoreMath.Clamp(distanceToRoad, 0, 500)/500  -- adjusts the normal road height to target
	local roadDistanceMultiplier = road_gravity_curve:GetValue(distanceNormalized)*10*velocity_normalized
	local roadGravity = 1000*-roadDistanceMultiplier -- road gravity force	

	local GravityUp = (propMotorcycle:GetWorldRotation () * Vector3.UP)*roadGravity -- orient the gravity vector to the players Vector.UP

		LastVelocity = GravityUp + Vector3.New(
									velocityDirection.x*propMaxSpeed, 	
									velocityDirection.y*propMaxSpeed,	
									velocityDirection.z*propMaxSpeed
									)																																				
	propMotorcycle:MoveContinuous(LastVelocity)
	
	else  -- player is in the air -- add gravity for falling later and it will be relative to the current gravity field vector the player is in
	
	LastVelocity = Vector3.New(
					velocityDirection.x*propMaxSpeed, 	
					velocityDirection.y*propMaxSpeed,
					CoreMath.Lerp(LastVelocity.z, -16000, deltaTime/4)					 
					)
	propMotorcycle:MoveContinuous(LastVelocity)
	
	end
	
Editor is loading...