Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Cannot Modify the Return Value of 'Rigidbody.velocity' Because It Is Not a Variable. I am working on my first game. I am using Unity, in

Cannot Modify the Return Value of 'Rigidbody.velocity' Because It Is Not a Variable. I am working on my first game. I am using Unity, in C#. I am following a tutorial that uses Javascript, having to translate as I go, with no more than a little experience in C++ to do so. I have held my own until now. I have included my script for Player Movement, and the syntax error is about two-thirds of the way down when I am trying to establish a maximum walking speed. I've read that it's because I need to set up a temporary variable? I actually tried to just turn the two lines that I am having issue with into one, I used: "rB.velocity = horizontalMovement" and it actually worked for one direction. In other words, I could control the walking speed in one direction, say side-to-side, but not the other, say front-to-back. Here is my code...

using UnityEngine;

[RequireComponent(typeof(Rigidbody))] public class PlayerMovement : MonoBehaviour { public float walkAcceleration = 5; // Initializing a Float to An Integer Value // Doesn't Require a Lowercase "f" As a Suffix public GameObject cameraObject; // Parameter for the Main Camera Object in Unity public float maxWalkSpeed = 20; // Initializing a Float to An Integer Value // Doesn't Require a Lowercase "f" As a Suffix [HideInInspector] // Hide In Inspector In Unity, But Not Making Private In Case You Need It In Another Source File private Rigidbody rB; // Parameter For Rigidbody Component In Unity public Vector2 horizontalMovement; // Horizontal Movement Declared As a 2 Element Vector // Use This For Initialization // Start Is Called Before the First Frame Update void Start() { rB = GetComponent(); // Parameter "rB" Initialized to Rigidbody Component in Unity }

// Update Is Called Once Per Frame void FixedUpdate() // Here "FixedUpdate" Is Used To Mess With Physics { horizontalMovement = new Vector2(rB.velocity.x, rB.velocity.z); // Store The X & Z-Components of Velocity Into the 2 Element Vector "horizontalMovement"

if (horizontalMovement.magnitude > maxWalkSpeed) // If Walk Speed Is Greater Than "maxWalkSpeed"... { horizontalMovement = horizontalMovement.normalized; // Make "horizontalMovement" Magnitude Equal to 1 horizontalMovement *= maxWalkSpeed; // Multiply "maxWalkSpeed" By This Normalized 2D Vector Over and Over // Continue Until Horizontal Movement Equals "maxWalkSpeed" }

rB.velocity.x = horizontalMovement.x; // *** This Is The First Line With Error rB.velocity.z = horizontalMovement.y; // *** This Is The Second Line With Error

// Prevent Rotation Around the X & Z-axes // Make the Y-Axis "Point Forward" In the Main Camera (Matched to PlayerCapsule) transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent().currentYRotation, 0);

// Add Walking Acceleration With the Pressing of the Arrow Keys rB.AddRelativeForce(Input.GetAxisRaw("Horizontal") * walkAcceleration, 0, Input.GetAxisRaw("Vertical") * walkAcceleration); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Database Programming With Visual C# .NET

Authors: Ying Bai

1st Edition

0470467274, 978-0470467275

More Books

Students also viewed these Databases questions

Question

Describe the new structures for the HRM function. page 676

Answered: 1 week ago