Chapter 4 – Making it follow the mouse

December 18th, 2009 Leave a comment Go to comments

This is a short chapter. To make the player follow the mouse when the mouse button is held, we need to go back to the PlayerTick function. Determining if the mouse is hold and not clicked is tricky, we need to specify a time interval  for mouse down and mouse up. Replace the content of the PlayerTick function with this one :

/******************************************************************
 *
 *  TUTORIAL FUNCTION
 *
 *  PlayerTick is called once per frame
 *
 ******************************************************************/
event PlayerTick( float DeltaTime )
{
	super.PlayerTick(DeltaTime);

	//Set the location of the 3d marker that moves with the mouse.
	MouseCursor.SetLocation(MouseHitWorldLocation);	

	//We use the right mouse button to move, change it to suit your need !
	if(bRightMousePressed)
	{
		//accumulate the time for knowing how much time the button was pressed.
		DeltaTimeAccumulated += DeltaTime;

		//Update destination so that while holding the mouse down the destination changes
		//with the mouse move.
		SetDestinationPosition(MouseHitWorldLocation);

		//If its not already pushed, push the state that makes the pawn run to destination
		//until mouse is unpressed. Make sure we do it after the allocated time for a single
		//click or else two states could be pushed simultaneously
		if(DeltaTimeAccumulated >= 0.13f)
		{
			if(!IsInState('MoveMousePressedAndHold'))
			{
				`Log("Pushed MoveMousePressedAndHold state");
				PushState('MoveMousePressedAndHold');
			}
			else
			{
				//Specify execution of current state, starting from label Begin:, ignoring all events and
				//keeping our current pushed state MoveMousePressedAndHold. To better understand why this
				//continually execute each frame from our Begin: label, see
				//http://udn.epicgames.com/Three/MasteringUnrealScriptStates.html,
				//11.3 - BASIC STATE TRANSITIONS
				GotoState('MoveMousePressedAndHold', 'Begin', false, true);
			}
		}
	}

	//DumpStateStack();
}

Notice now that if DeltaTimeAccumulated is after the time taken into account in StopFire, we have a mouse button held down. Now because this executes each frame, we need to pop the next state only once to make the player move.

The use of !IsInState is handy to check if we are already in the said state. Remember this function is called each frame. Now the GotoState here is trycky. Since we are already in the MoveMousePressedAndHold state. We need to execute each frame from the begin label, else we are going to push a tremendous amount of states and at some point the player stop doing what you want and go in a straight line.

So to prevent this we simply instruct to jump to the Begin label, keeping the current stack the way it is and we do not fire state transitions events. I suggest you look up the function yourself to learn more. I commented the DumpStateStack() function. Its very usefull to debug stack of states at a given frame.

Add the related state :

/******************************************************************
 *
 *  TUTORIAL STATE (MoveMousePressedAndHold)
 *
 *  MoveMousePressedAndHold is the state when a mouse button is pressed
 *  and kept to move the pawn freely.
 *
 *
 ******************************************************************/
state MoveMousePressedAndHold
{
Begin:
	if(!bPawnNearDestination)
	{
		`Log("MoveMousePressedAndHold at pos"@GetDestinationPosition());
		MoveTo(GetDestinationPosition());
	}
	else
	{
		PopState();
	}
}
  • Compile and execute and voila !

 

Get chapter 4 source code.

 

 

Chapter 5 – Pathfinding for the champions

 

 

 

Update me when site is updated
Share and Enjoy:
  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • Turn this article into a PDF!
  • Twitter
  1. Indy
    May 1st, 2010 at 12:43 | #1

    Hi,
    i got funny things following your tutorial and replacing the right with the left mouse-button in playertick.
    Somehow it suddenly stays in MoveMousePressedAndHold – State, though the PopState event is correctly received in the state according to my debugs. This results in release the left-mouse-button and the pawn follows the mouse still, no matter what i do. I would say that i just did something wrong if it wasnt for the right-mouse-button just working. Did you try to use the left button for movement and it works for you?

    thx for your help in advance!

  2. Roychr
    May 2nd, 2010 at 12:44 | #2

    Yes I actually did try and it worked well for me, my first implementation was with the left button. Did you use search and replace ? Be sure to check that the left button is not used for something else such as firing the gun. But then again since everything is processed inside the frame function, it should be fine. Put some debug output to trace the value. I think there is an alternate way of doing mouse down with the input file directly but i am not sure, this method is really manual.

  3. Indy
    May 2nd, 2010 at 13:15 | #3

    @Roychr
    Thats exactly what i did now, with the
    Bindings=(Name=”LeftMouseButton”,Command=”leftMousePressed | OnRelease leftMouseReleased”)
    commands. This Maps to your PlayerInput-Class and calls the according functions (like “exec function leftMousePressed()”).
    But in fact, nice tutorials and thx for the answer :)

  4. Kevin
    July 15th, 2010 at 16:22 | #4

    Super!!! Amazing tutorials!!!

  1. No trackbacks yet.