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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by quinson · Jul 02, 2012 at 02:43 AM · movementbasicconstraint

Constrained Movement for 2.5D character

I am a beginner at best, and I was wondering if there was a simple way of contraining the movement of my capsule character. I got the basic movement script off of the reference and made sure the constraints were on in the rigid body for movement in the z-axis, but when ever the character hit an object that is cuvered, it would be forced out of the my ideal plane, with positioning in the z-axis of 0.

Any ideas?

 void Update() {
     
     CharacterController controller = GetComponent<CharacterController>();
     // is the controller on the ground?
     if (controller.isGrounded) {
         //Feed moveDirection with input.
         moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
         moveDirection = transform.TransformDirection(moveDirection);
         //Multiply it by speed.
         moveDirection *= speed;
         //Jumping
         if (Input.GetButton("Jump"))
             moveDirection.y = jumpSpeed;

     }
     //Applying gravity to the controller
     moveDirection.y -= gravity * Time.deltaTime;
     //Making the character move
     controller.Move(moveDirection * Time.deltaTime);
 }

I have tried using this

 transform.position.z = 0;

but I keep getting an error.

  • Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Seth-Bergman · Jul 02, 2012 at 02:50 AM

It would help to post the script. the line "transform.position.z = 0;" in the Update function might work off the bat.

edit:

sorry didn't know you're using c#.. here's a link that may help..

http://forum.unity3d.com/threads/66768-Set-value-on-GameObject.transform.position.x-with-C

So maybe try:

 var x = transform.position.x;
 var y = transform.position.y;
 
 transform.position = new Vector3(x,y,0);

here's another link that should be just what you want:

http://forum.unity3d.com/threads/91358-Restrict-Z-movement-on-character-controller-without-breaking-into-colliders

Comment
Add comment · Show 5 · 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 quinson · Jul 02, 2012 at 03:05 AM 0
Share

I keep getting an error. •Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable

avatar image Seth-Bergman · Jul 02, 2012 at 03:21 AM 0
Share

See link above.. So, you'll need to reassign the Vector3, rather than just the z coordinate, looks like.. Still maybe not the most elegant solution though

avatar image quinson · Jul 02, 2012 at 03:56 AM 0
Share

It looks like it should work, but now the character isn't appearing. Is there any way to make my character static in the z-direction?

Will be looking through posts.

avatar image Seth-Bergman · Jul 02, 2012 at 04:17 AM 0
Share

check the other link above, it's got a bit more fleshed out example.. once you hit play on the scene, you can pause it and select the character in the hierarchy view to find it, may help figure out what's going wrong.

avatar image quinson · Jul 02, 2012 at 04:38 AM 0
Share

O$$anonymous$$, my character is continously falling... Luckly not in the z-direction. Your other link is great, but gives a very undesired effect of seeing the character jump back everytime it touches something slanted.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Make GameObject move in one direction continuously on key press on C# 1 Answer

Basic AI Movement C# 3 Answers

How can I properly integrate rotation to a character? 0 Answers

How to stop object from rotating during movement (Javascript) 1 Answer

Constraining an object's movement in one direction by range 0 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