FaceMouse
by Tom Perry · in Torque Game Builder · 05/31/2007 (4:36 pm) · 2 replies
Hello,
I'm having some trouble with the faceMouse behaviour. The code is below:
The behaviour works fine, except if you click the right mouse button and drag, the object no longer rotates to face the mouse, it just snaps right back when the right mouse button is released (turnSpeed = 0).
Can anyone tell me why? As I can't figure this out!
Cheers
Tom
I'm having some trouble with the faceMouse behaviour. The code is below:
//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
if (!isObject(FaceMouseBehavior))
{
%template = new BehaviorTemplate(FaceMouseBehavior);
%template.friendlyName = "Face Mouse";
%template.behaviorType = "Mouse Input";
%template.description = "Set the object to always face the mouse";
%template.addBehaviorField(turnSpeed, "The speed to rotate at (degrees per second). Use 0 to snap", float, 0.0);
%template.addBehaviorField(rotationOffset, "The rotation offset (degrees)", float, 0.0);
}
function FaceMouseBehavior::onBehaviorAdd(%this)
{
%this.owner.setUseMouseEvents(true);
}
function FaceMouseBehavior::onAddToScene(%this, %scenegraph)
{
%this.owner.setMouseLocked(true);
}
function FaceMouseBehavior::onMouseMove(%this, %modifier, %worldPos)
{
%vector = t2dVectorSub(%worldPos, %this.owner.position);
%targetRotation = mRadToDeg(mAtan(%vector.y, %vector.x)) + %this.rotationOffset;
if (%this.turnSpeed == 0)
%this.owner.setRotation(%targetRotation);
else
%this.owner.rotateTo(%targetRotation, %this.turnSpeed, true, false, true, 0.1);
}
function FaceMouseBehavior::onMouseDragged(%this, %modifier, %worldPos)
{
%this.onMouseMove(%modifier, %worldPos);
}The behaviour works fine, except if you click the right mouse button and drag, the object no longer rotates to face the mouse, it just snaps right back when the right mouse button is released (turnSpeed = 0).
Can anyone tell me why? As I can't figure this out!
Cheers
Tom
About the author
Torque 3D Owner Matthew Langley
Torque
function FaceMouseBehavior::onRightMouseDragged(%this, %modifier, %worldPos) { %this.onMouseMove(%modifier, %worldPos); }