简体   繁体   中英

Why the player is falling down after moving it's position?

I created a glass with reflection. The idea is when the player is getting close to a window to see the player reflection like the player is looking on the window.

The glass reflection is working and it's disabled in the Hierarchy. I'm enabling the glass reflection later in the game.

This is a screenshot of the glass when it's turned on : I positioned the glass to on the window :

玻璃

This is the player screenshot. The player have a child name NAVI. The NAVI is positioned a bit in front of the player : The player have a Rigidbody. In the screenshot on the left the NAVI object and in the right the player Inspector settings :

播放机

And this script is attached to the window not to the glass but to the window :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemAction : MonoBehaviour
{
    public RotateCamera rotatecamera;
    public GameObject glass;
    public GameObject player;

    public void Init()
    {
        player.transform.localPosition = new Vector3(transform.localPosition.x - 1, transform.localPosition.y, transform.localPosition.z);
        glass.SetActive(true);
    }
}

This is a screenshot of the window with the script attached to it :

窗口

The idea is when some where in the game when it's calling the method Init it will position the player as much as close to the window to see the player reflection on the glass.

But instead the player is falling down through the floor and keep falling nonstop.

You're using the window's local position to set the player's local position. Instead, use position and only change the player's x component:

float paddingDistance = 1f;  // Change this to adjust the padding

player.transform.position = new Vector3(transform.position.x - paddingDistance, 
                                        player.transform.position.y, 
                                        player.transform.position.z);

The problem with this is that it only works for glass rotated in exactly such a way. If you want to make this work for any such glass orientation, you can make a plane parallel to the glass but separated some distance and move the player to the closest point on the plane. Assuming the glass is normal to its transform.left direction:

float paddingDistance = 1f;  // Change this to adjust the padding
Vector3 paddingPoint = transform.position + transform.left * paddingDistance; 
float paddingToPlayerAlongLeft = Vector3.Dot(transform.left,
                                                 player.transform.position - paddingPoint);

// closest point to player.transform.position on padding plane
player.transform.position = player.transform.position 
                            - paddingToPlayerAlongLeft * transform.left;

But even this only works for one side of the glass. If you wanted to make this work for either side of the glass, you need to determine if the player's on the left side or not (we can do this using Vector3.Dot ), then make the plane in the corresponding direction:

float paddingDistance = 1f;// Change this to adjust the padding

float glassToPlayerAlongLeft = Vector3.Dot(player.transform.position - transform.position, 
                                         transform.left);

float paddingLeftMod = glassToPlayerAlongLeft > 0f ? 1f : -1f;

float paddingToPlayerAlongLeft = glassToPlayerAlongLeft - paddingDistance * paddingLeftMod;

// closest point to player.transform.position on padding plane
player.transform.position = player.transform.position 
                            - paddingToPlayerAlongLeft * transform.left;

However, even this only works if the glass is perfectly vertical. If the glass is not always vertical, then you could find the line intersection of the horizontal plane at the player's height and the padding plane and then set the player's position to the closest point on that line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM