filipe moita filipe moita - 11 days ago 9
C# Question

Ping pong between 2 points fails

I'm trying to move a object between 2 points in unity3d, seems there are lot of answers to this topic but i always get a error that I don't know how to solve when I try to solve this, so actually I tried to do this:

void Update () {
transform.position = Vector3(Mathf.PingPong(Time.time,10.0f), transform.position.y, transform.position.z);
}


I get this error:


Assets/PingPong.cs(7,38): error CS0119: Expression denotes a
type
, where a
variable
,
value
or
method group
was expected


What am I doing wrong? I'm a beginner need some help with this stuff :/

Answer

To create a Vector3 with x, y and z value, you have to use the new keyword.

transform.position = new Vector3(Mathf.PingPong(Time.time, 10.0f), transform.position.y, transform.position.z);

One exception is when using the static Vector3 functions that returns predefined Vector3 values such as Vector3.zero,Vector3.back and the rest.

Vector3 is a struct datatype.You can learn why you need to use the new keyword when creating new Vector3(struct)here.