facebooktwittergoogle_pluspinterestyoutubeby feather

In the last article I introduced our new game we are currently developing: Erwin’s Timewarp. The game is designed to run on mobile devices and one of the basic questions for this kind of games is how to let the user control the character.

The solution commonly used is controlling the player with a touch-control knob on the left lower side of the screen, a second solution used by a wide range of games is to let the character move to the position the user clicked on.

We had the charming idea of combining these tow possibilities: The click to move-approach and the knob as controller. When the character is moving by clicking to a destination point you can always interfere them movement by using the knob. This kind of controlling is very comfortable in my opinion.

Controlling movement with different methods

Controlling movement with different methods

Here is a demo in which you can see the controller in action:

C# Click to Move Script

The game is not open source but I will always give you some tips and tricks and show some of the scripting code we are developing. In this script I implemented the Click to Move behaviour. In the Start-method I store the rigidbody, animator and destination position in variables and set the global gravity:

/// <summary>
/// Get the rigidbody of the character and the Animator
/// </summary>
void Start()
{
	_rigidbody = GetComponent<Rigidbody>();

	_animator = PlayerTransform.gameObject.GetComponent<Animator>();

	Physics.gravity = new Vector3(0,-50.0f,0);

    _destinationPosition = transform.position;

}

In the FixedUpdate method I check if we are in the ClickToMove-mode and apply the motion and Run-animation in this case:

public void FixedUpdate()
{
    DoMoveTowards();
}

public void DoMoveTowards()
{
	if(_isMovingTowards)
	{
        _destinationDistance = Vector3.Distance(_destinationPosition, transform.position);
		
		if(_destinationDistance > .5f)
		{
			_isMovingTowards = true;
				
		}
		else
		{
			_isMovingTowards = false;
            return;
		}

        Vector3 vec = new Vector3(_destinationPosition.x, 0, _destinationPosition.z);

        _animator.SetBool("Jump", false);
	    _animator.SetFloat("MoveSpeed", 1.0f);

        transform.position = Vector3.MoveTowards(PlayerTransform.position, 
            _destinationPosition, MoveSpeed * Time.deltaTime);

        Vector3 rot = _destinationPosition - PlayerTransform.position;
        rot.y = 0;
        DoRotation(rot);
		
	}
}

To combine both methods of controlling the character we have to distinguish between 2 modes: When the character is controlled by „click to move“ the variable _isMovingTowards is set to true. When the user interacts with the knob-control, the variable is set to false:

/// <summary>
/// Raises the stick changed event when 
/// the position of the virtual joystick has changed
/// </summary>
/// <param name="distance">distance of the change</param>
/// <param name="stickPos">Stick position (coordinates)</param>
public override void OnStickChanged(float distance, Vector2 stickPos, bool stickPressed)
{
	if(stickPressed)
		_isMovingTowards = false;

	var movement = new Vector3(
		stickPos.x,
		0,
		stickPos.y);

	float speed = distance;

	// We have an animator so let's apply the states Run and Jump
	// by defining the parameters Jump and MoveSpeed
	if (_animator)
	{
		AnimatorStateInfo stateInfo = _animator.GetCurrentAnimatorStateInfo(0);			
	
		if(speed >= 0.3f)
		{
			_animator.SetBool("Jump", false);
			_animator.SetFloat("MoveSpeed", speed);

			movement *=speed;
						
			_rigidbody.MovePosition(PlayerTransform.position + (movement * MoveSpeed * Time.deltaTime))

		}
		else
		{
			if(!_isMovingTowards)
				_animator.SetFloat("MoveSpeed", 0.0f);
		}
	}
	DoRotation(movement);
}

The character has a Rigidbody component atached. When moving with the knob, this rigidybody is moved in the respective direction. When we move by clicking a destination-point, the player is moved with the method MoveTowards, which is a static method of the class Vector3.

The Unity3D-API is very handy to implement this special differentiation of control behaviour. The next step is to stop the movement on collisions. After that we enhance the level design and environment models. After that the gamelogic comes into play with new characters and AI.

I hope you like our game and the development hints, please subscribe to our channel and follow us.

facebooktwittergoogle_plusredditpinterestby feather