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 4illeen · Dec 14, 2011 at 11:18 PM · movementcharactercontrollertranslatetransform.translate

Custom movement problem

I'm creating a movement system from scratch, using transform.Tranlate, since I didn't like using character controller and want constant movement speed.

 void Update () {
         if(Physics.Raycast(transform.position, -transform.up,1f)) _grounded = true;
         else _grounded = false;
     
     if(_grounded )
     {
     //Jumping
         if (Input.GetKeyDown(KeyCode.Space))
         {
             rigidbody.AddRelativeForce(transform.up * 7, ForceMode.Impulse);
         }        
     }
     else
     {
     //Falling
         rigidbody.AddRelativeForce(transform.up * -20, ForceMode.Force);
     }
     //Movement
     if(!Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.W)) transform.Translate(0, 0, 1);
     if(!Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.S)) transform.Translate(0, 0, -1);
     if(Input.GetKey(KeyCode.D) ) transform.Translate(1, 0, 0);
     if(Input.GetKey(KeyCode.A)) transform.Translate(-1, 0, 0);


The problem is that my capsule seems to move on its own, especially after running towards a wall or other element with a collider. It's even worse since this movement cannot be stopped - sometimes it stops itself after a while.

Capsule has capsule collider and rigidbody attached (Doesn't use gravity, since the script does it). I found the solution to the stange movement problem by freezing position x an z in rigidbody (y is needed for force jumps). This solves the strange movements but now my capsule doesn't collide when I'm walking into something like cubes.

I feel like I'm running in circles here trying to fix another bug caused by the bug before, so I'm asking you guys - is there a simple solution to those problems? I'd rather not go back to using the character controller because this way it's easier for me to add new functions like sprinting or sneaking, and I'm not a unity or C# pro.

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 syclamoth · Dec 14, 2011 at 11:38 PM

The problem is (as usual with these things) that you are using a 'transform.translate' style controller, at the same time as using a rigidbody. A rigidbody object will always try to use Unity's inbuilt phyisics engine- so you should either write your entire movement script around that, or get rid of it entirely. Since you seem to be using it for jumps only, try writing the 'gravity' physics manually- keep track of its velocity, and apply gravity when it is above the ground.

Another thing- your input matrix looks really weird. Why don't you use Input.GetAxis instead?

Comment
Add comment · Show 2 · 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 syclamoth · Dec 14, 2011 at 11:39 PM 0
Share

Especially considering that 'forcemode.Impulse' is essentially just changing the object's velocity based on it's mass- that's really a pretty simple calculation.

avatar image 4illeen · Dec 15, 2011 at 12:09 AM 0
Share

Iirc when I used Input.GetAxis my character was gaining speed and I want fixed value. It was easier for me to write this that way, because as I said I'm a beginner.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to stop transform.Translate instantly ? 1 Answer

Drawbacks of Moving Rigidbody by transform.translate 1 Answer

Making character dash forward 0 Answers

Make an object that moves relative to the touch more natural 0 Answers

World and Local Axis out of Alignment 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