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);
}
}