[SOLVED] Need Help with finding the solution to my Camera Movement Script

Hello Everyone! So I am not an experienced C# programmer and I am struggling to find the solution to the issues I am having with the camera movement script that I tried to make myself. The issue is as follows:
“Assets/CameraMovement.cs(8,18): error CS1519: Unexpected symbol ;' in class, struct, or interface member declaration" when removing the suspected "Unexpected symbol" it would come up with another compile error saying this: " Assets/CameraMovement.cs(13,5): error CS1519: Unexpected symbol void’ in class, struct, or interface member declaration”.

When looking into this I understand that the missing “;” is the issue but it then comes up with the same error that is highlighted in red. I have tried everything but I think it’s best to get help from experts on these forums. The code is as follows:

using System.Collections;

using UnityEngine;

public class CameraMovement : MonoBehaviour {

    public float hspeed;
    public float vspeed;
    public CamPos
 


    // Update is called once per frame
    void FixedUpdate () {
        if (Input.GetKey(KeyCode.W)) {
            transform.CamPos.y = new Vector2 (0,vspeed * Time.deltaTime * 5);
        }

        if (Input.GetKey(KeyCode.S)){
            transform.CamPos.y = new Vector2 (0,vspeed * Time.deltaTime * -5);
        }

        if (Input.GetKey(KeyCode.A)) {
            transform.CamPos.x = new Vector2 (hspeed * Time.deltaTime * 5,0);
        }

        if (Input.GetKey(KeyCode.D)) {
            transform.CamPos.x = new Vector2 (hspeed * Time.deltaTime * -5, 0);
        }

       CamPos = Vector2(transform.position.x, transform.position.y);
    }
}

The aim of this code is to get the camera to move with the keys WASD, as my game is the only 2D - I don’t think I need Vector3 but I might be wrong. If you can help me out, I will appreciate that most definitely!

I think your problem is line 9 “public CamPos” is this supposed to be “public vector 2 CamPos;”

I have thought about that but it seems to come up with a new error in the compiler:

Assets/CameraMovement.cs(16,14): error CS1061: Type UnityEngine.Transform' does not contain a definition for CamPos’ and no extension method CamPos' of type UnityEngine.Transform’ could be found. Are you missing an assembly reference?

I know I need to refer CamPos but I don’t know where and what as.

Ok try this and see if it works for you. I just edited your code a little bit. To refer to “CamPos” you do not need to use “transform.CamPos” that was one of your problems. I am assuming you are setting the hspeed and vspeed in the editor screen yes? or else you will be multiplying by zero

using System.Collections;
using UnityEngine;
public class CameraMovement : MonoBehaviour {
    public float hspeed;
    public float vspeed;
    public Vector3 CamPos;
    // Update is called once per frame
    void FixedUpdate () {
        CamPos = Vector3.zero;
        if (Input.GetKey(KeyCode.W)) {
            CamPos.y = vspeed * Time.deltaTime * 5;
        }
        if (Input.GetKey(KeyCode.S)){
            CamPos.y = vspeed * Time.deltaTime * -5;
        }
        if (Input.GetKey(KeyCode.A)) {
            CamPos.x = hspeed * Time.deltaTime * 5;
        }
        if (Input.GetKey(KeyCode.D)) {
           CamPos.x = hspeed * Time.deltaTime * -5;
        }

       transform.position  += CamPos;
    }
}

Yes, it works! I was toying with it needing to be a Vector 3 but completely passed the idea of the last line. This is for a game prototype I am making for my college course, so I will make sure you get the credit for this one! :smile:

Glad I could help