Parent child position

Hello I have a little problem with position of player,

On jumping from static platform to rotating platform cube becomes child of rotating platform when it sticks to that platform. Problem is on collision exit because cube resets back its X position to where it was before jumping on rotating platform. How can I fix this?

public class RotacijaObjekta : MonoBehaviour {
   
        public Vector3 Pivot;
        public bool DebugInfo = true;
        public GameObject rotateScale;
        public GameObject player;

        //it could be a Vector3 but its more user friendly
        public bool RotateX = false;
        public bool RotateY = true;
        public bool RotateZ = false;

        void Start(){
            rotateScale = GameObject.Find ("FixScaleRotate");
            player = GameObject.Find ("Player");
        }

        void Update () {
            transform.localScale = transform.localScale;   
        }

        void FixedUpdate()
        {
            transform.position += (transform.rotation*Pivot);

            if (RotateX)
                transform.rotation *= Quaternion.AngleAxis(45*Time.deltaTime, Vector3.right);
            if (RotateY)
                transform.rotation *= Quaternion.AngleAxis(45*Time.deltaTime, Vector3.up);
            if (RotateZ)
                transform.rotation *= Quaternion.AngleAxis(45*Time.deltaTime, Vector3.forward);

            transform.position -= (transform.rotation*Pivot);

            if (DebugInfo)
            {
                Debug.DrawRay(transform.position,transform.rotation*Vector3.up,Color.black);
                Debug.DrawRay(transform.position,transform.rotation*Vector3.right,Color.black);
                Debug.DrawRay(transform.position,transform.rotation*Vector3.forward,Color.black); 

                Debug.DrawRay(transform.position+   (transform.rotation*Pivot),transform.rotation*Vector3.up,Color.green);
                Debug.DrawRay(transform.position+(transform.rotation*Pivot),transform.rotation*Vector3.right,Color.red);
                Debug.DrawRay(transform.position+(transform.rotation*Pivot),transform.rotation*Vector3.forward,Color.blue);
            }
        }

    void OnTriggerEnter(Collider other){


        if (other.tag == "Player") {
            //gameObject.transform.parent = null;
            //player.transform.localScale = new Vector3 (1f, 1f, 1f);
            player.transform.parent = rotateScale.transform;
        }
    }


    void OnTriggerExit(Collider other){
        if (other.tag == "Player") {
            other.transform.parent = null;
        }
    }

    }

The normal X

X when landing on platform, becoming child of rotating platform

X when jumping from the platform is where the problem is

It’s tough to see what’s going on exactly from a series of screenshots - would it be possible to upload a video of the problem?

A good thing to try might be to use other.transform.SetParent(whatever, true) instead of assigning it via .parent = … SetParent gives you a little more control over the process.

Get rid of GameObject.Find. This isn’t your main problem, but it’ll become an issue when you, for example, try to have multiple rotating platforms (GO.F is literally never the best way to get a reference to the object.)

1 Like

When an object becomes a child of something, the transform displays localposition based on its parent. So x will change to reflect that. But as @StarManta said, I’m not really understanding what your question is.

1 Like

not sure if it will help you but maybe try with

other.transform.SetParent(null, true);

true for worldPositionStays, see Unity - Scripting API: Transform.SetParent

1 Like

Thank you all for fast response. So I made video of problem and put it on youtube.

Here is the link:
https://www.youtube.com/watch?v=GrzzfYR8MaQ

You can see how rotation is changed once the box collides with rotation platform and go crazy when box jumps out of that platform. :stuck_out_tongue:

Tnx in advance! <3

How can I implement this into my script. I am new to unity and scripting. What is null for?

In SetParent, the first parameter (in that example, “null”) is the same as the right-hand side of the “transform.position =” line in your original code, and it simply replaces that line.

The video helps demonstrate the issue a lot. My gut instinct for this issue is that the problem is more likely on your cube’s movement script, than on this script. Could you post that code?

1 Like

I just found that my mouse movement script (that I got somewhere from internet) when disabled issue with position X stops. I will try to create something more simple but here you go that mouse look script. I don’t understand it but maybe you can.

using UnityEngine;
using System.Collections;

// Very simple smooth mouselook modifier for the MainCamera in Unity
// by Francis R. Griffiths-Keam - www.runningdimensions.com

[AddComponentMenu("Camera/Simple Smooth Mouse Look ")]
public class MouseLook : MonoBehaviour
{
    Vector2 _mouseAbsolute;
    Vector2 _smoothMouse;

    public Vector2 clampInDegrees = new Vector2(360, 180);
    public bool lockCursor;
    public Vector2 sensitivity = new Vector2(2, 2);
    public Vector2 smoothing = new Vector2(3, 3);
    public Vector2 targetDirection;
    public Vector2 targetCharacterDirection;

    // Assign this if there's a parent object controlling motion, such as a Character Controller.
    // Yaw rotation will affect this object instead of the camera if set.
    public GameObject characterBody;

    void Start()
    {
        // Set target direction to the camera's initial orientation.
        targetDirection = transform.localRotation.eulerAngles;

        // Set target direction for the character body to its inital state.
        if (characterBody) targetCharacterDirection = characterBody.transform.localRotation.eulerAngles;
    }

    void Update()
    {
        // Ensure the cursor is always locked when set
        Screen.lockCursor = lockCursor;

        // Allow the script to clamp based on a desired target value.
        var targetOrientation = Quaternion.Euler(targetDirection);
        var targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);

        // Get raw mouse input for a cleaner reading on more sensitive mice.
        var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));

        // Scale input against the sensitivity setting and multiply that against the smoothing value.
        mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));

        // Interpolate mouse movement over time to apply smoothing delta.
        _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
        _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);

        // Find the absolute mouse movement value from point zero.
        _mouseAbsolute += _smoothMouse;

        // Clamp and apply the local x value first, so as not to be affected by world transforms.
        if (clampInDegrees.x < 360)
            _mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);

        var xRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right);
        transform.localRotation = xRotation;

        // Then clamp and apply the global y value.
        if (clampInDegrees.y < 360)
            _mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);

        transform.localRotation *= targetOrientation;

        // If there's a character body that acts as a parent to the camera
        if (characterBody)
        {
            var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, characterBody.transform.up);
            characterBody.transform.localRotation = yRotation;
            characterBody.transform.localRotation *= targetCharacterOrientation;
        }
        else
        {
            var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
            transform.localRotation *= yRotation;
        }
    }
}

I think I’ve seen this script before on the forums, and it’s awful, especially in rotation. Do you know where you actually got it? If possible, I’d like to try and stop that cancer of a script file from being spread around further.

I also like that you didn’t write it because it means I get to, like, really rip into it. :wink:

The biggest issue with the script is that it uses Euler angles. Euler angles on their own are bad and should never be used, but this one is especially bad because it tries to use them with a Vector2. (see line 28) It’s like they went out of their way to find and e

So yeah, I recommend rewriting that script with something simpler, for sure. In the short term, I think you can probably solve your immediate problem if, in that movement script, you replace all ".localRotation"s with “.rotation”. This will make it always use the world-space rotation, even when the cube’s parentage changes (which will change the local rotation reference frame).

1 Like

I found it here: https://forum.unity3d.com/threads/a-free-simple-smooth-mouselook.73117/

Tnx so much for help. I am gonna for sure make something simpler and more usable than that script. Have a nice day!