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
0
Question by EugeneDudyak · Jan 23, 2019 at 07:05 AM · movementrigidbodycollidercontrollergrid

How to code PlayerController grid movement with RigidBody and Colliders?

I have a "PlayerController" script that allows grid movement (like a chess piece to a open square) and will stop the player on an exact square no matter how long the movement key has been pressed. However this "PlayerController" script uses Transform which clips the player with obstacles when using Rigidbody or colliders. I coded another script using rigidbody.moveposition which does work with colliders without clipping but the movement jumps from point to point instead of a smooth slide and won't stop the player on an exact square if the movement key is released at the wrong time. Is there a way to modify the "PlayerController" script to have grid movement and work with Rigidbody or colliders. I'm aware that using Velocity or addForce are better when using Rigidbody and colliders but I'm a novice to coding and need guidance. Thank you for your time.

CORRECT GRID MOVEMENT---

public class PlayerController : MonoBehaviour { Vector3 pos; // For movement public float speed = 2.0f; // Speed of movement

 void Start()
 {
     pos = transform.position;          // Take the initial position
 }

 void FixedUpdate()
 {
     if (Input.GetKey(KeyCode.A) && transform.position == pos)
     {        // Left
         pos += Vector3.left;
     }
     if (Input.GetKey(KeyCode.D) && transform.position == pos)
     {        // Right
         pos += Vector3.right;
     }
     if (Input.GetKey(KeyCode.W) && transform.position == pos)
     {        // Up
         pos += Vector3.forward;
     }
     if (Input.GetKey(KeyCode.S) && transform.position == pos)
     {        // Down
         pos += Vector3.back;
     }
     transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);

} }

CORRECT COLLIDER REACTION---

public class BetterPlayerController : MonoBehaviour { public Rigidbody rb; public float speed = 2.0f; // Speed of movement

 void Start()
 {
     rb = GetComponent<Rigidbody>();
 }

 void FixedUpdate()
 {
     if (Input.GetKey(KeyCode.A))
     {        // Left
         rb.MovePosition(transform.position += new Vector3(-10.0f, 0.0f, 0.0f) * Time.fixedDeltaTime * speed);
     }
     if (Input.GetKey(KeyCode.D))
     {        // Right
         rb.MovePosition(transform.position += new Vector3(10.0f, 0.0f, 0.0f) * Time.fixedDeltaTime * speed);
     }
     if (Input.GetKeyDown(KeyCode.W))
     {        // Up
         rb.MovePosition(transform.position += new Vector3(0.0f, 0.0f, 10.0f) * Time.fixedDeltaTime * speed);
     }
     if (Input.GetKeyDown(KeyCode.S))
     {        // Down
         rb.MovePosition(transform.position += new Vector3(0.0f, 0.0f, -10.0f) * Time.fixedDeltaTime * speed);
     }      
 }

}

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 xxmariofer · Jan 23, 2019 at 08:36 AM

You will not have a good time acomplishing this since rigidbody works with forces rather than just moving towards positions, the easiest hack you can do for accomplish this is give the player a velocityt with a desire direction and after than compare if the player position is equals to the desire position, the problem is that this never be the case since isnt moving to the position so you have to decide if you want to compare if the playe is in a range from the desire position relative to his position or compare if its position is higher than the expected vector

     if (Input.GetKey(KeyCode.A))
     {        // Left
         position = transform.position;
         position += new Vector3(-10, 0, 0);
         rb.velocity = new Vector3(-1, 0.0f, 0.0f) * Time.fixedDeltaTime * speed;
     }
     if (Input.GetKey(KeyCode.D))
     {        // Right
         position = transform.position;
         position += new Vector3(10, 0, 0);
         rb.velocity = new Vector3(1, 0.0f, 0.0f) * Time.fixedDeltaTime * speed;
     }
     if (Input.GetKeyDown(KeyCode.W))
     {        // Up
         position = transform.position;
         position += new Vector3(0, 0, 10);
         rb.velocity = new Vector3(0.0f, 0.0f, 1) * Time.fixedDeltaTime * speed;
     }
     if (Input.GetKeyDown(KeyCode.S))
     {        // Down
         position = transform.position;
         position += new Vector3(0, 0, -10);
         rb.velocity = new Vector3(0.0f, 0.0f, -1) * Time.fixedDeltaTime * speed;
     }
 }

you can do somithing like this the problem comes when comparing position, here is an answer of how to do it giving the vector some margin error

https://answers.unity.com/questions/950927/compare-vector3.html

you just have to set velocity to Vector3.zero if that method returns true.

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

193 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 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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Move 3D rigidbody sphere in the direction of it's rotation, controlled by the mouse. 0 Answers

Character Controller Follow me in the air 1 Answer

How to tag? 1 Answer

rigidbody It's going up and bounces when is moving 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