Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: coxy17 on March 23, 2014, 04:33:09 AM

Title: Physics and moving platform [SOLVED]
Post by: coxy17 on March 23, 2014, 04: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
Title: Re: Physics and moving platform
Post by: sebaslive on March 23, 2014, 10:59:46 PM
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.
Title: Re: Physics and moving platform
Post by: coxy17 on March 24, 2014, 01: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
Title: Re: Physics and moving platform
Post by: sebaslive on March 24, 2014, 01: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.
Title: Re: Physics and moving platform
Post by: coxy17 on March 24, 2014, 01: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
Title: Re: Physics and moving platform
Post by: sebaslive on March 24, 2014, 03: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.




Title: Re: Physics and moving platform
Post by: coxy17 on March 25, 2014, 08: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
Title: Re: Physics and moving platform
Post by: sebaslive on March 25, 2014, 01: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.
Title: Re: Physics and moving platform
Post by: coxy17 on March 25, 2014, 04: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 (https://www.dropbox.com/s/u5rmm4p1gd4778d/20140325_2309_16.avi)
any more advice would be great

Nick
Title: Re: Physics and moving platform
Post by: sebaslive on March 25, 2014, 09:29:21 PM
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.
Title: Re: Physics and moving platform
Post by: coxy17 on March 26, 2014, 05:31:18 AM
thanks, im testing too but no luck so far
Title: Re: Physics and moving platform
Post by: sebaslive on March 26, 2014, 08: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
Title: Re: Physics and moving platform
Post by: Graham on March 26, 2014, 03:03:56 PM
My own kit (http://3dsauce.com/demos/platformkit.html) 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?
Title: Re: Physics and moving platform
Post by: coxy17 on March 26, 2014, 04: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.
Title: Re: Physics and moving platform
Post by: coxy17 on March 27, 2014, 02: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
Title: Re: Physics and moving platform
Post by: sebaslive on March 27, 2014, 07:11:49 AM
It's designed more for 2.5D so it has the 3D option but the code is specifically made for 2D. An option would be tearing it apart but that would take much time.

Graham, would your kit work in 3d?

This seems to be much more frustrating then I thought  :-\
Title: Re: Physics and moving platform
Post by: coxy17 on March 28, 2014, 06:53:34 AM
sebaslive, I added a RigidBody to the platform and this helped a little in the fact that its stays on the platform BUT slides slightly for the first second but then moves with the platform.

Do you think the Velocity has something to do with it?

Thanks

Nick
Title: Re: Physics and moving platform
Post by: sebaslive on March 28, 2014, 07:20:57 AM
From the tests I did, if you add a lot of mass to the player, around 1000 it wont move, but you would need to add a ton more force for it to jump and such.

It also appears to move a lot less based on how fast the platform is moving since it doesnt give it enough time to slide off.

Also, a ton of drag would have the same effect as the mass but again would require a ton more force.

I dont find these to be the best solutions but they work...ish
Title: Re: Physics and moving platform
Post by: Graham on March 28, 2014, 08:09:08 AM
Yes sebaslive, my kit is in fact a 3d solution, just running on 2 axis'. If I knew why it works for me I would share, but I can't see what exactly I am doing differently.

I can tell you my character has a mass of 1.8, so I don't think upping the mass is a solution I would go with since it adds other issues.

Also, I can make my platforms go extremely fast and the character doesn't move an inch. Also my platforms do not have a rigidbody.

The only other thing I can point out is, my platforms move using the "iTween Move To" action. You say you move yours with velocity?
Title: Re: Physics and moving platform
Post by: coxy17 on March 31, 2014, 05:01:47 AM
Graham have you tried this without your kit?

I'm using the 'itween move to' as well.

Creating a new scene with no custom scripts or addons included and my platform still behaves the same. If you don't mind could you create a test scene so I could compare settings to yours?

Seems like the only way to see whats happening with our setup.

Thanks

Nick
Title: Re: Physics and moving platform
Post by: coxy17 on April 06, 2014, 04:19:31 AM
Hi Sebaslive,

Not heard back from Graham but i have been testing and thought i would try and move the platform without using iTween.

I used Translate and did a ping pong motion using C# and did the same process again and found that the player sticks to the platform now. I had to make the moving platform kinematic and have the platform move using FixedUpdate.

Maybe there is something wrong with iTween?

Nick
Title: Re: Physics and moving platform
Post by: Graham on April 06, 2014, 07:57:58 AM
Sorry coxy, I haven't had a chance to try it out yet. I do use iTween on mine, it doesn't cause any issues here.

You mention having to set the platform as kinematic. My platforms do not use a rigidbody component! Maybe that is your issue.
Title: Re: Physics and moving platform
Post by: coxy17 on April 06, 2014, 10:18:39 AM
Hi graham. I've tried it without a RigidBody but it behaves the same with iTween. That's why I was after a test scene to compare. No worries if you haven't got time..
Title: Re: Physics and moving platform
Post by: coxy17 on April 08, 2014, 02:51:11 PM
Hi graham,

Just wondered if you have had any time to take a look?

cheers

nick
Title: Re: Physics and moving platform
Post by: Graham on April 08, 2014, 05:37:56 PM
No I haven't, a lot of overtime lately. If you wanna send me your scene though I can take a look. It's quicker than making one from scratch myself.
Title: Re: Physics and moving platform
Post by: coxy17 on April 09, 2014, 08:47:32 AM
Hi, Ive attached it

Thanks for taking your time to look :)

Nick
Title: Re: Physics and moving platform
Post by: Graham on April 09, 2014, 11:00:30 AM
Hey Nick,

I played with your scene for a little while, unfortunately I don't know what's wrong with it and I'm not able to go much deeper into it right now.

Sorry,
Graham
Title: Re: Physics and moving platform
Post by: coxy17 on April 10, 2014, 02:31:49 PM
OK thanks anyway Graham

Would be interesting to know what's different in your games setup.

But i think iTween has got a problem with using FixedUpdate (realtime) feature?

Nick
Title: Re: Physics and moving platform
Post by: Graham on April 11, 2014, 05:45:01 AM
Not sure if you noticed, but the entire project is available on the Unity Asset Store. (https://www.assetstore.unity3d.com/#/content/11892)

You can always dissect it to see what you are able to find out.
Title: Re: Physics and moving platform
Post by: coxy17 on August 04, 2014, 12:30:21 AM
Not sure if you noticed, but the entire project is available on the Unity Asset Store. (https://www.assetstore.unity3d.com/#/content/11892)

You can always dissect it to see what you are able to find out.

Hi, ive finally got around to looking over your project and i cant see what the difference is? strange. Would be great if someone had a sample scene with it working.
Title: Re: Physics and moving platform
Post by: jeanfabre on August 04, 2014, 03:59:26 AM
Hi,
 
Ok, have you tried this:

http://hutonggames.com/playmakerforum/index.php?topic=2327.0

And also, I can't find the original trhead on this, but I made an elevator scene using iTween and character controller and all is working very well. I have attached the scene again on that  post. It's basically showing how to create lifts, but instead of moving up and down simply move sideways, the principles is identical.


Bye,

 Jean
Title: Re: Physics and moving platform
Post by: coxy17 on August 04, 2014, 07:48:10 AM
Hi Jean,

Ive taken a look and have applied to game but it still dont behave correctly as im using physics and not a character controller. Ive basically adapted your example and tested in a new scene.

to save you reading back on this post, basically my character attaches to the moving platform using Set Parent. All works fine. When using a platform with iTween it causes the player to slowly slide of the platform. The only way ive got it to work is with FixedUpdate without iTween. It seems from mine and grahams conversation is that iTween doesnt seem to work with FixedUpdate/Realtime feature?

ive also tried using 'is kinematic' set on the platform but behaves the same. im at a loss now :( i like the flexibility of iTween but wont behave correctly.

sorry if anything ive said above is not quite correct.

thanks

Nick
Title: Re: Physics and moving platform
Post by: jeanfabre on August 05, 2014, 04:04:49 AM
Hi,

 never attach transforms when using Physics. That's never going to work out well. ONLY use either the natural physics engine behavior to have objects colliding with one another or use joints ( fixed joints would do here).

 Bye,

 Jean
Title: Re: Physics and moving platform
Post by: coxy17 on August 05, 2014, 05:16:02 AM
Hi, ok i sort of understand what you mean. ill have to do a bit of research.

The only reason i need the platform as the parent is that when the player jumps up and down (when on the platform) they move with the platform horizontally too.

this is the script im currently using for my moving platform (works ok), can this be achieved in playmaker alone? behavior is like itween ping pong.

Code: [Select]
using UnityEngine;
using System.Collections;

public class MovingPlatform : MonoBehaviour {

public Transform DestinationSpot;
public Transform OriginSpot;
public float Speed;
public bool Switch = false;

void FixedUpdate()
{
// For these 2 if statements, it's checking the position of the platform.
// If it's at the destination spot, it sets Switch to true.
if(transform.position == DestinationSpot.position)
{
Switch = true;
}
if(transform.position == OriginSpot.position)
{
Switch = false;
}

// If Switch becomes true, it tells the platform to move to its Origin.
if(Switch)
{
transform.position = Vector3.MoveTowards(transform.position, OriginSpot.position, Speed);
}
else
{
// If Switch is false, it tells the platform to move to the destination.
transform.position = Vector3.MoveTowards(transform.position, DestinationSpot.position, Speed);
}
}
}
Title: Re: Physics and moving platform
Post by: jeanfabre on August 06, 2014, 03:37:56 AM
Hi,

 Yes, this is feasible in PlayMaker, but if using some custom actions to provide access to actions that lets you operate in the FixedUpdate call.

 typically, you can find them all on the ecosystem (https://hutonggames.fogbugz.com/default.asp?W1181), search for "advanced".

Bye,

 Jean
Title: Re: Physics and moving platform
Post by: coxy17 on August 07, 2014, 12:48:22 PM
Hi Jean,

Ive taken a look through the Ecosystem and cant find any script which i could use for this? looks like im out of options, not technically minded enough to work this out by writing one to incorporate into playmaker, not to worry.

thanks anyway

nick
Title: Re: Physics and moving platform
Post by: jeanfabre on August 08, 2014, 04:45:38 AM
Hi,

 Ok, don't worry, it's all there. Get back to me next week, I'll make a working sample in playmaker doing what this script does.

 Bye,

 Jean
Title: Re: Physics and moving platform
Post by: coxy17 on August 12, 2014, 05:57:37 AM
thanks, is it complicated to make? just reminding you as its next week now :)

greatly appreciated

Nick
Title: Re: Physics and moving platform
Post by: jeanfabre on August 18, 2014, 01:15:50 AM
Hi,

 ok, I worked a new ecosystem sample for this, where it shows the good and bad way to do this ( with the right custom actions) and how ti affects the physics simulation.

You can download it here (https://github.com/jeanfabre/PlayMakerCustomActions_U3/raw/c23c8bcf5d41af5dcc79736dd40a1432e3905aac/PlayMaker/Ecosystem/Custom%20Samples/Physics/RigidBodyPlatformMovement.unitypackage)

Bye,

 Jean
Title: Re: Physics and moving platform
Post by: coxy17 on August 19, 2014, 12:44:18 PM
Hi Jean,

the samples are great. however, i noticed that the cubes in the demo scene have drag on them when the platform returns. Im after the behaviour where the player moves with the block with precision and no shaking/drag.

Ive used your demo and have tried my player as a child of the platform and also i have tried this the way you have in your demo (not a child object) but my character falls off very quickly?

Ive tried tweaking with the player settings but still no joy.

Nick

Title: Re: Physics and moving platform
Post by: coxy17 on September 05, 2014, 07:56:50 AM
no luck. just used a script instead.
Title: Re: Physics and moving platform
Post by: jeanfabre on September 09, 2014, 05:17:18 AM
Hi,

 if you use the physics engine, then this drag and player behavior is to be expected (especiall if it has a weight), indeed you'll need to switch into a non physics behavior if you want a completly still player on a moving platform. It can be done with PlayMaker, but script will be fine of course.

 Bye,

 Jean
Title: Re: Physics and moving platform
Post by: sebaslive on September 11, 2014, 08:52:44 PM
Hey Coxy, what script did you end up using??
Title: Re: Physics and moving platform
Post by: coxy17 on October 29, 2014, 04:09:50 PM
hey sorry for the late reply, here is the code i used

Code: [Select]
using UnityEngine;
using System.Collections;

public class MovingPlatform : MonoBehaviour {

public Transform DestinationSpot;
public Transform OriginSpot;
public float Speed;
public bool Switch = false;
private float pauseTime;
public float delayBeforeMoving;
private bool arrivedAtOurDestination = false;

void FixedUpdate()
{
// For these 2 if statements, it's checking the position of the platform.
// If it's at the destination spot, it sets Switch to true.
if((transform.position == DestinationSpot.position) && !arrivedAtOurDestination)
{
Switch = true;
pauseTime = Time.time + delayBeforeMoving;
arrivedAtOurDestination = true;
}
if((transform.position == OriginSpot.position) && !arrivedAtOurDestination)
{
Switch = false;
pauseTime = Time.time + delayBeforeMoving;
arrivedAtOurDestination = true;
}

// If Switch becomes true, it tells the platform to move to its Origin.
if(Switch && (Time.time > pauseTime))
{
transform.position = Vector3.MoveTowards(transform.position, OriginSpot.position, Speed);
arrivedAtOurDestination = false;
}
else if (Time.time > pauseTime)
{
// If Switch is false, it tells the platform to move to the destination.
transform.position = Vector3.MoveTowards(transform.position, DestinationSpot.position, Speed);
arrivedAtOurDestination = false;
}
}
}