简体   繁体   English

你如何获得玩家游戏 object 旋转与球体游戏 object

[英]How do you get the player game object from rotating with the sphere game object

I am currently making a Katamari/Billy Hatcher game where the player has to roll spheres around.我目前正在制作一个 Katamari/Billy Hatcher 游戏,玩家必须在其中滚动球体。 When the game starts the player has normal platformer controls until it approaches a sphere and if the player presses the "attach" button, the player becomes a child of the sphere.当游戏开始时,玩家拥有正常的平台游戏控制,直到它接近一个球体,如果玩家按下“附加”按钮,玩家将成为该球体的子级。 The issue I am having is whenever this happens, the player rotates with the sphere.我遇到的问题是每当发生这种情况时,玩家都会随着球体旋转。 I tried freezing the player's rigid body so it can stop rotating but that just stops the sphere's rotation.我尝试冻结玩家的刚体,使其停止旋转,但这只会停止球体的旋转。 Is there any way to stop the rotation of the player, while keeping the sphere rotating?有什么方法可以在保持球体旋转的同时停止播放器的旋转?

Picture: enter image description here Here's my scripts for the process:图片:在此处输入图片描述这是我的流程脚本:

Rigidbody hitRB;
public Vector3 offset = new Vector3(0, 0, 1);
public LayerMask pickupMask;
bool isAttached;


private void TouchingGum()
{
    RaycastHit hit = new RaycastHit();
    foreach (GameObject gumball in gumBalls)
    {
        
        if (Input.GetButtonDown("Attach") && Physics.Raycast(transform.position,transform.forward, out hit, attachRequireDistance, pickupMask))
        {
            isAttached = true;
            Debug.Log(true);
        }
        else
        {
            isAttached = false;
        }
    }


    if (isAttached)
    {
        hitRB = hit.collider.gameObject.GetComponent<Rigidbody>();
        Vector3 heldOffset = transform.right * offset.x + transform.up * offset.y + transform.forward * offset.z;
        hitRB.isKinematic = true;
        hitRB.MovePosition(player.transform.position + heldOffset);
    }
    else if(!isAttached && !hitRB == null)
    {
        hitRB.isKinematic = false;
    }

If you can, don't use parent/child relationships in these situations.如果可以,请不要在这些情况下使用父/子关系。 I feel that there is always a better way.我觉得总有更好的方法。 One way of accomplishing this is by taking the player's transform, and adding the forward direction to an offset:实现这一点的一种方法是获取玩家的变换,并将正向添加到偏移量:

Vector3 offset = new Vector3(0, 0, 1);
LayerMask pickupMask; //change to mask of objects that can be picked up in editor.
public bool held;

void Update()
{
   RaycastHit hit;
   if (Input.GetKey(KeyCode.Mouse0) && Physics.Raycast(transform.position, transform.forward, out hit, 2, pickupMask))
   {
       held = true;
   }
   else
   {
       held = false
   }
   Rigidbody hitRB = hit.collider.gameObject.GetComponent<Rigidbody>();
   if (held)
   {
       Vector3 heldOffset = (transform.right * offset.x) + (transform.up * offset.y) + (transform.forward * offset.z);
       // if it still glitches, remove the previous line and add the line after this one.
       Vector3 heldOffset = transform.forward * offset.z;
       hitRB.isKinematic = true;
       hit.collider.gameObject.transform.position = transform.position + heldOffset;
   }
   if else (!held && !hitRB == null)
   {
      hitRB.isKinematic = false;
   }
}

This script uses raycast and input to detect if the player is clicking the left mouse button and is looking at an object within 2 distance and with a certain layer mask.此脚本使用光线投射和输入来检测玩家是否单击鼠标左键并在 2 距离内查看 object 并带有特定图层蒙版。 Then it will set the velocity to the offset plus the player's position.然后它将速度设置为偏移量加上玩家的 position。 In other words, this gets the object you looked at while pressing left click, and holds it in front of you (or whatever the offset is).换句话说,这将获得您在按下左键时查看的 object,并将其保持在您面前(或任何偏移量)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从玩家那里检测游戏对象是左侧还是右侧 - How to detect if the game object is left or right from the player 如何计算蛇游戏中玩家身后的游戏角度object - How to calculate the angle of a game object behind the player in a game of snake 你如何将一个游戏 object 与另一个游戏 object 推送一个设定的距离,例如 1 个瓷砖 - How do you push a game object with another game object a set distance e.g. by 1 tile 我如何使游戏对象跟随另一个游戏对象但不移动到目标对象中? - How do i get the game object to follow another game object but not move into the targeted object? Unity-如何将动画从一个游戏对象应用于另一个装备的游戏对象? - Unity - How do I apply an animation from one game object to another rigged game object? 我正在努力让游戏 object 在与玩家碰撞时被摧毁 - I am struggling to get a game object to be destroyed when colliding with the player 如何让游戏对象将更改应用于脚本中的预制件 - how do i get a game object to apply changes to a prefab in script 我如何让光线投射破坏游戏 object - how do i get the raycast to destroy the game object 当玩家与触发器发生碰撞时,如何启用游戏 object 的组件? - How do I enable a component of a game object when the player collides with a trigger? 在 Unity 中与游戏对象发生碰撞时,如何让我的玩家加速? - How do I make my player speed up when collide with a game object in Unity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM