I get this error message (Cannot modify a value type return value of `UnityEngine.Rigidbody2D.velocity’. Consider storing the value in a temporary variable) when I try to run my game. What’s wrong?
It redirects me to line 34, which is
rb2d.velocity.x = new Vector2(maxSpeed, rb2d.velocity.y);
What am i doing wrong?
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float maxSpeed = 3;
public float speed = 50f;
public float jumpPower = 150f;
public bool grounded;
private Rigidbody2D rb2d;
// Use this for initialization
void Start ()
{
rb2d = gameObject.GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update ()
{
}
void FixedUpdate()
{
float h = Input.GetAxis ("Horizontal");
rb2d.AddForce ((Vector2.right * speed) * h);
if (rb2d.velocity.x > maxSpeed)
{
rb2d.velocity.x = new Vector2(maxSpeed, rb2d.velocity.y);
}
}
}