using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour
{
[SerializeField] float speed;
float Xmax;
float Xmin;
float Ymax;
float Ymin;
// Start is called before the first frame update
void Start()
{
movementboundries();
}
private void movementboundries()
{
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.ViewportToScreenPoint(new Vector3(0, 1, 0)).y;
}
// Update is called once per frame
void Update()
{
var deltax = Input.GetAxis("Horizontal") * Time.deltaTime * speed ;
var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * speed;
var newYpos = Mathf.Clamp(transform.position.y + deltaY , Ymax, Ymin);
var newXpos = Mathf.Clamp(transform.position.x + deltax , Xmax,Xmin);
transform.position = new Vector2(newXpos, newYpos);
}
}
Hi Usman,
NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.
Before you try to solve this, I’d recommend to check your game objects first. The yellow message indicates a missing component, so look for “Missing” in your Inspector. Maybe the NullReferenceExceptions are caused by that.
See also:
- Forum User Guides : How to mark a topic as solved
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.