Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Willem · Feb 26, 2010 at 02:18 AM · rigidbodyvelocitycs1612

Rigid Body Velocity in C#

Hey all got a small problem. I am writing a movement script for my main character in C#. Here is what I have so far...

using UnityEngine;

using System.Collections;

public class MainPlayer : MonoBehaviour {

private int health; private int boards; private int nails;

// Use this for initialization void Start () {

}

// Update is called once per frame void Update () {

 gameObject.transform.position.y = -15;
 gameObject.rigidbody.freezeRotation = true;

 if( Input.GetKey( "a" ) ) // move left
 {
     gameObject.rigidbody.velocity.x = -8;
 }

}

}

I am getting these errors: Cannot modify the return value of UnityEngine.Transform.position' because it is not a variable Cannot modify the return value ofUnityEngine.Rigidbody.velocity' because it is not a variable

After reading around I figured its because of the struct class difference that C# seems to have. How would I go about resolving this problem? Thanks for any help :)

Comment
Add comment
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

4 Replies

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

Answer by Wolfram · Feb 26, 2010 at 11:32 AM

You cannot assign to individual elements of transform.position, only the whole object. So instead, you need to do one of the following:

gameObject.transform.position=new Vector3(gameObject.transform.position.x,
                                          -15,
                                          gameObject.transform.position+z);

or

Vector3 newpos=gameObject.transform.position;
newpos.y=-15;
gameObject.transform.position=newpos;

For relative movements, you can do:

gameObject.transform.position=gameObject.transform.position+new Vector3(0,-15,0);

or use the convenience methods:

gameObject.transform.Translate(new Vector3(0,-15,0), Space.World);

The same goes for velocity and others.

For a more detailed discussion, check out http://forum.unity3d.com/viewtopic.php?t=6900

Comment
Add comment · 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
1

Answer by Brian-Kehrer · Feb 26, 2010 at 02:26 AM

Velocity is a vector3, which is a struct.

The only way to assign to a Vector3 is by assigning the whole thing:

gameObject.transform.position = new Vector3(
      gameObject.transform.position.x,
      -15,
      gameObject.transform.position.z);

The data type is the same, the only difference is in Unityscript (javascript) they have included some helper functions which does this for you.

Comment
Add comment · 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
-1

Answer by Milad · Feb 26, 2010 at 10:17 AM

what do u want to do exactly?translate/position/velocity?

translate: transform.translate(vector3.right*amttomove*Time.deltatime);

Velocity: transform.rigidbody.velocity=transform.transformdirection(0,0,speed);

yourself correct the uppercase and lowercase.

good luck.

Comment
Add comment · 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
0

Answer by jonathan dinero · May 08, 2016 at 08:56 PM

que librería se necesita para usar un Addforce por que no funciona en un juego de unity con el comondo rigidbody.AddForce(movement, ForceMode.Force);

Comment
Add comment · 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

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

1 Person is following this question.

avatar image

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

How to make rigidbody rotation more unstoppable 0 Answers

how to limit horizontal input when jumping 1 Answer

Rigidbody cube randomly stops moving? 1 Answer

AddRelativeForce not working correctly 1 Answer


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