Unity C# - 如何检查随机移动的游戏对象是否在边界内?

问题描述 投票:1回答:1

嗨,我有一个随机移动的gameobject在场景中移动,并附上代码。问题是,虽然有一个刚性体的边界碰撞器,有时它通过他们的权利和下降。

如何限制它一旦碰到这些碰撞物就会做189次旋转,继续随机移动?

地形宽度是50f,地形高度也是50f。

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

public class PlayerTrainingMovement : MonoBehaviour {

    public float speed;

    private float randomizedSpeed = 0.0f;
    private float nextActionTime = -1.0f;
    private Vector3 targetPosition;
    private Vector3 moveVector;
    private void FixedUpdate() {

        if(speed > 0.0f) {
            Move2(); 
            //Move();
        }
    }

    private void Move2() {

        if(Time.fixedTime >= nextActionTime) {

            // Get a threshold time
            nextActionTime = UnityEngine.Random.Range(50.0f, 200.0f);

            // Pick a random target position
            targetPosition = TrainingArea.ChooseRandomPosition(transform.parent.position, 0f, 270f, 2f, 10f);


            // Rotate towards the target position
            transform.rotation = Quaternion.LookRotation(targetPosition - transform.position, Vector3.up);

            float timeToGetThere = Vector3.Distance(transform.position, targetPosition) / randomizedSpeed;
            nextActionTime = Time.fixedTime + timeToGetThere;


            // Get a movement vector
            moveVector = speed * transform.forward;
            //moveVector = speed * new Vector3(1.0f, 0.0f, 0.0f);
        }
        else {

            // Check -X boundary
            if(transform.position.x - 1.0f <= transform.parent.gameObject.transform.position.x) {

                transform.position = transform.position + new Vector3(1.0f, 0.0f, 0.0f);
                moveVector.x *= -1.0f;
            }

            // Check +X boundary
            if(transform.position.x + 1.0f >= transform.parent.gameObject.transform.position.x + TrainingArea.terrainWidth) {

                transform.position = transform.position + new Vector3(-1.0f, 0.0f, 0.0f);
                moveVector.x *= -1.0f;
            }

            // Check -Z boundary
            if(transform.position.z - 1.0f <= transform.parent.gameObject.transform.position.z) {

                transform.position = transform.position + new Vector3(0.0f, 0.0f, 1.0f);
                moveVector.z *= -1.0f;
            }

            // Check +Z boundary
            if(transform.position.z + 1.0f >= transform.parent.gameObject.transform.position.z + TrainingArea.terrainHeight) {

                transform.position = transform.position + new Vector3(0.0f, 0.0f, -1.0f);
                moveVector.z *= -1.0f;
            }

            // Move 
            transform.position = transform.position + moveVector * Time.fixedDeltaTime;

            // Update nextActionTime
            nextActionTime--;
        }
    }
}
public static Vector3 RandomPositionOnTerrain(Vector3 origin) {
    //Range(30.0f, terrainWidth - 30.0f);
    float randomX = origin.x + UnityEngine.Random.Range(20.0f, terrainWidth);
    float randomZ = origin.z + UnityEngine.Random.Range(20.0f, terrainHeight);

    return new Vector3(randomX, 30.0f, randomZ);
}
c# unity3d unityscript
1个回答
1
投票

您可以使用 OnCollisionEnter 当单元撞到其他碰撞器时,触发旋转。

这样的东西一定能做到。

void OnCollisionEnter(Collision col)
   {
       if(col.gameObject.tag(or name) = "a tag or name"){
          //Do Stuff here
       }
   }

这里有详细的文档https: /docs.unity3d.comScriptReferenceCollider.OnCollisionEnter.html.

另一个变通办法是使用 NavMesh. 它将使运动 UnityEngine.AI 的基础上,并且相当流畅,但你需要一个全新的脚本。

© www.soinside.com 2019 - 2024. All rights reserved.