Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Prevent pass through the wall (Character)

Discussion in 'Scripting' started by Berk18, May 5, 2018.

  1. Berk18

    Berk18

    Joined:
    Oct 19, 2015
    Posts:
    9
    Hello, I have a third person survival game. I have a problem and not solve it. my chracter pass the inside the wall. How can i prevent this ?



    This is my character and you can see inside the wall(If ı move the character, character pass the wall). My character has collider and rigidbody.



    This is my sample wall. I try add mesh collider and box collider but not working. Why?
    I do not want to pass through the wall when character move. Thank you.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    A Box collider would be good for the wall. Mesh collider's not needed.

    What is the code you're using to move the character? For a rigidbody and a collider, you should be moving with the physics system.

    Another option you could try is using the character controller instead: https://docs.unity3d.com/Manual/class-CharacterController.html
     
    Berk18 likes this.
  3. Berk18

    Berk18

    Joined:
    Oct 19, 2015
    Posts:
    9
    This script is nside the player script.

    Code (CSharp):
    1. void Move(){
    2.  
    3.         float moveSpeed = runSpeed;
    4.  
    5.         if (playerInput.IsWalking)
    6.             moveSpeed = walkSpeed;
    7.  
    8.         if (playerInput.IsCrouched)
    9.             moveSpeed = crouchSpeed;
    10.  
    11.         Vector2 direction = new Vector2 (playerInput.Vertical * moveSpeed, playerInput.Horizontal * moveSpeed);
    12.  
    13.         if (direction != Vector2.zero)
    14.             footSteps.Play ();
    15.         if(playerHealth.currentHealth > 0)
    16.         MoveController.Move (direction);
    17.  
    18.     }
    And this is the my movecontroller script.

    Code (CSharp):
    1. public class MoveController : MonoBehaviour {
    2.  
    3.     public void Move(Vector2 direction){
    4.         transform.position += transform.forward * direction.x * Time.deltaTime +
    5.             transform.right * direction.y * Time.deltaTime;
    6.     }
    7. }
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Right, so you are not moving with the physics system.
    One other option I didn't mention before is that you could raycast to determine valid movements.
    So, options are: raycasting to determine valid moves, physics system to move, or using a character controller.

    When you change the transform's position in code directly, it doesn't obey collisions automatically, as you imagined.
     
  5. Berk18

    Berk18

    Joined:
    Oct 19, 2015
    Posts:
    9
    Actually, I didnt understand. Can you explain more? How can İ fixed the code?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If you were stumped by all of that, my best advice to you is to follow some tutorials so you can be better acquainted with Unity: https://unity3d.com/learn/beginner-tutorials

    Start there, and expand on things from there! :)