团结精灵运动改变目标

时间:2017-01-07 16:10:11

标签: c# unity3d

我在游戏中有这些精灵;

  • 播放器
  • 圈(1)
  • 圈(2)
  • 圈(3)
  • 圈(4)
  • 圈(5)
  • 圈(6)

圆圈间隔开。当使用下面的代码按下空格键时,我设法让玩家从圆圈(1)移动到圆圈(2)。

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

public class movement : MonoBehaviour
{

    private Vector3 player;
    public GameObject position1;
    public float speed = 30;

    void Update()

    {
    if (Input.GetKeyDown("space"))
        player = position1.transform.position;
        player.z = transform.position.z;
        transform.position = Vector3.MoveTowards(transform.position, player, speed * Time.deltaTime);
    }
}

我需要玩家在每次按下空格键时逐个“敲击”,所以从圆圈(1)到圆圈(2)到圆圈(3)再到圆圈( 4)依此类推......

我还需要一种超越圆(6)的方法,并在理论上继续无限。

我在C#编码并使用Unity 5.5

提前谢谢

3 个答案:

答案 0 :(得分:0)

这是一个代码将正常工作。首先,你需要在你的玩家中拥有一个Rigidbody。 ,而不是添加此代码:

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

public class movement : MonoBehaviour
{

    public GameObject position1;
    public GameObject position2;
    public GameObject position3;
    public GameObject position4;
    public GameObject position5;
    public GameObject position6;
    public int PressedTime;
    public float speed = 30;

void Update()
{
    if (Input.GetKeyDown("space"))
    {
        PressedTime += 1;
        if (PressedTime == 1)
        {
            transform.position = Vector3.MoveTowards(transform.position, position1.transform.position, speed);
        }
        if (PressedTime == 2)
        {
            transform.position = Vector3.MoveTowards(transform.position, position2.transform.position, speed);
        }
        if (PressedTime == 3)
        {
            transform.position = Vector3.MoveTowards(transform.position, position3.transform.position, speed);
        }
        if (PressedTime == 4)
        {
            transform.position = Vector3.MoveTowards(transform.position, position4.transform.position, speed);
        }
        if (PressedTime == 5)
        {
            transform.position = Vector3.MoveTowards(transform.position, position5.transform.position, speed);
        }
        if (PressedTime == 6)
        {
            GetComponent<Rigidbody>().AddForce(position6.transform.position * speed);
        }

    }       
}
}

并在检查器中将变量position1设置为6

答案 1 :(得分:0)

如果您的目标是让玩家朝着目标移动,并且每次按下空格键时让目标都改变,那么您需要做一些简单的事情。

public class Movement : MonoBehaviour 
{
    // You'll need a list of available targets
    public List<Transform> Targets;

    // The speed to move at
    public float Speed = 30;

    // A variable to keep track of the current target
    private int _currentTargetIndex = 0;

    public void Update()
    {
        // Get the current target
        var target = Targets[_currentTargetIndex];

        // Move towards it every frame
        transform.position = Vector3.MoveTowards(transform.position, target.position, Speed);

        // If the space bar was pressed this frame
        if(Input.GetKeyDown("space"))
        {
            // Select the next target
            _currentTargetIndex++;

            // If we've run out of targets, wrap around to the beginning 
            if(_currentTargetIndex >= Targets.Count) 
            {
                _currentTargetIndex = 0;
            }
        } 
    }
}

答案 2 :(得分:-1)

if (Input.GetKeyDown("space"))
{
        transform.position = Vector3.MoveTowards(transform.position, position1.transform.position, speed);
}  

对于其他圆圈,您可以设置变量position2 3 4 5,依此类推。 并制作'int PressedTime',当PressedTime为6时,关闭上面的代码并将你的游戏对象添加到第6圈。

如果我的代码有效,请不要忘记接受我的回答! :)