- Home /
My player changes it original scale when on a moving platform..please help.THanks
hello guys, so i am working on a school project on unity and i am having a problem with my player...i have lot of moving platform on my project and i need my player to be to attached on the moving platform but the problem is that whenever my player is on the platform my player changes it orginal scale to some random scale..please i need help..i have read some answers related to to my question but up till now non of it works ...please i need help fast...THANKS
Answer by QPUNeptune · Mar 07, 2020 at 06:39 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AttachPlay : MonoBehaviour
{
public GameObject Player;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject == Player)
{
Debug.Log("touch");
Player.transform.parent = transform;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject == Player)
{
Player.transform.parent = null;
Player.transform.localScale = DefaultScale.defaultScale;
}
}
}
original code from @ultimate1999
The solution:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AttachPlay : MonoBehaviour
{
public GameObject Player;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject == Player)
{
Debug.Log("touch");
Player.transform.parent.parent = transform;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject == Player)
{
Player.transform.parent.parent = null;
Player.transform.localScale = DefaultScale.defaultScale;
}
}
}
On Player.transform.parent
you need to put another .parent
, so the code look like Player.transform.parent.parent
Answer by Magso · Feb 24, 2020 at 10:08 PM
The parent's scale influences the children as well. When it is parented, divide the player's scale by the platform's scale.
For example, if player is scaled (1,1,1)
and the platform is (0.7, 0.2, 0.7)
the player will need to be scaled to (1.4286, 5, 1.4286)
to keep it's correct size when parented. Of course when it is unparented, revert the scale back.
Hello $$anonymous$$agso,Thanks for your respond..but i have a little problem ...the problem i have is that i have a big platform i divide the scale of my player with the platform ,it results to a very small player which i cant use because i need my player big.... PLEASE CAN YOU HELP $$anonymous$$E WITH ANOTTHER SOLUTION BECAUSE I COULDNT TRY THIS ONE BECAUSE IT DOESNT FIT $$anonymous$$Y WOR$$anonymous$$...PLEASE I WOULD REALLY APPRECIATE IT ..THIS PROBLE$$anonymous$$ HAS BEING GIVING HEADACHE AND PISSING $$anonymous$$E OFF..PLEASE HELP $$anonymous$$E
It might be easier if you have one gameObject called PlayerPlatformGroup which has two children, the Player and the Platform. $$anonymous$$ove the entire group but only scale the Platform. You may run into issues if the platform shrinks and the player is no longer on it, but it may be easier to handle than keeping track of the sizes.
The other solution is to scale all physics related GameObjects with colliders, rigidbodies, etc to (1,1,1) and have meshes and all visuals as children.
Another solution is to use a raycast going straight down and OnCollisionEnter. If OnCollisionEnter collision.gameObject and hit.collider.gameObject return the same, get the position and rotation offset when the player lands and follow that GameObject.
GameObject platform;
Vector3[] offset;
void Update()
{
if(platform)
{
transform.position = platform.position + offset[0];
transform.eulerAngles = platform.eulerAngles + offset[1];
}
}
void OnCollisionEnter(Collision collision)
{
if (Physics.Raycast(transform.position, Vector3.down, out hit, (size of player / 2)))
{
if(hit.collider.gameObject == collision.gameObject)
{
platform = collision.gameObject;
offset[0] = transform.position - collision.transform.position;
offset[1] = transform.eulerAngles - collision.transform.eulerAngles;
}
}
}
HELLO magso..thank you very much for your answer..i really appreciate...i will try it and see if it works..but on question ..where would i add this code?..is it with the platform or the player...thank you once again