Hello there
I am trying to fix this error(error CS1022: Type or namespace definition, or end-of-file expected) for a long time but i don’t know what is wrong. It would be nice if someone can help me.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
private float y = transform.position.y;
private float x = transform.position.x;
}
public GameObject car;
private float xPosition;
private float yPosition;
private float alpha;
// Update is called once per frame
void LateUpdate()
{
alpha = car.transform.rotation.y;
xPosition = Mathf.Cos(alpha) * 7;
yPosition = Mathf.Sin(alpha)* 7;
x = car.transform.position.x + xPosition;
y = car.transform.position.y + yPosition;
}
}
If you looked at the line numbers on the error (you didnt supply here) it almost certainly points to the code on line 11 here, which says private float y … you cant declare private/public inside methods.
the code looks like this now but now i get an other error: Assets/Scrpts/CameraFollow.cs(27,30): error CS0019: Operator ‘+’ cannot be applied to operands of type ‘Vector3’ and ‘float’
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
public GameObject car;
private float xPosition;
private float yPosition;
private float alpha;
// Update is called once per frame
void LateUpdate()
{
alpha = car.transform.rotation.y;
xPosition = Mathf.Cos(alpha) * 7;
yPosition = Mathf.Sin(alpha)* 7;
transform.position = car.transform.position + xPosition;
transform.position = car.transform.position + yPosition;
}
}
don’t make sense. transform.position is a Vector3 property. So whatever you assign to this property has to be a Vector3. car.transform.position is also a Vector3. However xPosition is just a float variable. As the error tells you, it makes no sense to add a Vector3 and a float together. A Vector3 is 3 floats combined. You probably wanted to do
transform.position = car.transform.position + new Vector3(xPosition, yPosition, 0);
However apart from that syntactical issue, this line also doesn’t make sense:
alpha = car.transform.rotation.y;
The rotation property is a Quaternion and it’s y component is not an angle. If you want the camera being offset behind the car on the x-z- plane, you probably want to do something like: