Search Unity

Not Sure How To Setup proper Physics

Discussion in 'Physics' started by danilo12, Jul 21, 2015.

  1. danilo12

    danilo12

    Joined:
    Jul 21, 2015
    Posts:
    64
    So I've been using Unity for a while now and I came across a problem I couldn't fix from day one.

    Basically, my character can't move around properly(I am sure it has nothing to do with code), I am using a Capsule as my gameObject and I have a Capsule Collider attached to it, the thing is it floats on the air, some people told me to add a Rigidbody, I did that and atleast he can walk on completely plain gameobjects or Terrains, but whenever he climbs up a mountain for example he starts spinning on both X/Y/Z axises like objects in real life would do in a no gravity zone.

    I am not sure what is causing this problem, but I do know that my Capsule Collider is not a trigger, neither is my Terrain Collider.

    In case some of you need to see my code I can't acces it right now but it is pretty basic i use transform.Translate function to move around, and for my Jump function I added a empty GameObject as a child of my Capsule(player), and attached a capsule Collider on the gameobject, its right below my Player so whenever it touches the ground my Jump boolean is enabled(OnTriggerEnter).

    Please help me with this bug.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    And there's your problem, don't ever use transform.Translate, if you want to move an object with physics. Use rigidbody.AddForce instead, or you can set the rigidbody.velocity, but that is not recommended, because it's makes non-realistic movement

    If you don't want it to rotate around, when climbing mountain, you can just freeze the rotations in the rigidbody component

    It doesn't matter, that neither of those is a trigger, that just means, that it activates the onTriggerEnter() event
     
    Last edited: Jul 21, 2015
  3. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    transform.Translate ignores collision, even if you have rigidbody
     
  4. danilo12

    danilo12

    Joined:
    Jul 21, 2015
    Posts:
    64
    using UnityEngine;
    using System.Collections;

    public class BetterMovement : MonoBehaviour {
    public Rigidbody rb;
    public float PlayerSpeed = 80;
    public float RotateSpeed = 120;
    // Use this for initialization
    void Start () {
    rb = GetComponent <Rigidbody>();
    }

    // Update is called once per frame
    void Update () {
    if(Input.GetKey(KeyCode.W)){
    rb.AddForce(transform.forward * PlayerSpeed * Time.deltaTime);
    }
    if(Input.GetKey(KeyCode.S)){
    rb.AddForce(transform.forward * PlayerSpeed );
    }
    if(Input.GetKey(KeyCode.A)){
    transform.Rotate(-Vector3.up * RotateSpeed * Time.deltaTime);
    }
    if(Input.GetKey(KeyCode.D)){
    transform.Rotate(Vector3.up * PlayerSpeed * Time.deltaTime);
    }
    }
    }
    I tried this and it only works for rotating now
     
  5. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Apply a physics material with 0 friction to both object.
     
  6. danilo12

    danilo12

    Joined:
    Jul 21, 2015
    Posts:
    64
    I did it and it still doesn't move. it just knocks the character off when i try to move.
     
  7. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Did you read the whole thing i wrote? I mean, this part:
    And if you want to post code, use the import/code button on the toolbar
     
  8. danilo12

    danilo12

    Joined:
    Jul 21, 2015
    Posts:
    64
    Okay now it's almost all good but it feels like im walking on ice all the time, and the character is not moving forward/backwars at my current rotation, its moving on places it shouldnt
     
  9. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    instead of: rb.AddForce(transform.forward * PlayerSpeed * Time.deltaTime);, just do:
    rb.AddForce(transform.forward * PlayerSpeed * Time.deltaTime, ForceMode.VelocityChange);
    I think this way your force needs to be smaller, then you could cap the velocity by:
    if (rb.velocity.Magnitude > PlayerSpeed) {
    rb.velocity = rb.velocity.Normalized * PlayerSpeed;
    }
     
  10. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    And you messed up your code a little in line 25, you wrote PlayerSpeed instead of RotateSpeed
    If you want to move backwards, put a - before the transform.forward
     
  11. danilo12

    danilo12

    Joined:
    Jul 21, 2015
    Posts:
    64
    Thank you so much everything worked and now hes just going too fast, but i gues this is for fixing it
    if(rb.velocity > PlayerSpeed){
    rb.velocity = rb.velocity.Normalize * PlayerSpeed;
    }
    -the problem is that codeblock is giving me an error: Operator `>' cannot be applied to operands of type `UnityEngine.Vector3' and `float'
     
  12. danilo12

    danilo12

    Joined:
    Jul 21, 2015
    Posts:
    64
    Operator `*' cannot be applied to operands of type `method group' and `float'
    The other error is me being stupid..
     
  13. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Yeah, you know the magnitude from the end is important
     
  14. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Sorry, I messed up, it's not rb.velocity.Normalize, it's rb.velocity.normalized
     
  15. danilo12

    danilo12

    Joined:
    Jul 21, 2015
    Posts:
    64
    Now it's giving me this Operator `>' cannot be applied to operands of type `method group' and `float'
     
  16. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Second answer, this:
     
  17. danilo12

    danilo12

    Joined:
    Jul 21, 2015
    Posts:
    64
    yes i did it like that this is the code:
    if(rb.velocity.Magnitude > PlayerSpeed){
    rb.velocity = rb.velocity.normalized * PlayerSpeed;
    }
    it finds an error at if(rb.velocity.Magnitude > PlayerSpeed)
     
  18. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Sorry lowercase m
     
  19. danilo12

    danilo12

    Joined:
    Jul 21, 2015
    Posts:
    64
    It accelarates really fast idk why
     
  20. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    You need to tweak the playerspeed