Continued
Movement & Time
Create a new script for player, and open it (Also create a new folder for it). The next step is to make my ship move. In visual studio which open script for me, add code as below:
player.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Move();
}
private void Move()
{
var deltaX = Input.GetAxis("Horizontal"); // Edit -> Project Setting -> Input Manager
var newXPos = transform.position.x + deltaX;
transform.position = new Vector2(newXPos, transform.position.y); // Change the position
}
}
Time.deltaTime
Now our player’s ship could move along the x-coordinate. But it moves a little bit fast. To modify this, we should use Time.deltaTime which make games frame-rate independent, it means no matter your machine runs fast or slow you will get same experience.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] float moveSpeed = 10f; // speed up otherwise too slow
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Move();
}
private void Move()
{
var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed; // Edit -> Project Setting -> Input Manager
//Debug.Log(deltaX);
var newXPos = transform.position.x + deltaX;
transform.position = new Vector2(newXPos, transform.position.y); // Change the position
}
}
And then add more codes for moving along the y coordinate.
ViewPortToWorldPoint()
Def: converts the position of something as it relates to the camera view, into a world space value. This API we can use for constraining the range of our space ship moving, so we won’t lose our ship sprite in the game:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] float moveSpeed = 10f;
float xMin;
float xMax;
float yMin;
float yMax;
// Start is called before the first frame update
void Start()
{
SetUpMoveBoundaries();
}
private void SetUpMoveBoundaries()
{
//throw new NotImplementedException();
Camera gameCamera = Camera.main;
xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x;
xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x;
yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y;
yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y;
}
// Update is called once per frame
void Update()
{
Move();
}
private void Move()
{
var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed; // Edit -> Project Setting -> Input Manager
var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax);
var newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin, yMax);
transform.position = new Vector2(newXPos, newYPos); // Change the position
}
}
Of course we can add a padding value into the file.