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 /
  • Help Room /
avatar image
1
Question by Epsilone · Oct 23, 2016 at 02:29 PM · movementrigidbodycharactercontrollermoveposition

Control speed of a rigidbody that move to a fixed distance

Hello

I am quite new to unity and I am having a problem to move my player. My goal is to move my player smoothly one unit to the right if I press the right arrow and same to the left. So my player move this way: (x,x,x) to (x+1,x,x) and it continue to move one more unit(s) if I keep the key pressed.

I finally manage to do it with the player transform (and it works exactly as I wanted) but it has problem with collision with other object, so after reading a bit I decide to switch to use its rigidbody instead of changing its transform.

I manage once again to make it work by using rigidBody.MovePosition() and the player move one unit per one unit, halas it move really really fast, about 10 unit when I touch briefly the key, so I want to slow down it's movement.

The answers I saw on this website about speed change concerns movements in a given direction that were not restrained to a constant distance so I couldn't use them.

This is my code:

 public class PlayerController : MonoBehaviour{
     Vector3 pos = Vector3.zero;
     Transform tr;
     //public float movementspeed = 5.0f;
     CharacterController characterController;
     Rigidbody rigidBody;
 
     void Start(){
         pos = transform.position;
         tr = transform;
         characterController = GetComponent<CharacterController>();
         rigidBody = GetComponent<Rigidbody> ();
     }
 
     void FixedUpdate(){
         Vector3 delta = Vector3.zero;
 
         if (Input.GetKey(KeyCode.RightArrow) && tr.position == pos) 
             delta = Vector3.right;
         else if (Input.GetKey(KeyCode.LeftArrow) && tr.position == pos) 
             delta = Vector3.left;

         pos += delta;
         //transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * movementspeed);
         //characterController.Move(delta);
         rigidBody.MovePosition (pos);
     }
 }

This line was the First I tried that works but it use the transform and it gives bag collisions.

 //transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * movementspeed);

These two are my attempt to use the rigidbody but they both gives a result too fast. I don't really care between using a classic rigidbody or a charactercontroler as I don't know which is the most adapted to my grid game.

 //characterController.Move(delta);
 rigidBody.MovePosition (pos);

I also looked at the Rigidbody.MovePosition doc and it says we can smooth the movement by playing with interpolation and isKinematic but I didn't see a difference.

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

2 Replies

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

Answer by bateria_260791 · Oct 25, 2016 at 04:15 PM

Some time ago I published in my blog (written in spanish) a script to do this type of movement using Transform. But if you @Epsilonee wish to use it copy and paste this script:

 public class PlayerMovement : MonoBehaviour {
         [Range(1f, 10f)]
         public float speed = 1f;
         public LayerMask unwalkableMask;
         private Vector3 nextPosition;
         private bool isWalking;
  
         void Start(){
              nextPosition = transform.position;
              isWalking = false;
         }
  
         void Update(){
              float horizontalMovement = Input.GetAxisRaw("Horizontal");
              float verticalMovement = Input.GetAxisRaw("Vertical");
  
              // If the player is quiet.
              if(transform.position == nextPosition){
                   isWalking = false;
              }
  
              // If the player wants to move and is not walking.
              // The future position is calculated.
              if(horizontalMovement != 0 && !isWalking){
                   nextPosition += Vector3.right * horizontalMovement;
              }else if(verticalMovement != 0 && !isWalking){
                   nextPosition += Vector3.up * verticalMovement;
              }
  
              // If the future position is different from the current one is because the player wants to move the character ...
              if(nextPosition != transform.position){
                   Vector2 dir = nextPosition - transform.position;
                   Raycasthit2D hit = Physics2D.Raycast(transform.position, dir, dir.sqrMagnitude, unwalkableMask.value);
  
              // It is verified by a raycast that nothing interpose the movement.
              if(hit.collider == null){
                   transform.position = Vector3.MoveTowards(transform.position, nextPosition, Time.deltaTime * speed);
                   if(!isWalking){ isWalking = true; }
              }else{
                   Debug.LogError("There is an obstacle, the player can't move");
                   nextPosition = transform.position;
 }
              }
         }
 }


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 Epsilone · Oct 25, 2016 at 08:43 PM

@bateria_260791 Thanks a lot bateria_260791, but I already can do it with the transform. I can't use it because I need a rigidbody to deal with collision easily, and rigidbody seem to have problem with transform based mouvement.

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

104 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers

How to: rigidbody.MovePosition to player's position 2 Answers

i am using a code to make my cube walk with character controller ,but when i play the cube spins. 0 Answers

Help converting a character controller script to rigidbody 1 Answer

Converting my Character Controller Movement to Rigidbody 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