using UnityEngine,
using System.Collections;
public class PlayerControl : MonoBehaviour
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
float x = Input.GetAxisRaw ("Horizontal");//the value will be -1, 0, or 1 (for left, no input, and right)
float y = Input.GetAxisRaw ("Vertical");//the value will be -1, 0, or 1 (for down, no input, and up)
//now based on the input we compute a direction vector, and we normalize it to get a unit vector
Vector2 direction = new Vector2 (x, y).normalized;
//now we call the function that computes and sets the player's position
Move (direction);
}
void Move(Vector2 direction)
{
//Find the screen limits to the player's movement (left, right, top, and bottom edges of the screen)
Vector2 min = Camera.main.ViewportToWorldPoint (new Vector2 (0, 0)); //this is the bottom-left point (corner) of the screen
} Vector2 max = Camera.main.ViewportToWorldPoint (new Vector2 (1, 1)); //this is the top-right point (corner) of the screen
}
//Find how far the player sprite can go without going out
max.x = max.x - 0.500f;//subtract the player's sprite half width
min.x = min.x + 0.500f;//add the player's sprite half width
max.y = max.y - 0.550f;//subtract the player's sprite half height
min.y = min.y + 0.550f;//add the player's sprite half height
//get the players current position
Vector2 pos = transform.position;
//Calculate the new position
pos += Direction * speed * Time.deltaTime;
//Make sure the new position is not outside the screen
pos.x = Mathf.Clamp (pos.x, min.x, max.x);
pos.y = Mathf.Clamp (pos.y, min.y, max.y);
//Update the player's position
transform.position = pos
}
}
}
Hello I have no I de how to ask or fix this can anybody help?