- Home /
Question by
Milky13240 · Oct 23, 2016 at 09:08 AM ·
2drigidbodyunity 2dvector
Error Message: Cannot modify a value type return value of `UnityEngine.Rigidbody2D.velocity'. Consider storing the value in a temporary variable
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);
}
}
}
Comment
Answer by TheOnlyProphet · Oct 23, 2016 at 11:34 AM
You are doing this:
rb2d.velocity.x = new Vector2(...,...);
but you have to just do this:
rb2d.velocity = new Vector2(...,...);
Your answer
![](https://koobas.hobune.stream/wayback/20220612095240im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
CharacterController or Collider/Rigidbody 2 Answers
Game runs fine, build fails(error returs 395). 0 Answers
Calculating the Angle between two vectors 2 Answers
Megaman Movement 0 Answers