使立方体遵循统一后面的五个步骤

时间:2013-07-03 13:33:30

标签: unity3d

目前我有一个脚本,当你击中一个立方体时,它会跟随玩家......但是当你站着不动时,它会重叠你。我想要的是能够在任何时候将立方体的位置设置在玩家后面五步......我该怎么做?

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;


    }
    //}


    }

1 个答案:

答案 0 :(得分: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 /游戏编程