I Can't Assign Something To Local Space's X Axis

Hellos,

transform.InverseTransformDirection(rigidbody.velocity).x = Mathf.Clamp( (transform.InverseTransformDirection(rigidbody.velocity).x), -3.0f, 3.0f);

I need to write this code for my car but I can’t. It give me a warning and it doesn’t work. Warning is : “Assignment to temporary.”

I can write just this :

this.rigidbody.velocity.x = Mathf.Clamp( this.rigidbody.velocity.x, -3.0f, 3.0f);

but this is not my car’s velocity.(not local space) It is world space’s velocity. I need assign variable to my car’s local space.

In addition, my purpose is that, if my car drift in X axis too much, I want stop it’s drifting.

What should I do ?

Thanks for helps :slight_smile:

Typically I see this in three lines of code:

Vector3 localVelocity = transform.InverseTransformDirection(rigidbody.velocity);
localVelocity.x = Mathf.Clamp(localVelocity.x, -3.0f, 3.0f);
rigidbody.velocity = transform.TransformDirection(localVelocity);