- Home /
2d game sidescroller camera UNITY 5
Hello. I never used 2d and i want to do a sidescroller camera for my characher. How do i do that? only in x. This is what i tried:
using UnityEngine;
using System.Collections;
public class CameraAnchor : MonoBehaviour {
public Transform player;
public float distance = 10f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void LateUpdate () {
transform.position = player.position + Vector3 (distance, 0, 0);
}
}
But it give me a error:
Expression denotes a type', where a
variable', value' or
method group' was expected
I hope someone can help me with this. I did try search in internet i keep getting the same error or the same code.
Answer by HarshadK · Jun 12, 2015 at 01:47 PM
Your line #17 should be:
transform.position = player.position + new Vector3 (distance, 0, 0);
Notice the new
keyword before the Vector3(...)
.
Ok no more errors BUT now the camera goes inside the sprite texture. (inside the object) when i change the distance it change the x (goes left or right) the distance can be fixed for me i just want sidescroller to x only.
The distance should be z property.
transform.position = player.position + new Vector3 (0, 0, distance);
No never$$anonymous$$d it did not fix it. It does not set x only it set the y and z too.
y is the problem now z and x is now ok. it sets y too and i dont want that. Tried edit it in the editor does not let me its froozen soo its being set by the code.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200916im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Choppy camera follow. 1 Answer
The New 2D Pixel Perfect Camera Package "Stretch Fill" Causes Major Lag 0 Answers
Camera Smooth Follow 2D from JS to C# 2 Answers
Multiple Cars not working 1 Answer