The question is answered, right answer was accepted
Moving Plattform interaction with player
So in my jumping game i want moving plattforms : Now the plattforms work fine on their own, their iteraction with the player is wierd though in 2 situations:
if the player is below the plattform, he doesnt get pushed away/ "crashed". Instead the plattform moves through the player. Adding a rigidbody to the plattform makes it worse, because then the player even starts to push the plattform away instead of the other way around. How can i have the plattform push the player away from beeing underneath?
if the plattform goes in a direction other than the y-axis and the player is ON the plattform. The player isnt moved along with the plattform. So he kind a has to run to stay on the moving plattform. How can i make the player move with the plattform in all 3 axis?
Here the moving plattform code, not sure if it's of any help for answering my question though.
using UnityEngine;
using System.Collections;
public class MovingAB : MonoBehaviour {
public float x = 0;
public float y = 0;
public float z = 0;
public float offset = 0; //percent done going to B
public float speed = 2;
Vector3 PointA;
Vector3 PointB;
Vector3 direction;
float way;
float wayDone =0;
bool gotoB = true;
// Use this for initialization
void Start () {
PointA = transform.position;
PointB = PointA + new Vector3 (x, y, z);
direction = (PointB - PointA).normalized;
way = (PointB - PointA).magnitude;
transform.position += offset *way/100 * direction;
wayDone += offset* way/100;
}
// Update is called once per frame
void Update () {
if (gotoB) {
transform.position += direction * speed * Time.deltaTime;
wayDone += speed * Time.deltaTime;
} else {
transform.position -= direction*speed*Time.deltaTime;
wayDone += speed * Time.deltaTime;
}
//check if reached pointA or B and reverses direction
if (wayDone >= way) {
gotoB = !gotoB;
wayDone -= way;
}
}
}
Answer by Happy-Zomby · Apr 09, 2016 at 07:48 AM
Hi, I would recommend using colliders and functions on triggerStay and on triggerExit
function OnTriggerStay(other:Collider)
{
if(other.gameObject.tag == "platformAbove")
{
transform.position += platform.movement; //add the platform movement to your player
}
}
Then you could also create a second collider under the platform and ontriggerstay or on triggerenter it would kill or move your player. Hope this helps,
Answer by Ryanless · Apr 09, 2016 at 10:14 PM
Great, that works. I didnt use tag regonize, that way it moves anything on top of it . For example boxes, or enemies.
Now for the crushing part, the scaling of the transform works nice. There's a litte problem with the collider though:
// scales the collider if its not a boxcollider
void scaleCollider(Collider c){
if (c.GetType () == typeof(SphereCollider) || c.GetType () == typeof(CapsuleCollider)) {
if (c.radius >= 0.1) { //yes, i do know that c has a radius!!!
c.radius -= speed * Time.deltaTime * crushspeedfactor;
}
}
}
It wont accept c.radius. How can have it know that c is not only a collider but spherecollider/capsulecollider?
Hi, if you scale your transform, your collider should scale with it if I'm not mistaken. Is that not the case? (assu$$anonymous$$g collider is on the same gameobject as the transform you are scaling or on a child one). I would just transform.localscale of the main gameobject and make sure you move it down by half the crush factor to prevent it fron taking off from the ground. I think you should also deactivate the collider during the crusing to prevent any issues hope that helps,
it does so for boxcollider and the height of capsulecolliders. Wished it was for all colliders like that. But:
The radius of the spherecollider is just a collider with radius r (=max( x,y,z)). max(x,z) for capsule. And since i just scale them in y-direction (actually i even "flaten" them by increasing x,z size slightly).
Spheres keep their collidersize completly and capsulecollider get resized to radius x-sphere (asu$$anonymous$$g x==z).
And simply deactivating (enabled = false) or destroying it?
Follow this Question
Related Questions
any tutorial for playercontroll, physics ? 0 Answers
2D Rougelike Tutorial-Part 11 Player Does Not Move 0 Answers
Third person game hit a ball 0 Answers
C# Lerping Problem 0 Answers
Player sticking walking around inside tunnel.. Raycast HOW? 0 Answers