- Home /
transform.position.x brings up error
I tried this code for a simple camera movement over a plane trying to get the camera on the same vertical level, but when I include either .x, .y, .z on the end of both transform. position or transform.eulerAngles I get this error...
Operator '+' cannot be used with a left hand side of type 'float' and a right hand side of type 'UnityEngine.Vector3'.
This is my code
var moveSpeed:float = 1.0;
var turnSpeed:float = 1.0;
function Update () {
if(Input.GetButton("Forward"))
{
transform.position.x += transform.forward * moveSpeed * Time.deltaTime;
}
if(Input.GetButton("Backward"))
{
transform.position.x += -transform.forward * moveSpeed * Time.deltaTime;
}
if(Input.GetButton("Left"))
{
transform.eulerAngles.z += transform.forward * turnSpeed * Time.deltaTime;
}
if(Input.GetButton("Right"))
{
transform.eulerAngles.z += -transform.forward * turnSpeed * Time.deltaTime;
}
}
Answer by EHogger · Feb 12, 2013 at 12:51 AM
As the error states, you're mixing up floats and vectors.
Rather than directly changing the value of .x , .y , .z etc, you should probably use functions like Transform.Translate and Transform.RotateAround
http://docs.unity3d.com/Documentation/ScriptReference/Transform.Translate.html http://docs.unity3d.com/Documentation/ScriptReference/Transform.RotateAround.html
Example from your script..
if(Input.GetButton("Forward"))
{
transform.Translate(transform.forward * moveSpeed * Time.deltaTime);
}
if(Input.GetButton("Left"))
{
transform.RotateAround(transform.postion, Vector3.up, turnSpeed * Time.deltaTime);
}