0

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 :/

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

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.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Will fix the error, but doesn't explain or answer the question. – devRicher Nov 27 '16 at 20:23
  • @devRicher I just put a link to that since that part of question has been answered before. – Programmer Nov 27 '16 at 20:27
  • i got the answer, and it works kinda well, but how can i increase speed and control the begin and end point? –  Nov 27 '16 at 20:29
  • You can make a float variable that controls the speed then multiply it by `Mathf.PingPong` value. For example, `float speed = 100` then you can do `transform.position = new Vector3(Mathf.PingPong(Time.time, 10.0f) * speed, transform.position.y, transform.position.z);` – Programmer Nov 27 '16 at 20:31
  • hmm thank you very much that was great, and how can i control the begin and end point? –  Nov 27 '16 at 20:32
  • You can create a new question about that and also explain what you mean by end point. I think that the comment section is getting into other issues not related to the problem in the question – Programmer Nov 27 '16 at 20:35