• 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 camdogcrawford · Nov 14, 2020 at 10:45 PM · collision detection

how to fix my player from phasing through walls

I have set all of my colliders to not trigger and all of them are set. my player goes through them and if you stop in the wall it just pushes you out almost like it doesn't update in time.

Comment
Add comment · Show 2
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 Eno-Khaon · Nov 14, 2020 at 11:11 PM 0
Share

How do you move your character around? Could you offer an example from your script(s)?

A typical cause of passing through walls is directly changing Transform.position (or similar) to move the character around, which does not properly pass collision data to the Rigidbody/Collider interactions.

However, without more information, there's no way of knowing what information could help you personally.

avatar image camdogcrawford Eno-Khaon · Nov 15, 2020 at 12:00 AM 0
Share

mov = Input.GetAxisRaw("Horizontal") speed; movement = new Vector2(Input.GetAxis("Horizontal"), 0f); transform.position += movement Time.deltaTime * speed; animator.SetFloat("Speed", $$anonymous$$athf.Abs(mov));

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by InfinityMakesGames · Nov 15, 2020 at 12:17 AM

use FixedUpdate()

that might fix it

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 camdogcrawford · Nov 15, 2020 at 12:20 AM 0
Share

that might work

avatar image camdogcrawford · Nov 15, 2020 at 12:22 AM 0
Share

that worked thank you!

avatar image
0

Answer by Eno-Khaon · Nov 15, 2020 at 12:43 AM

Based on the details added in the comments, the problem lies in the difference between using Transform.position for movement and applying movement that will be properly handled by physics interactions.

Before jumping into a simple "Here's how to fix it", however, let's first look at why this doesn't work in its current form.

 void Update()
 {
     transform.position += movementInput * Time.deltaTime;
 }

While this will make the character move in the direction that input is provided, it won't affect their "physical" presence in the physics engine(s). This is because the rendered 3D model (or 2D texture/sprite) and their Collision data are not the same thing. While moving the GameObject around using Transform.position *does* move their Collider around, it doesn't send messages to it, nor does it occur with proper timing.

Physics simulations in Unity occur independently from the framerate for consistency, in FixedUpdate() (default 50fps) rather than in Update() (Each rendered frame). A Rigidbody (or Rigidbody2D) component will allow a GameObject to properly interact with the Colliders on other GameObjects.

With that in mind, there are numerous ways to move around using a Rigidbody.
The most standard "correct" way is to AddForce() to the GameObject:

 Rigidbody rb; // or Rigidbody2D
 
 void Start()
 {
     rb = GetComponent<Rigidbody>();
 }
 
 void FixedUpdate()
 {
     // For player movement, we'll want to ignore mass
     // Apply directional movement each frame based on provided input
     // Apply special forces (i.e. jump) once at a time
     
     // 3D
     rb.AddForce(v3MovementInput, ForceMode.Acceleration);
     if(jumpInput)
     {
         rb.AddForce(v3JumpInput, ForceMode.VelocityChange);
         jumpInput = false;
     }
     
     // 2D
     rb2D.AddForce(v2MovementInput * rb2D.mass, ForceMode2D.Force);
     if(jumpInput)
     {
         rb2D.AddForce(v2JumpInput * rb2D.mass, ForceMode2D.Impulse);
         jumpInput = false;
     }
 }


This applies forces which affect the Rigidbody's Velocity. On that note, the velocity can also be set directly, with less-believable results.

Additionally, a Rigidbody (typically a kinematic one) can be moved using Rigidbody.MovePosition() instead. This is effectively equivalent to using Transform.position, but will always attempt to resolve physics interactions in the process. To apply it to your example would look about like this:

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

 void Update()
 {
     movement = Vector2.right * Input.GetAxis("Horizontal");
 }

 void FixedUpdate()
 {
     // Because you're modifying position directly, you still
     // want to multiply by Time.deltaTime
     // Fortunately, Time.deltaTime is automatically converted
     // to Time.fixedDeltaTime in FixedUpdate()
     rb.MovePosition(transform.position + movement * Time.deltaTime * speed);
 }
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

143 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

Related Questions

OnCollisionEnter2D not triggering fast enough(?) 2 Answers

Problem with softbodies collisions 0 Answers

How to stop object from going through walls. 4 Answers

Why is OnCollisionStay activating before collisions? 2 Answers

Why the npc character walking strange when using a Rigidbody and Is Kinematic on enabled ? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges