- Home /
This question was
closed Apr 10 at 06:21 PM by
divinereignoflords for the following reason:
The question is answered, right answer was accepted
Question by
divinereignoflords · Apr 10 at 04:31 PM ·
cameraunity 2dmathf.clamp
Camera moving in both x and y directions when it should only move in y direction
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamMove : MonoBehaviour
{
[SerializeField]
float topLimit;
[SerializeField]
float botLimit;
public float speed = 50f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(new Vector3(0, -speed * Time.deltaTime, 0));
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(new Vector3(0, speed * Time.deltaTime, 0));
}
transform.position = new Vector3(Mathf.Clamp(transform.position.y, botLimit, topLimit),transform.position.x,transform.position.z);
}
}
whenever I press the up arrow or down arrow the camera moves in the x and y direction, any ideas, also when I get rid of the mathf.clamp code line the code works fine and the camera only goes up and down
Comment
just a guess, is the camera rotated? also, you seem to be swapping the x and y in the transform.position line.
no, the camera is not rotated, could you elaborate on how the x and y is being swapped I'm rather new to doing anything 2d
in new Vector3(Mathf.Clamp(transform.position.y, botLimit, topLimit),transform.position.x,transform.position.z);
I believe it should be new Vector3(transform.position.x,Mathf.Clamp(transform.position.y, botLimit, topLimit),transform.position.z);