Search Unity

Character moving through the wall

Discussion in 'Physics' started by BambusTwins, Aug 20, 2016.

  1. BambusTwins

    BambusTwins

    Joined:
    Aug 20, 2016
    Posts:
    22
    Hello,
    I was making my project in blender, but my professor told me about unity3d, so i wanted to move my project in unity3d.
    It looks better but my game doesnt work now.
    The players move through the ball and also through the walls.
    I want them to kick the ball and to not move through the walls.
    I watched tutorials and videos and added a rigid body and some colliders but still doesnt work..

    Picture: https://i.gyazo.com/95c37cb92bc32047ea42a86b594ab0ec.png
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    BambusTwins likes this.
  3. BambusTwins

    BambusTwins

    Joined:
    Aug 20, 2016
    Posts:
    22
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
  5. BambusTwins

    BambusTwins

    Joined:
    Aug 20, 2016
    Posts:
    22
  6. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    You should use FixedUpdate, not Update for physics. Also, as @mgear said, use Rigidbody.MovePosition (or Rigidbody.AddForce, whatever works better for your game). Finally, it is worth noting that Unity3D is notorious for having small colliders moving at fast speeds get stuck inside other colliders. Also avoid Mesh Colliders, they are expensive and can be buggier.

    P.S. Don't share screenshots of code, insert it inside the forum post. Also, your chances of finding someone who will use Teamviewer to help you are pretty low.
     
    BambusTwins likes this.
  7. BambusTwins

    BambusTwins

    Joined:
    Aug 20, 2016
    Posts:
    22
    I am using this code now:
    using UnityEngine;
    using System.Collections;

    public class ExampleClass : MonoBehaviour {
    public Vector3 teleportPoint;
    public Rigidbody rb;
    void Start() {
    rb = GetComponent<Rigidbody>();
    }
    void FixedUpdate() {
    rb.MovePosition(transform.position + transform.forward * Time.deltaTime);
    }
    }
    I think its ok as you said to use FixedUpdate, also i removed Mesh Colliders but still my players fly!
     
  8. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Well, are you using gravity? Your screenshots don't show that :)

    Better on inserting the code, but it should look like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour {
    5. public Vector3 teleportPoint;
    6. public Rigidbody rb;
    7. void Start() {
    8. rb = GetComponent<Rigidbody>();
    9. }
    10. void FixedUpdate() {
    11. rb.MovePosition(transform.position + transform.forward * Time.deltaTime);
    12. }
    13. }
     
    BambusTwins likes this.
  9. BambusTwins

    BambusTwins

    Joined:
    Aug 20, 2016
    Posts:
    22
  10. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    BambusTwins likes this.
  11. BambusTwins

    BambusTwins

    Joined:
    Aug 20, 2016
    Posts:
    22
    When i select use gravity my players fall down...
     
  12. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Onto the ground right? That's good. If they fall thru the ground, add a box collider to the ground.
     
    BambusTwins likes this.
  13. BambusTwins

    BambusTwins

    Joined:
    Aug 20, 2016
    Posts:
    22
  14. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Lol I really don't get what you're doing. This is pretty simple:

    1: Your players must have colliders. I recommend box or sphere colliders.
    2: Your floor must have a collider.
    3: Make sure no objects are inside of each other.

    Try disabling your control script and just get your scene so it doesn't fly anywhere. Once you do that then we can talk about scripting :)
     
    BambusTwins likes this.
  15. BambusTwins

    BambusTwins

    Joined:
    Aug 20, 2016
    Posts:
    22
    Thank you a lot, now its okay they doesnt fall but i cant move them...
    https://i.gyazo.com/1f87571f8dfc0d63d8f735469999064b.gif

    here is my script for moving:
    using UnityEngine;
    using System.Collections;

    public class ExampleClass : MonoBehaviour
    {
    public Vector3 teleportPoint;
    public Rigidbody rb;
    void Start()
    {
    rb = GetComponent<Rigidbody>();
    }
    void FixedUpdate()
    {
    rb.MovePosition(transform.position + transform.forward * Time.deltaTime);
    }
    }
     
  16. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    FANTASTIC :D

    Now that nothing is jumping or flying, let's move this!
    Are you sure you don't have constraints on your RigidBody? Also why in the code you gave me the class is ExampleClass but in the inspector it is Minjon1? Make sure they match. Also put a Debug.Log in your fixedupdate to see if it is working at all.
     
    BambusTwins likes this.
  17. BambusTwins

    BambusTwins

    Joined:
    Aug 20, 2016
    Posts:
    22
    I wrote you in private conversation
     
  18. BambusTwins

    BambusTwins

    Joined:
    Aug 20, 2016
    Posts:
    22
    Hello i put a Debug.Log and it gives me:
    https://i.gyazo.com/ebf61e388b403090ff2bf6993f9b21fa.gif