Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by forgotten893 · Oct 07, 2012 at 06:42 AM · c#javascripttutorialconversion

C#, Rigidody, Javascript Tutorial

Trying to follow this tutorial, which is in Javascript, while I've been using C#. Hasn't been a problem up to this point. I'm having a problem with rigidbody.

I get the error at, [ rigidbody.velocity.x = horizontalMovement.x; ] and [ rigidbody.velocity.z = horizontalMovement.z; ], with the error code: "Assets/Scripts/PlayerMovementScript.cs(28,19): error CS1612: Cannot modify a value type return value of `UnityEngine.Rigidbody.velocity'. Consider storing the value in a temporary variable "

I'm trying to figure out how to store the value to no avail, or I'm just wondering if I'm doing this wrong. Can anyone tell me what I'm doing wrong or what I'm not using properly? I've included the video of the tutorial I'm following at the point of completed code(Looks like mine, except mine is all sparkly with C# instead of JavaScript).

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovementScript : MonoBehaviour
 {
     // Player Movement
     public float walkAcceleration = 5.0f;
     public float maxWalkSpeed = 10.0f;
     public Vector3 horizontalMovement;
 
     // Object Reference
     public GameObject cameraObject;
 
     public void Start()
     {
 
     }
 
     public void Update()
     {
         horizontalMovement = new Vector3(rigidbody.velocity.x, 0, rigidbody.velocity.z);
         if (horizontalMovement.magnitude > maxWalkSpeed)
         {
             horizontalMovement = horizontalMovement.normalized;
             horizontalMovement *= maxWalkSpeed;
         }
 
         rigidbody.velocity.x = horizontalMovement.x;
         rigidbody.velocity.z = horizontalMovement.z;
 
         transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent<MouseLookScript>().CurrentYRotation, 0);
         rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration, 0, Input.GetAxis("Vertical") * walkAcceleration);
     }
 }

Tutorial Video End Result

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image forgotten893 · Oct 07, 2012 at 06:58 AM 0
Share

I've already got an "AddRelativeForce" method, I'm trying to set the maximim speed of the gameObject by getting the velocity of the rigid body but it's throwing the above error.

avatar image forgotten893 · Oct 07, 2012 at 07:31 AM 0
Share

error code: "Cannot modify expression because it is not a variable"

Unity error code: "Assets/Scripts/Player$$anonymous$$ovementScript.cs(32,23): error CS1612: Cannot modify a value type return value of `UnityEngine.Rigidbody.velocity'. Consider storing the value in a temporary variable"

avatar image forgotten893 · Oct 07, 2012 at 07:35 AM 0
Share

The code is directly attached to the rigidbody.

avatar image forgotten893 · Oct 07, 2012 at 07:43 AM 0
Share

It works perfectly in javascript, replicating it in C# throws the same error, cannot modify expression because it is not a variable.

avatar image nikescar · Oct 07, 2012 at 07:45 AM 0
Share

I fixed my answer. In C# you have to modify the entire Vector3, you can't modify it's components (x,y,z).

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by nikescar · Oct 07, 2012 at 07:05 AM

You have two options for errors like these:

 1. rigidbody.velocity = new Vector3(horizontalMovement.x, rigidbody.velocity.y, rigidbody.velocity.z);
 2. Vector3 tempVariable = new Vector3(horizontalMovement.x, rigidbody.velocity.y, rigidbody.velocity.z); 
 rigidbody.velocity = tempVariable;

To limit speed you could do something like:

 if (rigidbody.velocity < maxSpeed) 
 { 
     rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration, 0, Input.GetAxis("Vertical") * walkAcceleration);
 }
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nikescar · Oct 07, 2012 at 07:08 AM 0
Share

But like Fattie said, you should be doing this to a rigidbody.

avatar image forgotten893 · Oct 07, 2012 at 07:14 AM 0
Share

The 2 methods don't work, error, "Cannot modify expession because it is not a variable".

Since I'm not supposed to modify rigidbodies, how do I limit it's movement to a max, ins$$anonymous$$d of having it's speed go faster and faster as I move in a direction?

avatar image nikescar · Oct 07, 2012 at 07:34 AM 0
Share

Fixed.

To limit speed you could do something like: if (rigidbody.velocity < maxSpeed) { rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") walkAcceleration, 0, Input.GetAxis("Vertical") walkAcceleration);
}

avatar image forgotten893 · Oct 07, 2012 at 07:52 AM 0
Share

It works now, thank you! C# seems to be more complicated than JavaScript, but I prefer the safety of C#. Is there a way to close this or will it do it on it's own over time?

avatar image Fattie · Oct 07, 2012 at 08:55 AM 1
Share

good one forgotten. you guy's tidest code option is probably something like...

 newSpeed =
 (
 $$anonymous$$athf.Clamp( -100, 100, rig.vel.x ),
 $$anonymous$$athf.Clamp( -100, 100, rig.vel.y ),
 $$anonymous$$athf.Clamp( -100, 100, rig.vel.z )
 )

 rig.vel = newSpeed
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Some Basic Conversions from JavaScript to C# 1 Answer

Converting a JS script into C# - hit a wall with arrays... 1 Answer

Distribute terrain in zones 3 Answers

Help with conversion from javascript to c# 3 Answers

Converting variable from javascript to C# 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges