Moving colliders moving through player *detailed explanation*

To start off with.
If the issue is very clear to you please explain it to me instead of telling me how easy it is, i have been through A LOT of tutorials but i´m still new to coding.
So every help is very appreciated, i really want to make a game which is enjoyable and fun and prove to my self that it is possible for me to get started in the game developer industry! :slight_smile:

The Issue
So i have a player which is a capsule and looks like this -

And a door who looks like this -

And the doors script to animate looks like this -

#pragma strict

var theDoor : Transform;
private var doorIsClosed = true;
var theDoorIsOpen = false;

var AudioPlaying = false;

var DoorOpen : AudioClip;
var DoorClose : AudioClip;
private var drawGUI = false;

function Update ()
{
    if (drawGUI == true && theDoorIsOpen == false && Input.GetKeyDown("mouse 0"))
    {
        TheDoorOpens();
    }
    else if (drawGUI == true && theDoorIsOpen == true && Input.GetKeyDown("mouse 0"))
    {
        TheDoorCloses();
    }
}

function OnTriggerEnter (theCollider : Collider)
{
    if (theCollider.tag == "Player")
    {
        drawGUI = true;
    }
}

function OnTriggerExit (theCollider : Collider)
{
    if (theCollider.tag == "Player")
    {
        drawGUI = false;
    }
}

function OnGUI ()
{
    if (drawGUI == true)
    {
        GUI.Box (Rect (Screen.width*0.5-50, Screen.height*0.5-25, 170, 22), "Left click to open the door");
    }
}

function TheDoorOpens()
{
        theDoor.animation.CrossFade("BasicDoorOpen");
        theDoorIsOpen = true;
        if (AudioPlaying == false)
        {
            audio.PlayOneShot(DoorOpen);
            AudioPlaying = true;
            yield WaitForSeconds(0.7);
            AudioPlaying = false;
        }
}
function TheDoorCloses()
{
            theDoor.animation.CrossFade("BasicDoorClose");
        theDoorIsOpen = false;
        if (AudioPlaying == false)
        {
            audio.PlayOneShot(DoorClose);
            AudioPlaying = true;
            yield WaitForSeconds(0.7);
            AudioPlaying = false;
        }
}

The problem is -

When the player left clicks to open the door the door can go right through him.
The door can´t go through him if it ain´t animated, only if it is animating, why is that?

I think that you need to add a rigidbody or check the ‘animate physics’ checkbox for it to work.
Objects without rigidbodies aren’t ment to move. It’s like that so unity doesn’t do useless caculations. That’s being changed in unity 5 though, which is sadly going to decrease the performance :confused: I just hope that it doesn’t have that much of an impact(I don’t really have much experience with Game Calculations).

should i add a rigidbody both to the player and the door? And should they both have animate physics?

I think that the character controller is already some kind of built-in rigidbody. o_o
try the ‘animate physics’ button first though!

did not help sadly… :i

did you add a rigidbody to the door? what size/shape is it?

also in the door’s rigidbody make sure to check “is kinematic” so it doesn’t just fall off

just added a rigidbody and checked Is Kinematic, but did not work either.
The doors size is 1 * 1 * 1

Maybe you should consider switching to a rigidbody character controller? :s
The Sample Assets(Beta) has a pretty sweet one :3
alternatively you could use joints & forces instead of using an animation! o;

also a thought: raycast to check whether the player is standing in the doorway.

you probably mean the door’s scale is 1x1x1 which isn’t its actual size in world space, just the percentage of its original width/height/depth (100%, 100%, 100%)

show us a screenshot of the rigidbody in scene view and in the inspector

here is it with a rigidbody :slight_smile:

I have tried with using coding to animate the objects but i always end up with a script setting it to 90 degress at the click instead of making a fade from 0 to 90 :3

oh i think i see what’s wrong here
the door object has both a mesh collider and a box collider, and the box collider is set to “is trigger” (because it’s used as a trigger) and so it won’t react in physics
the mesh collider isn’t convex, so the physics engine probably won’t use it
try checking the “convex” box on the mesh collider and see if it works

I tried it and it added a green looking collision to the door which you can see on the picture below, but sadly it does the same !! :hushed:

could the problem maybe be in my player?

actually it might be possible that the rigidbody thinks it’s supposed to be reacting with the trigger box instead of the mesh. usually people keep the trigger as a separate game object from the door itself.

here’s what i would try then:

  • make the door and the trigger as separate objects. if you need the trigger to move with the door, then make the trigger object a child of the door by dragging it under the door in the heirarchy.
  • your animation goes on the door object.
  • your script goes on the trigger object. for theDoor, you drag the door object into that slot in the inspector.
  • put the audio component on the trigger object as well to keep the code simple.
  • for the door object, give it a box collider, and use the editing tools to scale it to the size of the door
  • check “animate physics” on the animation component on the door.

It doesn’t have to be a seperate gameobject Õ 3 Õ
I also don’t think that the door should be kinematic. “If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore. The rigidbody will be under full control of animation or script control by changing transform.position.”
.

@Quist You can use functions like Vector3.Lerp / Mathf.Lerp to make it transition smoothly.

then the problem may actually be the player object. if you’re using a character controller, i know it doesn’t use regular physics like a normal rigidbody would – but then, using a normal rigidbody makes moving a player around a little bit more technical.

you may end up having to use a regular rigidbody for the player, if you want to be pushed around by a door. you would do everything related to your controls in FixedUpdate() and use rigidbody.MovePosition() to move

Still does the same :frowning:

Please check my pictures if i did it right:

The door object should look like the following -

And the trigger should look like this -

Yeah, thats where it starts to get complicated,

  • I cant find any models i can use since i can´t draw :stuck_out_tongue:
  • as magiichan mentioned i had to use rigidbody.MovePosition() to move which i have NEVER tried before :stuck_out_tongue:

It´s my first game and i dont know what people expect from it.
I know that I expect to atleast find a few people who actually like it and i want it to be something new

yeah it looks like you set it up right

it probably just is the charactercontroller then.
rigidbody.MovePosition() basically just moves the rigidbody to the given position in space, and it handles all the physics stuff itself. you just feed it a vector3 representing the new position.

so you would just get rid of your charactercontroller code & component, add a regular rigidbody, check the boxes for freeze rotations, and do something like this

var move : Vector3
move.x = Input.GetAxis("Horizontal");
move.y = 0;
move.z = Input.GetAxis("Vertical");
rigidbody.MovePosition( move + transform.position );
1 Like

Yey!!!
Now the player gets moved by the the door even when it animates :slight_smile:

So, i found a script to let me move the player around normally like if it had a character Controller.

Is it possible that you can tell me why the following code lets my player stay up in the air in a jump if i keep holding the space button down?
Again, thanks a lot for the help!! :smile:

var speed = 10.0;
var gravity = 10.0;
var maxVelocityChange = 10.0;
var canJump = true;
var jumpHeight = 2.0;
private var grounded = false;
@script RequireComponent(Rigidbody, CapsuleCollider)
function Awake ()
{
    rigidbody.freezeRotation = true;
    rigidbody.useGravity = false;
}
function FixedUpdate ()
{
    if (grounded)
    {
        // Calculate how fast we should be moving
        var targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        targetVelocity = transform.TransformDirection(targetVelocity);
        targetVelocity *= speed;
        // Apply a force that attempts to reach our target velocity
        var velocity = rigidbody.velocity;
        var velocityChange = (targetVelocity - velocity);
        velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
        velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
        velocityChange.y = 0;
        rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
        // Jump
        if (canJump && Input.GetButton("Jump"))
        {
            rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
        }
    }
    // We apply gravity manually for more tuning control
    rigidbody.AddForce(Vector3 (0, -gravity * rigidbody.mass, 0));
    grounded = false;
}
function OnCollisionStay ()
{
    grounded = true;   
}
function CalculateJumpVerticalSpeed ()
{
    // From the jump height and gravity we deduce the upwards speed
    // for the character to reach at the apex.
    return Mathf.Sqrt(2 * jumpHeight * gravity);
}