[英]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.