---
# System prepended metadata

title: Unity Platform 02 Horizontl move
tags: [platform]

---

Unity Platform 02 Horizontl move
===

###### tags: `platform`

## LIST
1. [Horizontal Movement](#HM)
2. [Player Movement](#Plater)

## Content
### Horizontal Movement<a id="HM"/>
Inheritace class of `PhysicsObjs.cs` not only be affected by Gravity, but also own force or velocity(like jump).<br>Use `targetvelocity`, make this movement.

```csharp=
private void FixedUpdate()
{
    ~~~
    
	velocity.x = targetVelocity.x;
	Vector2 moveAlongGround = new Vector2(groundedNormal.y, -groundedNormal.x);

	Vector2 deltaPosition = velocity * Time.deltaTime;
	// Move first horizontal(real moving)
	Vector2 move = moveAlongGround * deltaPosition.x;
	Movement(move, false);
	// Move Gravity(yMove)
	move = Vector2.up * deltaPosition.y;
	Movement(move, true);
    ~~~
}
```

Set velocity's x_speed tartget's x_speed.<br>And real move direction with ground direction(like slope).
<br><br>

### Player Movement<a id="PM"/>
`Player class` is herritated from `PhysicsObj class`.<br>Player Control is attached by `update()`, lifecycle function.<br>`Input` get input of client, and change state of object(player).
```csharp=
protected override void ComputeVelocity()
{
	Vector2 move = Vector2.zero;
	move.x = Input.GetAxis("horizontal");

	if (Input.GetButtonDown("jump") && grounded)
		velocity.y = jumpTakeOffSpeed;
	//cancle jump
	else if (Input.GetButtonUp("jump"))
	{
		if (velocity.y > 0)
			velocity.y = velocity.y * .5f;
	}

	//object look right direction(with flipX)
	bool flipSprite = (spriteRenderer.flipX ? (move.x > 0.01f) : (MonoBehaviour.x < 0.01f));
	if (flipSprite)
	{
		spriteRenderer.flipX = !spriteRenderer.flipX;
	}

	//Set anim
	animator.SetBool("grounded", grounded);
	animator.SetFloat("velocity", Mathf.Abs(velocity.x) / maxSpeed);

	targetVelocity = move * maxSpeed;
}
```

Jump check  `grounded` flag and cancel calculate rate `* .5f`<br>Animation is one of example any possible changed.