0

目前我有一个脚本,当你击中一个立方体时,它会跟随玩家……但当你静止不动时,它会与你重叠。我想要的是能够始终将立方体的位置设置为落后玩家五步……我该怎么做?

GameObject.Find("Cube2").transform.position = Vector3(0.5, 0.5, 0.5);

这就是我到目前为止所尝试的,但这只是让立方体消失了吗?

整个脚本:

       static var target : Transform; //the enemy's target
    var moveSpeed = 3; //move speed
    var rotationSpeed = 3; //speed of turning    
    var Player = GameObject.Find("Player").transform.position;
    var Cube2 = GameObject.Find("Cube2").transform.position;

    var myTransform : Transform; //current transform data of this enemy

    function Awake()
    {
    //myTransform = transform; //cache transform data for easy access/preformance
    }

    function Start()
    {
    //target = GameObject.FindWithTag("Player1").transform; //target the player

    }    
    //var distance = Vector3.Distance(Player.transform.position, Cube2.transform.position);
    //Debug.Log(distance);

    function Update () {



    Debug.Log(Player);

    //var distance = Vector3.Distance(Player.transform.position, Cube2.transform.position);
    //var distance = Vector3.Distance(player_distance, cube_distance);

//    if (distance > 5)
   // {

    if (target == GameObject.FindWithTag("Player").transform)
    {
    //rotate to look at the player
    GameObject.Find("Cube2").transform.position = Vector3(0.5, 0.5, 0.5);
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

    //move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;


    }
    //}


    }
4

1 回答 1

1

就像我说的,我真的不知道团结(玩了 5 分钟)

老实说,您似乎已经掌握了它 - 不知道为什么您不能让它工作:

这是应该起作用的:(假设四元数调用是正确的)-这是使用该站点的最新 Unity 参考,因此可能与适合您的方法不同(您使用的是哪个版本的 Unity?)

// Params
var moveSpeed = 3; // Move speed
var rotationSpeed = 3; // Speed of turning    

// Find game objects
var Player = GameObject.Find("Player");
var Cube2 = GameObject.Find("Cube2");

function Update () 
{
    // Vector from cube pos to player pos (vector math: target - position = vector to target from pos)
    var dir = Player.transform.position - Cube2.transform.position;

    // If the distance is over 5 units
    if(dir.magnitude > 5.0f)
    {
        // Rotate towards player
        Cube2.transform.rotation = Quaternion.Slerp(Cube2.transform.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime);

        // Move forward at specified speed
        Cube2.transform.position += Cube2.transform.forward * moveSpeed * Time.deltaTime;
    }
}

应该这样做 - 如果不让我知道会发生什么(或者如果你遇到编译错误) - 就像我说我不太了解 Unity 但我看过并且我熟悉 3D/游戏编程

于 2013-07-03T13:51:51.933 回答