I’m trying to keep the player within a non-moving camera’s view, so that they can’t take the player off screen, and have it be resolution independent.
So far, the player is limited to the view of the camera. However, if im holding the move button down, the player moves slightly past the stopping point, and when I release the button it moves back to the stopping point. If the player is at the stopping point and I press the same direction key again it will scoot over past slightly and stay there until i release the key, Then it will move back to the limit.
Here is the script controlling the players movement.
using UnityEngine;
using System.Collections;
public class ShipMovement : MonoBehaviour {
public float moveSpeed = 3.0f; //Move speed modifier
public float speedModifier = 1.0f; //variable to store speed increase bonuses
private float speedModDuration = 0;
public float clampOffset = 12.0f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
screenPos.x = Mathf.Clamp(screenPos.x, 0f + clampOffset, Screen.width - clampOffset);
transform.position = Camera.main.ScreenToWorldPoint(screenPos);
if(Input.GetKey (KeyCode.D)) //Right movement
{
transform.position += Vector3.right * Time.deltaTime * moveSpeed * speedModifier;
}
if(Input.GetKey(KeyCode.A)) //Left movement
{
transform.position += Vector3.left * Time.deltaTime * moveSpeed * speedModifier;
}
if(speedModDuration > 0) //Decrements timer once activated
{
speedModDuration -= Time.deltaTime;
}
if(speedModDuration == 0) //normalizes speed when time is up
{
speedModifier = 1.0f;
}
if(speedModDuration <0) //resets timer if it drops below zero, to prevent shorted buffs
{
speedModDuration = 0;
}
}
public void AdjustSpeedModifier(float modifier, float time) //Function to adjust speed modifier
{
speedModifier = modifier;
speedModDuration += time; //+= allows stackable durations
}
}