2D Platformer Camera follow on y and continue to go right on x
I want my camera to keep moving right on the x Axis but i also want it to follow the player on the y axis answer please in C# as i am new to scripting and only know that
Answer by ComradeVanti · Mar 19, 2016 at 02:34 PM
Add this to you cameraobject
using UnityEngine;
using System.Collections;
public class CameraMover : MonoBehaviour
{
public Transform playerTrans; // Transfrom of the player
public float xSpeed; // The speed you want your camera to have
void Update ()
{
transform.Translate (new Vector3 (xSpeed * Time.deltaTime, transform.position.y - playerTrans.position.y, 0));
}
}
Good luck! :)
I get an error when adding it to the camera. transform.position assign attempt for '$$anonymous$$ain Camera' is not valid. Input position is { 0.790000, Infinity, -5.000000 }. UnityEngine.Transform:Translate(Vector3) Camera$$anonymous$$ovement:LateUpdate() (at Assets/Scripts/Camera$$anonymous$$ovement.cs:16) The Camera just keeps going up.
Your answer
![](https://koobas.hobune.stream/wayback/20220612072356im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D collider is not working on instantiated gameobjects. 0 Answers
what is the most efficient way to create a map for a 2D endless runner? 0 Answers
2D Object Pickup - Occasionally adding score twice 0 Answers
Character hits invisible ghost collision when jumping against objects while pushing against them 0 Answers
Nullreference on mouseHit.collider.name using raycasting 1 Answer