- Home /
Issue with clamping player to camera view
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
}
}
Answer by KellyThomas · Dec 11, 2013 at 04:01 AM
You are calling clamp at the start of your update.
This will correct the position from the last frame.
Move it to the end of the method and you have the behaviour you are expecting.
success! that worked, i've tried like 20 iterations of that script in different ways and haven't figured out what was wrong.
and to think it was that simple :\ ... thank you $$anonymous$$elly$$anonymous$$.
Answer by fdmurphy123 · Dec 11, 2013 at 03:35 AM
At the end of line #9 it says = 0; It should be = 1;
Or at least that is what it comes up with on $$anonymous$$e
line #9 is the clampOffset its set to 12.0f, im not sure what you mean by its = 0. if you mean line 8 thats a variable to store a timer for the speed multiplier. its set by the AdjustSpeed$$anonymous$$odifier() function, and takes a float as a parameter. it then subtracts time.deltatime from that every frame until it hits 0, then resets the multiplier to 1. it shouldnt have anything to do with the weird motion.
Your answer
Follow this Question
Related Questions
Sprite seems to lose frames when moving 0 Answers
Using Mathf.Clamp To Limit Camera Movement 0 Answers
Top down movement and rotation 2 Answers