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

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.

transform.Translate (Input.acceleration.x, 0, -Input.acceleration.z/2);       
 if (GetComponent<Rigidbody>().velocity.magnitude < maxSpeed)    
    {          
  GetComponent<Rigidbody>().AddForce(Input.acceleration * moveSpeed);      
  }

Why are you doing a Translate? That simply updates the position without regard for physics or collisions. Try taking that out?

1 Like

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.

what are some other alternatives?

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. :slight_smile:
Even if you don’t want to look at those, you can look up the options I mentioned.

1 Like

I will do this right away! thank you for pointing me in the right direction :slight_smile:

No problem :slight_smile: Good luck!

I made this change. unfortunately the player does not move now.

    private Rigidbody move;
    //
    void Start ()
    {
        spawn = transform.position;
        move = GetComponent<Rigidbody>();
    }
    void FixedUpdate () {
   
//    transform.Translate (moveSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis ("Vertical") * Time.deltaTime);
        move.AddForce (moveSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis ("Vertical") * Time.deltaTime);

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.

Ok I tried this but it still won’t move3293278--255152--Screen Shot 2017-11-19 at 4.35.00 PM.png

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:

    private Rigidbody rb;
    [SerializeField]
    float moveSpeed = 2f;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    void FixedUpdate()
    {

        //    transform.Translate (moveSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis ("Vertical") * Time.deltaTime);
        // rb.AddForce(moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
        Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0,Input.GetAxis("Vertical"));
        if (move.magnitude > 1f) move.Normalize();
        move *= moveSpeed;
        rb.velocity = move;
    }

Sorry, I’m a little preoccupied atm to work out the previous code.

Note: I also locked the rotation**

1 Like

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!

Cool. :slight_smile: I’ve never used that, but I don’t see why it wouldn’t work just the same :wink:

1 Like

It works when using the accelerometer as well. Thank you so much for your help! I appreciate this so so much <3

No problem. :slight_smile: Have a good time making your game. =)

One quick thing. I notice that the player tends to stick to the wall sometimes. Is there any way I can fix this?

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 :slight_smile:

1 Like

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

Like he said, try a physics material

1 Like

Physic material was the fix thank you again.