[Need Help] Looking for a way to slow down the player

My code looks like this and I am looking to add a movement feature but every way I try seems to fail please help
Attempted every way to add movment * Time.deltaTime here but I cant seem to get it to function properly

//sets up location of mouse
		float mousePos = Input.mousePosition.x / Screen.width * screenWidth;

		//sets a new location for the player
		Vector3 playerPos = new Vector3(mousePos, yPos, 0);
		//binds player in playspace
		playerPos.x = Mathf.Clamp(mousePos, leftBoundry, rightBoundry);
		//displays the new location of the player
		transform.position = playerPos;

Hi Jason,

You may run into a few unexpected issues with this.

For example, you could consider setting a multiplier in the Paddle class and then multiplying the results of the GetXPosition method by it;

[SerializeField]
private float speedMultiplier = 1f;
private float GetXPosition()
{
    if (gameSession.IsAutoPlayEnabled())
    {
        return ball.transform.position.x;
    }
    else
    {
        return (Input.mousePosition.x / Screen.width) * screenWidthInUnits * speedMultiplier;
    }
}

This does work, however, when you using lower speedMultiplier values the mouse will reach the edge of the screen but the paddle will be stuck somewhere in the play space and unable to move any further. The issue here is that there is a direct correlation between the mouse position and the paddle.

To separate this, try the following;

[SerializeField]
private float speedMultiplier = 1f;


private void Update()
{
    Move();
}

private void Move()
{
    Vector2 paddlePosition = new Vector2(transform.position.x, transform.position.y);
    Vector2 mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f));

    mousePosition.x = Mathf.Clamp(mousePosition.x, minPositionX, maxPositionX);
    mousePosition.y = paddlePosition.y;

    transform.position = Vector2.MoveTowards(transform.position, mousePosition, speedMultiplier);
}

The above determines where the mouse cursor is in world units using the camera’s ScreenToWorldPoint method.

Next we clamp the mouse position on the X axis between the existing values set previously.

We then use the Vector2.MoveTowards method to move the paddle towards the mouse position. With a speedModifier value 1f this seems fairly real time, if you lower the value, you will slow down the paddle movement. A downside of this method is that it isn’t limited to only the X axis, so it will try to move the paddle on the Y also, which is why we use the mousePosition.y = paddlePosition.y; statement to force the Y axis to remain as it was at the beginning.

The code within the Move method can be refined a little further, you don’t really need to create the paddle position, the only thing it is useful for at this time is holding the starting position on the Y axis for the paddle. You could instead create a private Vector2 within the Paddle class for this, e.g. it’s starting position, this may be useful for repositioning the player after they have lost a life for example.

With the speedModifier exposed to the Inspector using the [SerializeField] attribute you can experiment with speeds. Later, if you consider adding power-ups/power-downs, you could consider creating an initialSpeedModifier value so that you have a default, then perhaps a power-up could be collected which increases the speedModifier to x2, or a power-down which reduces the speedModifier to x 0.5 - if you have a timer running you could then restore the paddle movement to the initialSpeedModifier value so that the game returns to normal, effectively, ending the use of the power-up/power-down.

Hope this helps :slight_smile:


Updated Fri Dec 21 2018 11:42

I should have also added, the above no longer requires the GetXPosition method.

1 Like

Privacy & Terms