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 kiran1 · Aug 19, 2021 at 09:32 AM · collisionmovement3dunity5

Make character not move through walls and other objects

alt textalt textMy character/Player[polyman] is passing through walls and other objects ,my player had attached with character controller ,rigidbody Is kinematic in inspector.Below is my Player's script. What i done wrong in script ,Can i add anything in script? .I added colliders to all obstacles including player.I took some screenshots in my inspector. pls answer anyone!!!@rage_co

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class PlayerMovement : MonoBehaviour
 {
   
     
     //VARIABLES
     [SerializeField] private float moveSpeed;
     [SerializeField] private float walkSpeed;
     [SerializeField] private float runSpeed;
 
     
     private Vector3 moveDirection;
     private Vector3 velocity;
    
     [SerializeField] private bool isGrounded;
     [SerializeField] private float groundCheckDistance;
     [SerializeField] private LayerMask groundMask;
     [SerializeField] private float gravity;
     [SerializeField] private float jumpHeight;
 
     //REFERENCES
     private CharacterController controller;
     
     private Animator anim;
 
     private void Start()
     {
         
         controller = GetComponent<CharacterController>();
        
         anim = GetComponent<Animator>();
     }
 
     private void FixedUpdate()
     {
        
         Move();
         
 
     }
 
     private void Move()
     {
         
         isGrounded = Physics.CheckSphere(transform.position, groundCheckDistance, groundMask);
 
         if(isGrounded && velocity.y < 0)
         {
             velocity.y = -2f;
         }
         float moveZ = Input.GetAxis("Vertical");
        
         moveDirection = new Vector3(0, 0, moveZ);
         moveDirection = transform.TransformDirection(moveDirection);
         
         if (isGrounded)
         {
             if (moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
             {
                 Walk();
             }
             else if (moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
             {
                 Run();
             }
             else if (moveDirection == Vector3.zero)
             {
                 Idle();
             }
             moveDirection *= moveSpeed;
 
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 Jump();
             }
 
         }
 
        
 
 
         controller.Move(moveDirection * Time.deltaTime);
 
         velocity.y += gravity * Time.deltaTime;
         controller.Move(velocity * Time.deltaTime);
     }
 
     private void Idle()
     {
         anim.SetFloat("Speed", 0, 0.1f, Time.deltaTime);
     }
     
     private void Walk()
     {
         moveSpeed = walkSpeed;
         anim.SetFloat("Speed", 0.5f, 0.1f, Time.deltaTime);
         
 
     }
 
     private void Run()
     {
         moveSpeed = runSpeed;
         anim.SetFloat("Speed", 1,0.1f,Time.deltaTime);
     }
 
     private void Jump()
     {
         velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
     }
 }
 


1.png (68.9 kB)
2.png (50.3 kB)
Comment
Add comment · Show 12
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 rage_co · Aug 19, 2021 at 09:40 AM 0
Share

its not a problem with the character or the script...i believe the trees don't have a collider

avatar image kiran1 · Aug 19, 2021 at 09:47 AM 0
Share

ok but i had problem with walls and cubes

avatar image rage_co kiran1 · Aug 19, 2021 at 10:08 AM 0
Share

well i meant to say walls, just add colliders to all obstacles, i guess i was seeing some memes so got a bit distracted there....

avatar image kiran1 rage_co · Aug 19, 2021 at 10:40 AM 0
Share

I added colliders to both player and cube (wall)but Not worked!!Is trigger is unchecked in both!! But not worked!! Any help!!

avatar image kiran1 · Aug 19, 2021 at 10:58 AM 0
Share

I done again not worked!!

avatar image rage_co kiran1 · Aug 19, 2021 at 12:04 PM 0
Share

can you share a screenshot of the inspector screen of the player and the wall?

avatar image kiran1 rage_co · Aug 19, 2021 at 12:32 PM 0
Share

I added scrrenshots of inspector of player and wall. pls can you check if anything wrong!!

Show more comments

4 Replies

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

Answer by rage_co · Aug 20, 2021 at 04:13 AM

sorry...im late cuz i just woke up like an hour ago....now for the problem, why do you have a capsule collider with a character controller? the controller comes with it's own collider and this just might be the problem...so try removing the capsule collider....also what is the physic material you are using on the colliders (i don't think it's the problem , just confirming), also head to the project settings in editor > physics category and check that the collision matrix is properly setup

Comment
Add comment · Show 1 · 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 kiran1 · Aug 20, 2021 at 05:30 AM 0
Share

Thanks! It worked by removing capsule collider and in physics collider matrix is checked with player and collider .I made in inspector Layer as player in player and wall in layer as collider.So,It Worked!!Thanks a Lott!!!

avatar image
0

Answer by unity_8jz2K_QgYq8cUw · Aug 19, 2021 at 11:04 AM

Firstly, do u have a charactercontroller and a rigidbody on one object? Secondly do u have colliders on everything if it isnt sinking through the floor? Lastly, is the rigidbody’S collider being disabled due to the charactercontroller?

Comment
Add comment · Show 3 · 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 kiran1 · Aug 19, 2021 at 11:32 AM 0
Share

1)-YES, I have a charactercontroller and a rigidbody on one object(player) 2)Even without colliders on everything is not sinking through the floor. But, I have colliders on everything 3)no. rigidbody's colliders not disabled. I had applied all your conditions above. But ,Didn't works still passing through objects.. Any Help!!

avatar image rage_co kiran1 · Aug 19, 2021 at 12:03 PM 0
Share

you should NOT have a rigidbody with a character controller, i's not necessary for simple collisions and might even break them

avatar image kiran1 rage_co · Aug 20, 2021 at 03:34 AM 0
Share

Can you check screenshots that I shared pls!!

avatar image
0

Answer by mischkom · Aug 20, 2021 at 04:22 AM

https://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html

"If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore. "

You need to make sure to disable isKinematic (at the right time) if you want collisions with walls to prevent your character from passing through.

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 parmardarshanv · Aug 20, 2021 at 07:36 AM

You should add RigidBody to your player and make sure isKinematic is unchecked. I think that should work.

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

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

check if two colliders Rub against each other 1 Answer

Movement based on collision on a custom mesh 0 Answers

How to Have Two Child Objects of One Parent Detect Collisions Between Eachother. 1 Answer

Separate Objects after Mouse Clicking in Unity 5.6.4 1 Answer

Player getting stuck in ground (3D) player has Rigidbody, and Box Collider, world is Mesh Colliders 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