Search Unity

Player still goes through walls. I have done research and tried several solutions.

Discussion in 'Scripting' started by mattydennis, Nov 19, 2017.

  1. mattydennis

    mattydennis

    Joined:
    Nov 8, 2017
    Posts:
    39
    Hello,

    My player goes through walls even though I have "is trigger unchecked" on player and walls, player has rigid body and box collider, and walls have a box collider. I have tried to make the box collider bigger. I have tried all options under collision detection is box collider. I am using the accelerometer on a mobile device for movement. Any help or direction would be appreciated. Posted below is the code utilizing the accelerometer.

    Code (CSharp):
    1. transform.Translate (Input.acceleration.x, 0, -Input.acceleration.z/2);      
    2.  if (GetComponent<Rigidbody>().velocity.magnitude < maxSpeed)    
    3.     {          
    4.   GetComponent<Rigidbody>().AddForce(Input.acceleration * moveSpeed);      
    5.   }
     
  2. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,240
    Why are you doing a Translate? That simply updates the position without regard for physics or collisions. Try taking that out?
     
    angrypenguin likes this.
  3. mattydennis

    mattydennis

    Joined:
    Nov 8, 2017
    Posts:
    39
    Ok I just tried taking the Translate out in transform.Translate but it seems I can't just take it out but I need to replace it with something else.
     
  4. mattydennis

    mattydennis

    Joined:
    Nov 8, 2017
    Posts:
    39
    what are some other alternatives?
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    To obey the physics system collisions, you must move your character with it. Adding force to the rigidbody or setting its velocity, for example. The translate, as mentioned, ignores physics in that regard.
    There are at least 1 or 2 character physics / rigidbody examples in the standard assets. I believe one first person and one third person controller, as an example. :)
    Even if you don't want to look at those, you can look up the options I mentioned.
     
    mattydennis likes this.
  6. mattydennis

    mattydennis

    Joined:
    Nov 8, 2017
    Posts:
    39
    I will do this right away! thank you for pointing me in the right direction :)
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem :) Good luck!
     
  8. mattydennis

    mattydennis

    Joined:
    Nov 8, 2017
    Posts:
    39
    I made this change. unfortunately the player does not move now.
    Code (CSharp):
    1.     private Rigidbody move;
    2.     //
    3.     void Start ()
    4.     {
    5.         spawn = transform.position;
    6.         move = GetComponent<Rigidbody>();
    7.     }
    8.     void FixedUpdate () {
    9.    
    10. //    transform.Translate (moveSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis ("Vertical") * Time.deltaTime);
    11.         move.AddForce (moveSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis ("Vertical") * Time.deltaTime);
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I would try allowing the movespeed to be adjustable in the inspector, play the scene and move the speed up , to see if it works.
     
  10. mattydennis

    mattydennis

    Joined:
    Nov 8, 2017
    Posts:
    39
    Ok I tried this but it still won't move Screen Shot 2017-11-19 at 4.35.00 PM.png
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    confusing..Sorry, I don't use add force much; it does work, though, for sure.
    I was testing your code and didn't get it working properly right away.
    I modified it like so:
    Code (csharp):
    1.  
    2.     private Rigidbody rb;
    3.     [SerializeField]
    4.     float moveSpeed = 2f;
    5.     void Start()
    6.     {
    7.         rb = GetComponent<Rigidbody>();
    8.     }
    9.     void FixedUpdate()
    10.     {
    11.  
    12.         //    transform.Translate (moveSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis ("Vertical") * Time.deltaTime);
    13.         // rb.AddForce(moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
    14.         Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0,Input.GetAxis("Vertical"));
    15.         if (move.magnitude > 1f) move.Normalize();
    16.         move *= moveSpeed;
    17.         rb.velocity = move;
    18.     }
    Sorry, I'm a little preoccupied atm to work out the previous code.

    Note: I also locked the rotation**
     
    mattydennis likes this.
  12. mattydennis

    mattydennis

    Joined:
    Nov 8, 2017
    Posts:
    39
    No problem. I appreciate all of the time you are giving to help me. The player is moving! I am about to see what will happened when I use the accelerometer!
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool. :) I've never used that, but I don't see why it wouldn't work just the same ;)
     
    mattydennis likes this.
  14. mattydennis

    mattydennis

    Joined:
    Nov 8, 2017
    Posts:
    39
    It works when using the accelerometer as well. Thank you so much for your help! I appreciate this so so much <3
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem. :) Have a good time making your game. =)
     
  16. mattydennis

    mattydennis

    Joined:
    Nov 8, 2017
    Posts:
    39
    One quick thing. I notice that the player tends to stick to the wall sometimes. Is there any way I can fix this?
     
  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Not 100% sure what you mean by that? like there is some friction or something else?
    if it's friction, you could try adding a physics material... if it's something else, could you try to explain somehow? heh :)
     
    mattydennis likes this.
  18. mattydennis

    mattydennis

    Joined:
    Nov 8, 2017
    Posts:
    39
    Sorry I'm not responsive. I'll try to explain it better. When I am moving my player up against a wall it tends to grab onto the wall. For example, if I am holding down the left and down button trying to slide against the left wall, it may get hung up on the wall. So I then have to use the right button to get unstuck. This didn't happen before. I'm just curious if there is a way to fix this or if that's just the way it is. I appreciate your time so much!!!!!!
     
  19. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  20. mattydennis

    mattydennis

    Joined:
    Nov 8, 2017
    Posts:
    39
    Physic material was the fix thank you again.
     
  21. mattydennis

    mattydennis

    Joined:
    Nov 8, 2017
    Posts:
    39
  22. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, glad ya got it working :)