Search Unity

How do i freeze position and use collision?

Discussion in 'Physics' started by Epic-Username, Sep 4, 2015.

  1. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I have this player with a sphere collider but when i run into a wall it pushes me to the side when i keep running due to my collider shape and i don't want this to happen, but when i enable freeze x and z position on RB the player can pass through walls how can i get this to test and stop for collision and not be moved by it.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Could you post your moving code?
     
  3. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    Code (CSharp):
    1. float playerRotation = Input.GetAxis ("Mouse X");
    2.         float moveVertical = Input.GetAxis ("Vertical");
    3.         float moveHorizontal = Input.GetAxis ("Horizontal");
    4.  
    5.             transform.Translate (moveHorizontal * speed * Time.deltaTime, 0, moveVertical * speed * Time.deltaTime);
    6.             transform.Rotate(0, playerRotation * rotationSpeed, 0);
    7.         if (Input.GetKey (KeyCode.Space) && grounded == true) {
    8.             jumpPos = 0.3f;
    9.         }
    10.         transform.Translate(new Vector3(0,0 + jumpPos,0));
    11.     if(jumpPos > 0.01f)
    12.         jumpPos -= 0.01f;
    13.  
    14.     }
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Yeah,that's it, never ever use transform. Translate, it ignores collision, use rigidbody instead
     
  5. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    but the only movement i know with rigidbody is rigidbody.AddForce() but i want the movement to respond instantly to have smooth movement, rigidbodys movement adds up force until its high enough then moves it quickly and then you have to apply lots of force then to stop it, so i don't like using rigidbodys movement.
     
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    If you dont want realistic physics, then change the velocity itself, it will be instant, and you can make it stop instantly too.
     
  7. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    What is the code to do that?
     
  8. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    If you have the rigidbody already as a variqble, tgen rb.velocity =, where rb is tge variable
     
  9. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    this: RB.velocity = new Vector3(moveHorizontal * speed, 0, moveVertical * speed); doesn't work it move the player along the global vector not local. this doesn't work efficiently with my method of movement.
     
  10. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Multiple everything them with transform.forwards corresponding axis, so tranform.forward.x, or.y and for y value use rb.velocity.y
     
  11. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    What do you mean?
     
  12. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Rb.velocity = Vector3(put your movement here) * transform.forward
     
  13. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    When i try that it gives me errors.

    error CS0019: Operator `*' cannot be applied to operands of type `UnityEngine.Vector3' and `UnityEngine.Vector3'
     
  14. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
  15. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
  16. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Continue that tutorial series, its good