0
  • 我的场景中有一个名为“玩家”的对象。
  • 我还有多个名为“”的对象。

现在,我希望每当用户单击“树”时,“玩家”缓慢移动到该位置(使用 Lerp 或 moveTowards)对我来说都可以。


现在我对这段代码有两个问题:

我希望这段代码是通用的

每当我单击树对象时,我都希望将播放器移向该。我不想编写此脚本并将其附加到每个树对象

  • 我应该把脚本放在哪里?

    目前我将此代码附加到每个树对象

  • 我应该如何写下来,以便它适用 于场景中的每个树对象?

如果在移动时再次单击,则取消之前的移动并开始移动到新位置

  • 我应该如何编写它,以便如果我在 玩家向另一个被点击的对象移动时单击另一个对象,玩家将停止向其先前位置移动,并开始向新点移动。

我在适应新的 UnityScript 时遇到了一些麻烦。我完全来自 Javascript 背景,看起来它们中的 2 种语言具有非常不同的语义。因此,如果有人用代码回答这个问题(这是我想要的:)),我也会很感激一些冗长的评论。


我目前这样做:

var playerIsMoving = false;
Public playerObject: Gameobject; //I drag in the editor the player in this public var

function update(){  

   var thisTreePosition = transform.point; //this store the X pos of the tree
   var playerPosition = player.transform.point;

   if(playerIsMoving){
    player.transform.position = Vector2.MoveTowards(playerPosition, thisTreePosition, step);
   }

}

function OnMouseDown(){
    playerIsMoving = true;
}

我是在没有 Unity 的家里写的,我忘记了代码语法,因此希望上面的代码有拼写错误或问题,在工作中它工作得很好,除了非常粗糙和简单

4

2 回答 2

3

我建议您将移动脚本放在播放器上。以及如何使用 Raycast 测试您是否击中树?

http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

Vector3 positionToWalkTo = new Vector3();

void Update() {
        if (Input.GetMouseButtonDown(0)) {
            RaycastHit hit;
            Ray ray = new Ray(transform.position, direction);

            if (Physics.Raycast(ray, out hit))
               if (hit.gameObject.tag.Equals("tree")){
                  positionToWalkTo = hit.gameObject.transform.position;
               }

        }

        float step = speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, positionToWalkTo, step);
    }

要运行这样的东西,你应该标记所有的树。

'JavaScript'

var target: Transform;
// Speed in units per sec.
var speed: float;

function Update () {

        if (Input.GetMouseButtonDown(0)) {
             var hit: RaycastHit;
             var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

        // Raycasting is like shooting something into the given direction (ray) and hit is the object which got hit
        if (Physics.Raycast(ray, hit)) {
            if (hit.gameObject.tag = "tree")
                target = hit.gameObject.transform.position;  // Sets the new target position
        }
    }

    // The step size is equal to speed times frame time.
    var step = speed * Time.deltaTime;

    // Move our position a step closer to the target.
    transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
于 2015-01-18T17:14:58.133 回答
1

好的,所以我自己完成了(在 Freshchris 的回答的帮助下)

这里是:

var target:Vector2;
var targetObject:GameObject;
var initialY:float;
var tolerance = 1;
var speed = 3;
var isMoving = false;

function Start(){
    target = transform.position; 
}

function Update () {

    if (Input.GetMouseButtonDown(0)) {
        var hit: RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); //raycast the scene

        // if we hit something
        if (hit.collider != null) {
            isMoving=true; //it should start moving
            targetObject = hit.collider.gameObject; //the target object 
            target = hit.collider.gameObject.transform.position;
        }
    }

    // The step size is equal to speed times frame time.
    var step = speed * Time.deltaTime;

    // if it should be moving - move it - and face the player to the correct direction
    if (isMoving){
        if(transform.position.x>target.x){
            gameObject.transform.localScale.x =  -0.7;
        }else {
            gameObject.transform.localScale.x =  0.7;
        }
        transform.position = Vector2.MoveTowards(transform.position, target, step);
    }

    // if it reached the target object by a specified tolerance, tell it to stop moving
    if(isMoving){
        if((transform.position.x < target.x+tolerance)&&(transform.position.x > target.x-tolerance)){
            print("position reached"); //this is fired just once since isMoving is switched to false just one line below
            print(targetObject);
            isMoving=false;
        }
    }
}
于 2015-01-18T22:54:25.820 回答