Unity - Child moves faster than parent - c#

I'm using the following code to make my player move along with moving platforms, and it works in that it does indeed make you the child of the platform and move along with it while on it, however the player will move faster than the platform.
The platform is moving left then right between two points repeatedly, but the player moves quicker, so will slide along the platform, and you obviously might fall off therefore.
void OnCollisionEnter2D (Collision2D other) {
if(other.gameObject.tag == "moving") {
transform.parent = other.transform.parent;
}
}
void OnCollisionExit2D (Collision2D other) {
transform.parent = null;
}
Now I figure that this code is fine because as I have said you become the child of the platform and move along which is all this does.

Was going to delete this but I thought it'd be best just to leave it here in case anyone has the same issue.
After removing friction from the platform and the player, everything happens as expected, and I can move with the moving platforms without pressing anything.

Related

Maintaining DontDestroyOnLoad method after object becomes a child to another object

in the game I am trying to make there is a level with moving platforms that make the player object a child for the duration of the collision.
private void OnCollisionEnter2D(Collision2D collision)
{
collision.transform.SetParent(transform);
}
private void OnCollisionExit2D(Collision2D collision)
{
collision.transform.SetParent(null);
}
Player does have a DontDestroyOnLoad method so he can move between the levels freely. However that method disappears whenever i come into contact with one of the moving platforms so the game crashes while going to the next level because the camera is using the player object to follow him and without player is missing a reference. I am pretty sure that the cause is that it becomes a child object of the platform and the platform itself does not use that method. Is there a way to keep the DontDestroyOnLoad method no matter the circumstances fn the player object?
The scene cannot be loaded automatically. It is controlled by you, so you can unlink the player's parent before switching to the next scene.
player.transform.SetParent(null);
DontDestroyOnLoad(player.gameObject);
SceneManager.LoadScene("next scene");

Character moves slow when parented to moving platform

I'm working on a 2D platformer game in Unity. My character moves by applying velocity to the rigid body and works just as I want it to for precise controls.
When the character is on a moving platform, I parent the character to the moving platform so that the character stays on while the platform moves. Here's the code that's on the moving platform
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player")
{
collision.collider.transform.SetParent(transform);
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player")
{
collision.collider.transform.SetParent(null);
}
}
I'm also using transform.position from one position to another to move the platform.
Here's the issue. The character works perfectly fine only if the speed of the platform is 0. If the platform is moving at all, the character will move like a slug while walking on the platform. Any ideas on how to fix the issue or is there a way to have the character ride on the moving platform without parenting it to it?
Edit: I forgot to include this earlier. I've tested to see if the physics materials would make a difference. I included a 0 friction physics material on the character, the platform, and both at the same time. None of the combinations made any difference. The player still move sluggish while the platform is moving.
If you are moving your character with a force and a rigidbody, the hierarchy and who is the parent is not going to have much influence as the movement is provided by the force, this is with the physics.
If you where manipulating the transform in the code for the movement that would be important, but if movement is produced by forces I think its not.
I would check the frictions of the Physic Materials of the player and the platform in order to define the movement of the elements respect each other.
It is quite a while for the answer but I think I found one. Moving the platform in FixedUpdate rather than in Update can fix this issue.

How to stop player from automatically jumping on a moving platform?

I made a moving platform that goes vertically top to bottom and then bottom to top and so on. Platform moves fine but placing my player on it makes my player unstable.
When platform moves from top to bottom, my player kinda bounces on it. When platform moves from bottom to top, it remain stable on the way but when it reaches top point, my player makes a jump by itself.
Its get even worse when i increase the speed of my platform. I don't know if its due to unity 2d physics effect or what. I have tried to use physics material 2D on my player object and platform by setting bounce to 0 and friction to 50 but nothing seems to work. Any one have idea how to disable physics effect of moving platform? Following is my code for moving platform:
public class BrickMoveVErtical : MonoBehaviour {
public Vector3 positionOne;
public Vector3 positiontwo;
public Vector3 nextposition;
/*Empty object is already made on unity editor and its parent of platform(Plank) and other
empty object "pointB". Point "B" is already mapped on editor and platform is set to go from
its original pos to point B */
public Transform plankTranform;
public Transform positionBTransform;
public float speed;
// Use this for initialization
void Start () {
positionOne = plankTranform.localPosition;
positiontwo = positionBTransform.localPosition;
nextposition = positiontwo;
}
// Update is called once per frame
void Update () {
move();
}
private void move() {
plankTranform.localPosition = Vector3.MoveTowards(plankTranform.localPosition,nextposition,Time.deltaTime*speed);
if(Vector3.Distance(plankTranform.localPosition,nextposition)<0.1)
{ changeMovementPlank(); }
}
void changeMovementPlank() {
nextposition = nextposition != positionOne ? positionOne : positiontwo;
}
}
I had similar problems with fast platforms in my game, the character would be left out in the air or slide off, depending on the direction of the platform.
I found a workaround for the problem by simply changing the character transform.parent to the platform it was jumping on, whenever the player exists the platform collider, simply return the players original transform.parent.
Note that this might cause other bugs or problems that will need to optimize, depending on your game that is.
You can use this code to set/unset the parent of a GameObject:
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "PlatformGroup")
player.SetParent(other.gameObject.transform)
}
void OnTriggerExit2D(Collider2D other)
{
if(other.gameObject.tag == "PlatformGroup")
player.SetParent(null);
}
What you should do is:
Create an empty GameObject, and you make your platform child of this
empty GameObject. Tag it as PlatformGroup.
You add the script to move the platform to this EmptyGame Object,
instead of using it in the platform itself.
Then add a collider to empty gameobject(PlatformGroup) and another to the Player. Set the one of the player as trigger.
Add the code above to your player controller.
Now, when the Player jumps over the Platform if will become a child of the same GameObject of the platform and they will move together without any deformations in the GameObject. Also when the player leaves the platform, walking or jumping out of it, it will stop being child.

Two scripts Input.GetMouseButton(0) in Unity

I have two scripts like this:
void Update()
{
if (Input.GetMouseButton(0))
{
if (GameObject.FindGameObjectWithTag("Object").transform.position.x > -1.9)
{
GameObject.FindGameObjectWithTag("Object").transform.Translate(Vector2.left * szybkosc);
}
}
}
And second script is similiar, only there is "Vector2.right" difference. If I have one script, it works perfectly, but when I add second script, they doesn't work. I add these scripts to two different gameobjects.
And second script is similiar, only there is "Vector2.right"
difference
That's the problem right there.
You are moving the object left with ("Object").transform.Translate(Vector2.left * szybkosc);
then you have another script that is moving it right with ("Object").transform.Translate(Vector2.right * szybkosc);.
What do you expect to happen? I think that the "Object" GameObject should remain still or even create a weird behavior. You can't be moving one Object to two opposite directions at the-same time.
Maybe the second script is supposed to be moving another GameObject GameObject.FindGameObjectWithTag("AnotherObject") instead of the first GameObject the first script is already moving....
Not related to your problem but you should call GameObject.FindGameObjectWithTag once in the Start function.
GameObject objToMove;
void Start()
{
objToMove = GameObject.FindGameObjectWithTag("Object");
}
void Update()
{
if (Input.GetMouseButton(0))
{
if (objToMove.transform.position.x > -1.9)
{
objToMove.transform.Translate(Vector2.left * szybkosc);
}
}
}
EDIT:
I'm making control system. I have two arrows - left arrow and right
arrow. When I touch and hold left arrow, object moves left, when I
touch and hold right arrow, object moves right.
It's good to mention what you are doing in your question. This is what EventSystems is used for. OnPointerClick, OnDrag and OnEndDrag are used to make such things. You can find more about this here.
In fact, you need a Visual Joystick. Use Unity's CrossPlatformInputManager from the Asset store and that should do it. It will save you so much time of having to make your own with OnPointerClick and OnDrag .
The value from CrossPlatformInputManager.GetAxisRaw("Horizontal") can then be used to move your Object left or right.

How to allow an object to move within a collider?

I am trying to have a block that can only be pushed inside of a rectangular collider. The player has to be able to move through the collider in order to push the block. I have tried to affect the mass upon the block being pushed into the edge collider, the drag, the angular drag, the velocity, isKinematic but nothing will stop the cube from moving when it hits the collider. It is really confusing, any help would really be appreciated...Here is the code:
public class pushBlock2 : MonoBehaviour {
public Rigidbody2D pBlock2;
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "edge") {
Debug.Log ("pushblock2 touched edge");
pBlock2.isKinematic = true;
pBlock2.isKinematic = false;
}
}
void OnTriggerStay2D(Collider2D col)
{
if (col.tag == "edge") {
pBlock2.isKinematic = true;
}
}
Why don't you try and create four colliders around the area you want to move your block within? This way you'll have to stop the block from entering those which is much easier task than trying to prevent an item from leaving the collider.
I am pretty sure you will be able to pull this off without any code at all, you just need to setup a couple of static colliders to collide with objects on certain layer and put the cube you wanna move on that layer.
More info on Layer-based collisions can be found here: http://docs.unity3d.com/Manual/LayerBasedCollision.html
The way I understood, you're trying to use the volume defined by a collider as a delimiter for movement. I'm supposing you also desire "realistic" collisions when the object attempts to go outside said volume. That being the case, I'm afraid it is not possible.
The whole physics simulation is made by assuming some rules. Whenever two colliders intersect, the physics follows the rule that two objects should not occupy the same space (just like the real world), and thus it will try to figure out how to separate them again.
You can somewhat work around this by using a set of colliders that defines an hollow object, such as a barrel or box, but its more likely you'll turn off physics simulation for the box and roll up your own algorithm if consistent behavior is needed.
Many things in games are actually "fake" behaviors. Perhaps you can achieve what you want by faking physics or faking collisions?

Categories

Resources