Can't apply max speed to rigidbody player

Hi, I started not long ago using Unity and programming in c#, so I’m still a beginner. My goal is to create a mini Fps and the project went well until I wanted to apply an if statement to the rigidbody that prevents the player to accelerate continuously and sets a max speed. The problem is that I can’t apply the statement to the rigidbody and it gives me this error “CS1612: Cannot modify the return value of ‘expression’ because it is not a variable” when I try to type this “rigidbody.velocity.x = horizontalMovement.x” and “rigidbody.velocity.z = horizontalMovement.y”. (Sorry for my English)

So here’s the code:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class move : MonoBehaviour
{
    public Rigidbody rigidbody;
    public float speed;
    public float raycastRange = 1.1f;
    public float jumpForce;
    public float airFriction = 0.2f;
    public float maxSpeed;
    public Vector2 horizontalMovement;
    public float walkDeacceleration;
   

    public bool isGrounded()
    {
        return Physics.Raycast(transform.position, Vector3.down, raycastRange);
    }
   
    private void Start()
    {
        rigidbody = GetComponent<Rigidbody>();
    }
  
   
    private void Update()
    {
        Jump();
        Move();
    }

    private void Jump()
    {
        if(Input.GetKeyDown(KeyCode.Space) && isGrounded())
        {
            rigidbody.AddForce(0, jumpForce, 0, ForceMode.Impulse);
        }
        else
        {
            rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * speed * airFriction, 0, Input.GetAxis("Vertical") * speed * airFriction);
        }
    }

    private void Move()
    {
        rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * speed, 0, Input.GetAxis("Vertical") * speed);
       
        horizontalMovement = new Vector2(rigidbody.velocity.x, rigidbody.velocity.z);

        rigidbody.velocity = new Vector3(horizontalMovement.x, rigidbody.velocity.y, horizontalMovement.y);


        if (Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0 && isGrounded())
        {
            rigidbody.velocity = new Vector3(horizontalMovement.x, 0f, horizontalMovement.y);
           
            horizontalMovement.x /= walkDeacceleration;
            horizontalMovement.y /= walkDeacceleration;
           
       
        // Max speed
        if(horizontalMovement.magnitude > maxSpeed)
        {
            horizontalMovement = horizontalMovement.normalized;
            horizontalMovement *= maxSpeed;

        // rigidbody.velocity.x = horizontalMovement.x
        // rigidbody.velocity.z = horizontalMovement.y
        }

        }
    }

}

Create a new vector as you desire it, then set velocity equals to this new vector.

1 Like

Thank you for the response! I tried your suggestion but still it didn’t work… After sometime I googled a bit more and I found this line of code “rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity, maxSpeed);” and it works like a charm!

Thank you nonetheless.