Jump to content

Rotate Entity with Mouse Movement


Stona Persona
 Share

Recommended Posts

CamOffset = -5
CamRot = 0
CamHeight = 2

function Script:Start()
	
	PlayerCamera = Camera:Create()
	
	
	
	
end



function Script:UpdateWorld()
	
	PlayerCamera:SetPosition(self.entity:GetPosition().x, self.entity:GetPosition().y + CamHeight, self.entity:GetPosition().z + CamOffset)
	local window = Window:GetCurrent()
	local context = Context:GetCurrent()
	
	window:SetMousePosition(context:GetWidth()/2, context:GetHeight()/2)

	mx = context:GetWidth()/2
	my = context:GetHeight()/2

	CurrMPos = window:GetMousePosition()

	mxdiff = Math:Clamp(CurrMPos.x - mx,-45,45)
	
	PlAngle = Math:CurveAngle(self.entity:GetRotation().y + mxdiff, self.entity:GetRotation().y , Time:GetSpeed())
	local move = 0
	local strafe = 0
	
	self.entity:SetInput(PlAngle, move, strafe)
	

end

 

Hello guys, so, i have this above code where im trying to get an entity to rotate around its Y-axis when i move the mouse from side to side. It kinda works but the rotation is not smooth and the entity just jumps to rotation values instead of rotating to them smoothly. 

What am i doing wrong there? Thanks in advance for your time. 

  • Like 1
Link to comment
Share on other sites

Your going to want to use lerp to handle smoothing out the angle differences.

 


function LerpNumber(p0, p1, a)
	a = math.min(a, 1)
	return p0 + ((p1 - p0) * a)
end

 

p0  is your new angle projection

p1 is the current angle of the entity

and a is the speed at which you want to move into that new projection angle.

The function will return the amount to change the entity angle.

So basically, rough example : 

self.entity:SetInput(LerpNumber(currentY, mx, .1), move, strafe)

 

 

  • Like 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...