- Home /
C#, Rigidody, Javascript Tutorial
Trying to follow this tutorial, which is in Javascript, while I've been using C#. Hasn't been a problem up to this point. I'm having a problem with rigidbody.
I get the error at, [ rigidbody.velocity.x = horizontalMovement.x; ] and [ rigidbody.velocity.z = horizontalMovement.z; ], with the error code: "Assets/Scripts/PlayerMovementScript.cs(28,19): error CS1612: Cannot modify a value type return value of `UnityEngine.Rigidbody.velocity'. Consider storing the value in a temporary variable "
I'm trying to figure out how to store the value to no avail, or I'm just wondering if I'm doing this wrong. Can anyone tell me what I'm doing wrong or what I'm not using properly? I've included the video of the tutorial I'm following at the point of completed code(Looks like mine, except mine is all sparkly with C# instead of JavaScript).
using UnityEngine;
using System.Collections;
public class PlayerMovementScript : MonoBehaviour
{
// Player Movement
public float walkAcceleration = 5.0f;
public float maxWalkSpeed = 10.0f;
public Vector3 horizontalMovement;
// Object Reference
public GameObject cameraObject;
public void Start()
{
}
public void Update()
{
horizontalMovement = new Vector3(rigidbody.velocity.x, 0, rigidbody.velocity.z);
if (horizontalMovement.magnitude > maxWalkSpeed)
{
horizontalMovement = horizontalMovement.normalized;
horizontalMovement *= maxWalkSpeed;
}
rigidbody.velocity.x = horizontalMovement.x;
rigidbody.velocity.z = horizontalMovement.z;
transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent<MouseLookScript>().CurrentYRotation, 0);
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration, 0, Input.GetAxis("Vertical") * walkAcceleration);
}
}
I've already got an "AddRelativeForce" method, I'm trying to set the maximim speed of the gameObject by getting the velocity of the rigid body but it's throwing the above error.
error code: "Cannot modify expression because it is not a variable"
Unity error code: "Assets/Scripts/Player$$anonymous$$ovementScript.cs(32,23): error CS1612: Cannot modify a value type return value of `UnityEngine.Rigidbody.velocity'. Consider storing the value in a temporary variable"
It works perfectly in javascript, replicating it in C# throws the same error, cannot modify expression because it is not a variable.
I fixed my answer. In C# you have to modify the entire Vector3, you can't modify it's components (x,y,z).
Answer by nikescar · Oct 07, 2012 at 07:05 AM
You have two options for errors like these:
1. rigidbody.velocity = new Vector3(horizontalMovement.x, rigidbody.velocity.y, rigidbody.velocity.z);
2. Vector3 tempVariable = new Vector3(horizontalMovement.x, rigidbody.velocity.y, rigidbody.velocity.z);
rigidbody.velocity = tempVariable;
To limit speed you could do something like:
if (rigidbody.velocity < maxSpeed)
{
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration, 0, Input.GetAxis("Vertical") * walkAcceleration);
}
But like Fattie said, you should be doing this to a rigidbody.
The 2 methods don't work, error, "Cannot modify expession because it is not a variable".
Since I'm not supposed to modify rigidbodies, how do I limit it's movement to a max, ins$$anonymous$$d of having it's speed go faster and faster as I move in a direction?
Fixed.
To limit speed you could do something like: if (rigidbody.velocity < maxSpeed) { rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") walkAcceleration, 0, Input.GetAxis("Vertical") walkAcceleration);
}
It works now, thank you! C# seems to be more complicated than JavaScript, but I prefer the safety of C#. Is there a way to close this or will it do it on it's own over time?
good one forgotten. you guy's tidest code option is probably something like...
newSpeed =
(
$$anonymous$$athf.Clamp( -100, 100, rig.vel.x ),
$$anonymous$$athf.Clamp( -100, 100, rig.vel.y ),
$$anonymous$$athf.Clamp( -100, 100, rig.vel.z )
)
rig.vel = newSpeed
Your answer
![](https://koobas.hobune.stream/wayback/20220613082004im_/https://answers.unity.com/themes/thub/images/avi.jpg)