Game Development Reference
In-Depth Information
To finish off this chapter, add a new C# script to the Scripts folder by navigating to
Create | C# script in the project window and saving it as Assets\Scripts\ Char-
acterMovement.cs . Open the script in the editor and replace its contents with the fol-
lowing script:
using UnityEngine;
public class CharacterMovement : MonoBehavior
{
// RigidBody component instance for the player
private Rigidbody2D playerRigidBody2D;
// For determining which way the player is currently
facing.
private bool facingRight;
// Speed modifier for player movement
public float speed = 4.0f;
//Initialize any component references
void Awake()
{
playerRigidBody2D =
(Rigidbody2D)GetComponent(typeof(Rigidbody2D));
}
// Update is called once per frame
void Update () {
// Cache the horizontal input.
float movePlayerVector = Input.GetAxis("Horizontal");
playerRigidBody2D.velocity = new
Vector2(movePlayerVector * speed,
playerRigidBody2D.velocity.y);
if (movePlayerVector > 0 && !facingRight)
{
Flip();
}
Search WWH ::




Custom Search