0
\$\begingroup\$

I have the following line of code from a project I'm working on:

this.transform.position += (keyMapping.Value * this.movementSpeed).Scale(this.transform.forward);

At a first glance, this line of code looks like it should work, yet, I get the following error when I try to run it:

error CS0019: Operator `+=' cannot be applied to operands of type `UnityEngine.Vector3' and `void'.

I'm stumped as to what might be causing this. How do I fix this?

\$\endgroup\$
  • \$\begingroup\$ Please note that, this keyword will refer to current instance of the "class" you are working on, which is a script. In your case, the code will work but you should use gameObject to refer the instance of GameObject you are working on. And you can use transform directly to refer the Transform assigned to the GameObject you are working on. \$\endgroup\$ – S. Tarık Çetin Jan 2 '16 at 22:17
4
\$\begingroup\$

Vector3.Scale with one parameter input does not return a value so you will need to apply Scale as a separate step.

var movement = (keyMapping.Value * this.movementSpeed);
    movement.Scale(this.transform.forward);
transform.position += movement;

http://docs.unity3d.com/ScriptReference/Vector3.Scale.html

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.