- Home /
Problems Scripting a Camera
I'm trying to make a camera that follows a player character, but not exactly. First off I tried to make a script that would follow the player from an exact distance, I figured I could change it later. However It doesn't work?
using UnityEngine;
using System.Collections;
public class Camproper : MonoBehaviour {
public Transform target;
int distance = -10;
int lift = 2;
// Update is called once per frame
void Update () {
transform.LookAt (target);
transform.position = (target.position + Vector3(0,lift,distance));
}
}
Upon running this script i Receive an error,
"Assets/Camprpopr.cs(14,65):error CS0119: Expression denotes a 'type', where a 'variable', 'value' or 'method group' was expected"
This is extremely confusing to me, it seems it has a problem with "(target.position + Vector3(0,lift,distance))" and its saying that it requires a value. So I figured It might have a problem with my variables. To test that I rewrote the line to '(target.position + Vector3(1,1,1))', but the same error log persisted.
Why isn't vector3 accepting my variables/values?
Also I was thinking to only make the camera move when the player is a certain distance away. So if (Camera(x)- player(x))> 5 transform.Translate(Vector3.x + 1); So that the camera would only follow if the distance from it exceeded a set value
But I don't if/how to get the x variable out of the "transform" or "transform.position" Is there a way to get those numbers out so I could set 6 different floats(CamX,CamY,CamZ,TargetX,TargetY,TargetZ)
Answer by robertbu · Mar 30, 2014 at 02:02 AM
You need the 'new' keyword in C# when creating a Vector3:
transform.position = (target.position + new Vector3(0,lift,distance));
You can get the 'x' variable out by:
transform.position.x
Note the typical way of doing the distance calculation is to do:
if (Vector3.Distance(target.position, transform.position) < someValue) {
// Do something.
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
GUI cale and position according to the actual screen resolution. 1 Answer
Raycast Destroys player. 1 Answer
GUI.HorizontalSlider not working 2 Answers
Why is my camera switching angles? 0 Answers