0

ステージを回転させてボールをゴールに入れるゲームを作っているところですが、
ステージを回転させるとスムーズに動く黒い障害物が(恐らくVector3のせいで) 沈んだり浮いたりします。
これに対する解決策、つまりステージと障害物の間の距離を固定する方法
(ステージを回転させても障害物が浮いたり沈んだりしない)が知りたいです。

障害物の移動

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

public class RepeatMovementOfShark : MonoBehaviour
   {
        public float zSpeed = -1.0f; //units per second, set negative to go the other way
        public float zDistance = 10.0f; //distance to travel before turning back
        public bool FreezeY = false;

        private Vector3 startPos;

        float way = 1.0f;
        float zPos = 0.0f;
        float zPosPrev = 0.0f;
        void Start()
        {
            //save start position
            startPos = transform.position;
        }
        void Update()
        {
            Vector3 currentPos = transform.position;
            //advance position
            zPosPrev = zPos;
            zPos += way * zSpeed * Time.deltaTime;
            //handle turn around at end
            if (Mathf.Abs(zPos) > zDistance)
            {  
                way = -way;
                zPos = zPosPrev;
            }
            //handle turn around at start
            if ((zPos < 0.0f && zPosPrev > 0.0f) || (zPosPrev < 0.0f && zPos > 0.0f))
            {
                way = -way;
                zPos = zPosPrev;
            }
            if (FreezeY)
            {
                currentPos.y = startPos.y;
            }
            //set new position
            transform.position = new Vector3(transform.position.x, transform.position.y, startPos.z + zPos);
        }
    }

ステージの回転

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

// Transform.rotation example.

// Rotate a GameObject using a Quaternion.
// Tilt the cube using the arrow keys. When the arrow keys are released
// the cube will be rotated back to the center using Slerp.

public class StageRotation : MonoBehaviour
{
    public float smooth = 5.0f;
    public float tiltAngle = 60.0f;

    void Update()
    {
        // Smoothly tilts a transform towards a target rotation.
        float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle * -1;
        float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle * -1;

        // Rotate the cube by converting the angles into a quaternion.
        Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);

        // Dampen towards the target rotation
        transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
    }
}

Attached Files:

1 件の回答 1

0

ヒエラルキーで、ステージの親と障害物の親が同じになってませんか?
障害物の親をステージにすることで障害物の座標を更新しなくてもステージの回転に追従するようになります。

5
  • 返信ありがとうございます。うっかりいうのを忘れていましたが既にステージと障害物の親は同じにしています 2020年10月9日 6:01
  • いえ、同じ親ではまずいです。障害物の親がステージになるような親子関係にして下さい。
    – nee
    2020年10月9日 7:22
  • 障害物の親をステージにしてもまだ沈んだり浮いたりします 2020年10月10日 8:40
  • 障害物のコードがないので推測ですが、おそらくボールのようにUpdate()で毎フレーム座標計算してるのではないですか?子は親の座標系に存在する、すなわち自動で親に追従するので毎フレームの座標更新は不要です。
    – nee
    2020年10月10日 12:02
  • はい、確認しましたところ障害物の移動はUpdateの中で行っていました 2020年10月11日 2:22

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。