playMaker

Author Topic: Physics and moving platform [SOLVED]  (Read 26511 times)

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Physics and moving platform [SOLVED]
« on: March 23, 2014, 07:33:09 AM »
Hi,

I have a physics based character which jumps, walks etc. I have gravity switches in place and have stumbled upon a problem. My character cant stay on moving platforms. It just slides off.

My setup
My character at the moment doesn't use the CharacterController
Everything works except landing on moving platforms
Im using a custom gravity switch as my game requires it (not RigidBody gravity)

when i add a character controller to use the moving platform, my character no linger jumps using AddForce.

is there a script which can add a moving platform feature to Physics? or have i got the wrong idea?

Hope it makes sense.

Nick
« Last Edit: October 29, 2014, 07:10:13 PM by coxy17 »

sebaslive

  • Hero Member
  • *****
  • Posts: 572
    • Frame Tale Studios
Re: Physics and moving platform
« Reply #1 on: March 24, 2014, 01:59:46 AM »
If i follow what you mean by moving platform you could add a small trigger box on the platform that when player enters trigger it becomes a child. The child player will move with the platform parent. When player jumps off trigger, on trigger exit, remove child. So you can move on the platform as it moves.
All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5


Follow me @sebasRez

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Physics and moving platform
« Reply #2 on: March 24, 2014, 04:00:42 AM »
Hi,

Ok, if i wanted to set the parent of my character from the platform itself, instead setting it from the player. then how would i do that?

Reason being is that there's going to be loads of platforms and i want the platform to set the player as its going to be a prefab.

Will this process reduce performance?

Thanks

Nick

sebaslive

  • Hero Member
  • *****
  • Posts: 572
    • Frame Tale Studios
Re: Physics and moving platform
« Reply #3 on: March 24, 2014, 04:29:33 PM »
you want the player to be the parent? I am not sure why you would want that. Even with a ton of platforms you just copy and paste each one and the effect will still work because...

Player jumps on moving platform and now they are a child of platform so
platform is the parent.
when player jumps off platform you use an action called remove child so the player is no longer a child of the platform so when the player jumps on another platform it will now become the child of that one and so on. So you just need to make one and copy the rest.
All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5


Follow me @sebasRez

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Physics and moving platform
« Reply #4 on: March 24, 2014, 04:52:39 PM »
Sorry, no I want the platform to be the parent. I created a FSM on the platform trigger using OnTriggerEnter and used SetParent but not sure how I pass the player to the setparent on the platform trigger?

Do I assisn a vector3 to my player? Confused.

Thanks

Nick
« Last Edit: March 24, 2014, 04:54:52 PM by coxy17 »

sebaslive

  • Hero Member
  • *****
  • Posts: 572
    • Frame Tale Studios
Re: Physics and moving platform
« Reply #5 on: March 24, 2014, 06:03:43 PM »
Oh okay so here are the steps.

player needs to have a rigidbody and a custom tag on it. I used Player
platform needs to have the collider with the trigger checkbox checked.


here is the FSM for the platform--
for the variables you need to create 2 gameobjects. Owner and Player.
states you need two, off and on.
for events you can just use finished.
the actions for off will be trigger event and set parent
actions for on will be trigger event, get owner and set parent
Trigger event has a store collider which I set to player.
For get owner I set it to the owner variable.
For set parent on OFF I specified the gameObject as player and parent to none
for set parent on ON I specified the gameObject as player and parent to Owner

triggerEnter tag is player and send event finished.
triggerExit tag is player and send event finished.




All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5


Follow me @sebasRez

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Physics and moving platform
« Reply #6 on: March 25, 2014, 11:34:17 AM »
yes that works great, thanks for spending time to explain.

Ive just noticed that the player tends to very slowly slide off the platform when moving. I have the player setup with a SphereCollider and the player is a Sphere at the moment. Is this why?

When i jump on the moving platform it moves slightly to the opposite way the platforms moving.

Thanks

Nick

sebaslive

  • Hero Member
  • *****
  • Posts: 572
    • Frame Tale Studios
Re: Physics and moving platform
« Reply #7 on: March 25, 2014, 04:32:57 PM »
I have not used the sphereCollider with these steps but I would try playing around with the cube, capsule and such to see what gets the best results.
All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5


Follow me @sebasRez

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Physics and moving platform
« Reply #8 on: March 25, 2014, 07:13:37 PM »
hi, ive tried a cube with a box collider and it behaves the same?

uploaded video of what happens...

https://www.dropbox.com/s/u5rmm4p1gd4778d/20140325_2309_16.avi
any more advice would be great

Nick

sebaslive

  • Hero Member
  • *****
  • Posts: 572
    • Frame Tale Studios
Re: Physics and moving platform
« Reply #9 on: March 26, 2014, 12:29:21 AM »
That looks it has to do with world/self on transform OR maybe the rigidbody is the issue. I am going to make a scene and try it out to see since this is interesting.
All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5


Follow me @sebasRez

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Physics and moving platform
« Reply #10 on: March 26, 2014, 08:31:18 AM »
thanks, im testing too but no luck so far

sebaslive

  • Hero Member
  • *****
  • Posts: 572
    • Frame Tale Studios
Re: Physics and moving platform
« Reply #11 on: March 26, 2014, 11:22:57 AM »
Okay so I found out that this is a recurring problem among many devs and I also found some alt solutions that might help some being free and some involving money... So the problem is the rigidBody

free!
Turn on kinematic when player lands on the platform! No more moving side to side but you can't move the player either unless unlocked.
--
Turn this into playmaker.
Code: [Select]
// Moving platform support
private var activePlatform : Transform;
private var activeLocalPlatformPoint : Vector3;
private var activeGlobalPlatformPoint : Vector3;
private var lastPlatformVelocity : Vector3;
 
// If you want to support moving platform rotation as well:
private var activeLocalPlatformRotation : Quaternion;
private var activeGlobalPlatformRotation : Quaternion;
 
function Update () {
 
    // Perform gravity and jumping calculations here
    ...
 
    // Moving platform support
    if (activePlatform != null) {
        var newGlobalPlatformPoint = activePlatform.TransformPoint(activeLocalPlatformPoint);
        var moveDistance = (newGlobalPlatformPoint - activeGlobalPlatformPoint);
        if (moveDistance != Vector3.zero)
                controller.Move(moveDistance);
        lastPlatformVelocity = (newGlobalPlatformPoint - activeGlobalPlatformPoint) / Time.deltaTime;
 
        // If you want to support moving platform rotation as well:
        var newGlobalPlatformRotation = activePlatform.rotation * activeLocalPlatformRotation;
        var rotationDiff = newGlobalPlatformRotation * Quaternion.Inverse(activeGlobalPlatformRotation);
 
        // Prevent rotation of the local up vector
        rotationDiff = Quaternion.FromToRotation(rotationDiff * transform.up, transform.up) * rotationDiff;
 
        transform.rotation = rotationDiff * transform.rotation;
    }
    else {
        lastPlatformVelocity = Vector3.zero;
    }
 
    activePlatform = null;
 
    // Actual movement logic here
    ...
    collisionFlags = myCharacterController.Move (calculatedMovement);
    ...
 
    // Moving platforms support
    if (activePlatform != null) {
        activeGlobalPlatformPoint = transform.position;
        activeLocalPlatformPoint = activePlatform.InverseTransformPoint (transform.position);
 
        // If you want to support moving platform rotation as well:
        activeGlobalPlatformRotation = transform.rotation;
        activeLocalPlatformRotation = Quaternion.Inverse(activePlatform.rotation) * transform.rotation;
    }
 
}
 
function OnControllerColliderHit (hit : ControllerColliderHit) {
    // Make sure we are really standing on a straight platform
    // Not on the underside of one and not falling down from it either!
    if (hit.moveDirection.y < -0.9 && hit.normal.y > 0.5) {
        activePlatform = hit.collider.transform;   
    }
}

This is based on the 2d Platformer and has rotations and all. here is the link for full details. http://answers.unity3d.com/questions/8207/charactercontroller-falls-through-or-slips-off-mov.html
---
Leave it as is and increase the platformer speed. I found another tutorial on this forums that does it this way as well.

Paid!
Soooo the reason I didn't run into this problem on my platformer was because I was using this asset. http://forum.unity3d.com/threads/173640-Released-2D-Platform-Controller
The asset has its own built in gravity system which means no rigidbodies.

Or maybe someone on here has found another solution which would be amazing if shared :D
All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5


Follow me @sebasRez

Graham

  • Sr. Member
  • ****
  • Posts: 333
  • I Love Playmaker!
    • Portfolio
Re: Physics and moving platform
« Reply #12 on: March 26, 2014, 06:03:56 PM »
My own kit uses a variation of the parenting solution sebaslive suggested. My character uses a capsule collider, and moving platforms work just fine. I don't think using a sphere is causing your sliding.

Does your collider have a physic material applied? If not, create a physic material and drag it into your collider's material slot. In the material play with the friction settings, perhaps this can help.

Another possibility is, you say it doesn't use the rigidbody gravity. Maybe that has something to do with your issue. What kind of gravity are you using if not rigidbody's gravity?
More templates on the Unity Asset Store!

Disclosure: We are using affiliate links to our assets; we may receive a commission for purchases made through them. This helps us to continue developing and maintaining great products!

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Physics and moving platform
« Reply #13 on: March 26, 2014, 07:47:49 PM »
thanks sebaslive ill take a look at your options and let you know.

graham.... I have gravity set up using a vector3 as when I enter triggers my player can climb walls etc. So I had to turn RigidBody off. Ill take a look and see if this us the issue.  Also I have the default material on the sphere.  Only using this object as a temp character before making one.

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Physics and moving platform
« Reply #14 on: March 27, 2014, 05:26:46 AM »
Update:

Graham.....i have setup a new scene and dragged in a new cube and used RigidBody gravity and a box collider so i could see if the gravity was working and its the same result?

Also i created a physical material and have decreased and increased friction to see if that helps but it doesnt. Putting friction down to 0 helped a little more than increasing it.

Sebaslive... The paid option you said below, will it support a 3D game?
Quote
http://forum.unity3d.com/threads/173640-Released-2D-Platform-Controller

Its frustrating lol!

When i turn on 'Kinematic' it works ok but i cant then jump etc. grrrrr

Thanks

Nick
« Last Edit: March 27, 2014, 05:29:45 AM by coxy17 »