- Home /
How to get a Character to move with a moving platform ?
I've been trying to get my character to move along with my moving platform (like when i jump unto the platform it carries me long with it), but so far have been unable to do so.
I have a character with CharacterController, it has also been tagged by 'Player'.
My moving platform has a 'Rigidbody' attached to it, a 'Configurable Joint'and a 'Box Collider'. The platform is tagged with 'JumpPad5'.
This is the script attached to my moving platform:
var targetA : GameObject;
var targetB : GameObject;
var speed : float = 0.1;
function FixedUpdate ()
{
var weight = Mathf.Cos(Time.time speed 2 Mathf.PI) 0.5 + 0.5; transform.position = targetA.transform.position weight + targetB.transform.position (1-weight);
}
So if anyone can help me, pleaso do.
Answer by superventure · Nov 05, 2011 at 07:37 PM
Can't you make the player a child to the platform with On trigger stay/exit events? That way the player will move, rotate, etc with the platforms movements AND speed, while still having their own controls.
function OnTriggerStay(other:Collider){
if(other.gameObject.tag == "platform"){
transform.parent = other.transform;
}
}
function OnTriggerExit(other:Collider){
if(other.gameObject.tag == "platform"){
transform.parent = null;
}
}
that was a lot of help, thanks!
Quick question, though. I only want the player to ride the platform. The scale on the platform is different, and it changes the size of the player. Is there a way to make it so that the player doesn't change size and ins$$anonymous$$d just is parented via only the position?
well you can set only the position and rotation manually like this:
transform.position = other.transform.position;
transform.rotation = other.transform.rotation;
EDIT: AND you don't need an OnTriggerExit, BUT you can't move while on the platform. The best thing you can do is to put the graphics of the platform into a child, and on the parent change the size of the collider accordingly to the graphics, and use the code by @superventure
This is what happens when you don't use Empty objects.
The right way to implement the platform would be:
Platform (Empty)
Platform's $$anonymous$$esh (Scaled and possibly with attached collider objects here)
Player (Parented to the Empty which isn't scaled)
The Empty has the attached scripts and is what you modify the transform and rotate properties of.
the platform empty, with a platform in it didn't work for me. I even went into the code attached to the player and wrote "if(theCollision.gameObject.name == "_Empty"){
An empty isn't a collision, and it doesn't have a trigger option either so..... i didn't expect it to work..
and I tried what you said, Doctor Jellyface (lol), but I got an error. this is what the code looks like:
function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.name == "$$anonymous$$over"){
transform.position = other.transform.position;
}
}
and I get the error: Assets/Scripts/Player.js(68,43): BCE0019: 'position' is not a member of 'UnityEngine.Collision'.
I have since found a workaround to my problem, but it would still be nice to know of a proper way of doing things.
The collision would still happen in the mesh, but you'd attach movement and the player to the Empty.
Answer by Mahtub · Dec 22, 2016 at 04:30 PM
i have a simple way.
1.add box collider to your platform and make sure that isTrigger is enabled
2.create new c# script
3.attach the c# script to the platform you want
4.copy script below to your c# script that you made
private GameObject target=null;
private Vector3 offset;
void Start(){
target = null;
}
void OnTriggerStay2D(Collider2D col){
target = col.gameObject;
offset = target.transform.position - transform.position;
}
void OnTriggerExit2D(Collider2D col){
target = null;
}
void LateUpdate(){
if (target != null) {
target.transform.position = transform.position+offset;
}
}
good Luck
first share is here http://answers.unity3d.com/answers/1289185/view.html
Finally a solution that worked! Thanks! Every other solution changed the scale of my player when it was affected by gravity.
$$anonymous$$y character is still just falling through the platform
This is exactly what I needed thanks! but I had a problem when my player get inside the platform and tried to move it outside the platform again it doesnt move.. how to fix? your response is much appreciated..
Answer by dsamuelson · Aug 06, 2015 at 03:35 PM
I've been working on this for 2 days and finally found a way so that the character can stay on the platform and then be able to move around and continue to stay on the platform.
The principle is simple you want it so that when you collide with the moving platform that the character changes to kinematic mode and the parent is the collider that way the character moves with the collider. so something like this:
void OnCollisionEnter2D (Collision2D coll) {
if (coll.transform.tag == "Moving Platform")
{
GetComponent<Rigidbody2D>().isKinematic=true;
transform.parent = coll.transform;
}
}
Now that's all well and good but now while it's kinematic and parented it's unable to move on it's own and programming extra controls to control it in this state is a pain so what you want is that if you move or jump then the character is no longer kinematic or parented. This is best done in the functions update and fixedupdate because it will be checked every frame. So something like this:
if (move != 0 && transform.parent != null)
{
GetComponent<Rigidbody2D>().isKinematic= false;
transform.parent = null;
}
This command basically says if your moving and your parented then the player is no longer kinematic and is no longer parented. Then add in your command for jumping so something similar to this:
if ((grounded) && Input.GetKeyDown (KeyCode.Space))
{
GetComponent<Rigidbody2D>().isKinematic = false;
transform.parent = null;
}
This way when you are grounded (on the moving platform) and when you press your jump key or function then the object is no longer kinematic and is no longer parented.
This way while you are in collision with the moving platform you will be kinematic and moving with the platform because it's your object's parent. But if you are moving or jumping then you are not moving with the platform and the platform is no longer your parent.
Thanks a lot, man. It works better if you use OnCollisionStay, though.
This is working for me but stretching my character. Any Idea's why?
Answer by Sebas · Feb 24, 2010 at 10:31 PM
There are already multiple answers for this question on the site. Please use the search option for next time. These Answers should get you started. If questions remain after reading those, feel free to post them.
I already checked those answers and I couldn't get it working with my character. That's why I asked the question myself, providing my own information, hoping that would help.
Not sure which option would be best, but I'd try to implement those other solutions first and ask questions in those answers via comments. What exactly didn't work when you tried those solutions? There are multiple answers provided, so it's not very likely that somebody is going to take the time and come up with something totally new here. Was the joint a problem? Try to break your platform/project down so you can get it to work with one of the provided solutions and so that you understand how they got it working, then expand to suit your needs. But that's just a thought.
When I use 'Answer1' I get an error saying: Assets/CharStickPlatform.js(26,44): BCE0020: An instance of type 'FPSWalker' is required to access non static member 'speed'. How do I solve this?
Do you use the Character Controller with the FPSWalker script? The script for Answer1 requires you to use the FPSWalker script, because it refers to its variable speed.
No I probably didn't cause I don't know how to do this. (But I'm not trying to make a FPS, I'm trying to make a 3rd person game.)
Answer by Fatback · Jun 22, 2017 at 03:59 PM
Here is a video that allows you too get multiple objects to stick to a platform while allowing you to interact and manipulate them all you want. https://youtu.be/owX_t5OYhFg
Your answer
Follow this Question
Related Questions
How to prevent Character Controller from falling through moving Platforms? 4 Answers
Trying to avoid collision with sides and interiors of polygons 1 Answer
Character falling through moving object using iTween 1 Answer
Voxel engine physics 3rd person character controller 1 Answer
moving platforms 1 Answer