-3
\$\begingroup\$

I'm getting the following error

Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable

with this code:

transform.position = new Vector3(target.position.x, transform.position.y, target.position.z); 
Vector3 desiredPosition = new Vector3(transform.position.x , target.position.y, transform.position.y ); 
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed); 
transform.position += smoothedPosition;
\$\endgroup\$
  • \$\begingroup\$ This needs substantial improvement as a question. \$\endgroup\$ – Stephan Dec 9 '17 at 17:28
  • \$\begingroup\$ I want the camera to have Lerp on the y axis but not on the x and z. \$\endgroup\$ – Vlad Ilisan Dec 9 '17 at 17:31
  • \$\begingroup\$ The code so far. transform.position = new Vector3(target.position.x, transform.position.y, target.position.z); //gameObject.transform.position.x = target.transform.position.x; //Vector3 actualPositionY = transform.position.y; Vector3 desiredPosition = new Vector3(transform.position.x , target.position.y, transform.position.y ); Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed); transform.position += smoothedPosition; } \$\endgroup\$ – Vlad Ilisan Dec 9 '17 at 17:31
1
\$\begingroup\$

If you want to modify the transform.position property, you have to modify the transform.position property: it's values are effectively read-only. This is due to the fact that Unity Vectors themselves are value types and not instanced objects. What you're trying to do is similar to doing 4 += 1 which makes no sense.

Vector3 pos = transform.position;
pos.x = target.transform.position.x;
transform.position = pos;
\$\endgroup\$
  • \$\begingroup\$ This is a bit offtopic...but how do I implement this to make so that I have smooth view on y but not on x and z.. \$\endgroup\$ – Vlad Ilisan Dec 9 '17 at 18:17
  • \$\begingroup\$ Vector3 pos = transform.position; pos.x = target.position.x; pos.y = target.position.y; pos.z = target.position.z + offsetZ; Vector3 desiredPosition = new Vector3(pos.x + offsetX, target.position.y, pos.z + offsetZ ); Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed); transform.position = desiredPosition; \$\endgroup\$ – Vlad Ilisan Dec 9 '17 at 18:18
  • \$\begingroup\$ You don't need desiredPosition there at all, that's what pos is doing. With your desiredPosition value, you've added offsetZ twice (you already added it to pos.z on the previous line). Then, you create a lerp value (smoothedPosition) and then don't do anything with it. \$\endgroup\$ – Draco18s Dec 9 '17 at 18:28
  • \$\begingroup\$ New code: Vector3 pos = transform.position; pos.x = target.position.x; pos.y = target.position.y; pos.z = target.position.z + offsetZ; Vector3 smoothedPosition = Vector3.Lerp(transform.position, pos, smoothSpeed); transform.position = smoothedPosition; \$\endgroup\$ – Vlad Ilisan Dec 9 '17 at 18:38
  • \$\begingroup\$ I dont want the x,z to have lerp, just the y...how do i do that? \$\endgroup\$ – Vlad Ilisan Dec 9 '17 at 18:39
0
\$\begingroup\$

You can only store whole vectors to gameObject.transform.position

Position is read-only in all other contexts.

\$\endgroup\$
  • \$\begingroup\$ I am new to coding...can you explain more...or what I need to change.Thanks \$\endgroup\$ – Vlad Ilisan Dec 9 '17 at 17:32
  • \$\begingroup\$ Yeah, let me get to a computer and I'll suggest an edit to the question, and go into more detail. \$\endgroup\$ – Stephan Dec 9 '17 at 17:33
  • \$\begingroup\$ Draco beat me to the full explanation, but yes that's what I meant be read-only. You can only set values to it, not updated values in it. \$\endgroup\$ – Stephan Dec 9 '17 at 18:42
  • \$\begingroup\$ :) I dont want the x,z to have lerp, just the y...how do i do that? \$\endgroup\$ – Vlad Ilisan Dec 9 '17 at 18:44

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.