0
\$\begingroup\$

I am trying to prevent a player cube from falling through the plane (Battleground) in Unity, but after adding Mesh collider to both the cube and plane, as well as adding a rigidbody, nothing seems to work. The only way I can prevent this from happening is by locking the y axis of the cube, but this is a problem because I am adding in a jumping ability, and the player needs to be able to fall of the map. Is this something I can do within the Unity editor, or do I need to make a script that does that.

To verify, all 3 colliders have "Is Trigger" Enabled, the platform is not designed to be moved, so the X, Y and Z of both the plan's rotation and position are locked, and the material for the colliders are all default. Each of the cubes and the plane's material are custom (Not the collider's material). The X, Y and Z of the cube's rotation are locked. Both cubes have the tag 'player'

The code for player movement is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMove : MonoBehaviour {
    private Rigidbody rb;
    // Use this for initialization
    void Start() {
        rb = GetComponent<Rigidbody>();
    }

    //Update is called once per frame
    void FixedUpdate() {
        float h = Input.GetAxis("Horizontal") * 5;
        float v = Input.GetAxis("Vertical") * 5;

        Vector3 vel = rb.velocity;
        vel.x = h;
        vel.z = v;
        rb.velocity = vel;
    }

}
```
\$\endgroup\$
11
  • \$\begingroup\$ Show us your code for how you're moving the cube. The usual mistake here is moving the cube with the Transform component instead of with collision-aware physics methods. Also, you'll likely want to use a box collider over a mesh collider whenever your case allows. \$\endgroup\$
    – DMGregory
    Aug 23, 2019 at 1:37
  • \$\begingroup\$ @DMGregory I added that in. The problem is, I only know how to make it move with transform \$\endgroup\$
    – Wendy
    Aug 23, 2019 at 2:06
  • \$\begingroup\$ That's surprising, since the code you've shared does it the right way with a rigidbody, not with the transform. ;) So there's something else going on. Do you move the plane at all? Are either of the colliders marked as triggers? Please show us the complete steps we need to reproduce this problem in a new empty project, so we can help you spot what's wrong and test solutions. \$\endgroup\$
    – DMGregory
    Aug 23, 2019 at 2:10
  • \$\begingroup\$ @DMGregory I added in a few more details into the question. Hopefully that is enough detail. \$\endgroup\$
    – Wendy
    Aug 23, 2019 at 3:04
  • \$\begingroup\$ Don't mark the colliders as triggers. That says "these colliders should not be solid" — if you uncheck IsTrigger, does anything change? \$\endgroup\$
    – DMGregory
    Aug 23, 2019 at 3:06

1 Answer 1

0
\$\begingroup\$

Unchecked "Is Trigger". Because it will your object non solid as on the Unity page mentioned:

A trigger doesn't register a collision with an incoming Rigidbody. Instead, it sends OnTriggerEnter, OnTriggerExit and OnTriggerStay message when a rigidbody enters or exits the trigger volume.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .