Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Can't apply max speed to rigidbody player

Discussion in 'Scripting' started by lmarosevic7, Aug 23, 2020.

  1. lmarosevic7

    lmarosevic7

    Joined:
    Aug 21, 2020
    Posts:
    2
    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:


    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class move : MonoBehaviour
    7. {
    8.     public Rigidbody rigidbody;
    9.     public float speed;
    10.     public float raycastRange = 1.1f;
    11.     public float jumpForce;
    12.     public float airFriction = 0.2f;
    13.     public float maxSpeed;
    14.     public Vector2 horizontalMovement;
    15.     public float walkDeacceleration;
    16.    
    17.  
    18.     public bool isGrounded()
    19.     {
    20.         return Physics.Raycast(transform.position, Vector3.down, raycastRange);
    21.     }
    22.    
    23.     private void Start()
    24.     {
    25.         rigidbody = GetComponent<Rigidbody>();
    26.     }
    27.  
    28.    
    29.     private void Update()
    30.     {
    31.         Jump();
    32.         Move();
    33.     }
    34.  
    35.     private void Jump()
    36.     {
    37.         if(Input.GetKeyDown(KeyCode.Space) && isGrounded())
    38.         {
    39.             rigidbody.AddForce(0, jumpForce, 0, ForceMode.Impulse);
    40.         }
    41.         else
    42.         {
    43.             rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * speed * airFriction, 0, Input.GetAxis("Vertical") * speed * airFriction);
    44.         }
    45.     }
    46.  
    47.     private void Move()
    48.     {
    49.         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * speed, 0, Input.GetAxis("Vertical") * speed);
    50.        
    51.         horizontalMovement = new Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
    52.  
    53.         rigidbody.velocity = new Vector3(horizontalMovement.x, rigidbody.velocity.y, horizontalMovement.y);
    54.  
    55.  
    56.         if (Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0 && isGrounded())
    57.         {
    58.             rigidbody.velocity = new Vector3(horizontalMovement.x, 0f, horizontalMovement.y);
    59.            
    60.             horizontalMovement.x /= walkDeacceleration;
    61.             horizontalMovement.y /= walkDeacceleration;
    62.            
    63.        
    64.         // Max speed
    65.         if(horizontalMovement.magnitude > maxSpeed)
    66.         {
    67.             horizontalMovement = horizontalMovement.normalized;
    68.             horizontalMovement *= maxSpeed;
    69.  
    70.         // rigidbody.velocity.x = horizontalMovement.x
    71.         // rigidbody.velocity.z = horizontalMovement.y
    72.         }
    73.  
    74.         }
    75.     }
    76.  
    77. }
    78.  
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    lmarosevic7 likes this.
  3. lmarosevic7

    lmarosevic7

    Joined:
    Aug 21, 2020
    Posts:
    2
    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.