【问题标题】:How to have player "slide" on pipes and rails?如何让玩家在管道和铁轨上“滑动”?
【发布时间】:2014-06-17 02:18:49
【问题描述】:

我需要创建打滑或滑倒效果。我想像日落过载一样创建打滑或滑倒效果:

http://i.stack.imgur.com/vmx8f.jpg

当主角在电缆和铁轨上滑倒时。如何创建相同的结果?在 C# 中。观看此视频:https://www.youtube.com/watch?v=zXjNkoQQPls(从 1.20 分钟到 1.25)。我已经创建了这个代码:-OnTrigger-(适用于电缆)

使用 UnityEngine;使用 System.Collections;公共课 OnTriggerEnterScroll : MonoBehaviour {

/*ISTRUZIONI : Inserire questo Script all'interno dell'Elemento che entra in contatto con il Trigger */

public Transform target; // Player
AutoMove AutoMoveScript; // richiamo variabili Script AutoMove

// Use this for initialization
void Start () {
    AutoMoveScript =  target.GetComponent<AutoMove>(); // Richiano componente AutoMove
}

// Update is called once per frame
void Update () {
    Vector3 targetDir = target.position - transform.position;
    Debug.Log(targetDir);
}

void OnTriggerEnter(Collider other) { // Quando il player entra nel Trigger
    if(other.collider.name == "MainCharacter"){

            AutoMoveScript.MoveSpeed = 8;  // Cambio variabile di Velocita'
            AutoMoveScript.activeAutoMoveW = true; // Cambio variabile di scorrimento
            AutoMoveScript.activeAutoMoveS = false; // Cambio variabile di scorrimento


    }

}

void OnTriggerStay(Collider other) { // Quando il player e' nel Trigger
    if(other.collider.name == "MainCharacter"){
        if(Input.GetKey(KeyCode.W)){

            AutoMoveScript.activeAutoMoveW = true; // Cambio variabile di scorrimento
            AutoMoveScript.activeAutoMoveS = false; // Cambio variabile di scorrimento

        }

        if(Input.GetKey(KeyCode.S)){

            AutoMoveScript.activeAutoMoveS = true; // Cambio variabile di scorrimento
            AutoMoveScript.activeAutoMoveW = false; // Cambio variabile di scorrimento

        }

    }

}

void OnTriggerExit(Collider other) { // Quando il player esce nel Trigger

    if(other.collider.name == "MainCharacter"){

            AutoMoveScript.MoveSpeed = 8; // Cambio variabile di Velocita'
            AutoMoveScript.activeAutoMoveS = false; // Cambio variabile di scorrimento
            AutoMoveScript.activeAutoMoveW = false; // Cambio variabile di scorrimento

    }

}
}

-AutoMove-(应用于主要角色)

使用 UnityEngine;使用 System.Collections;公共类 AutoMove : 单一行为{

/*ISTRUZIONI : Inserire questo Script all'interno del Player */

public bool activeAutoMoveW = false; //variabile controllo Forward
public bool activeAutoMoveS = false; //variabile controllo Back

public int MoveSpeed = 8;   //variabile velocità di scorrimento
public bool activeSelect = false; //variabile controllo Select


// Use this for initialization
void Start () {


}

// Update is called once per frame
void Update () {

    if(activeAutoMoveW == true)
    {
        transform.Translate(Vector3.forward * MoveSpeed * Time.fixedDeltaTime, Space.World); // Scorrimento in avari
    }

    if(activeAutoMoveS == true)
    {
        transform.Translate(Vector3.back * MoveSpeed * Time.fixedDeltaTime, Space.World); // Scorrimento in  dietro
    }

    if(Input.GetKeyDown(KeyCode.Q) && activeSelect == false)
    {
        MoveSpeed = 1; // Cambio variabile di scorrimento
        activeSelect = true;  // Cambio variabile Slect

    }else if(Input.GetKeyDown(KeyCode.Q) && activeSelect == true)
    {
        MoveSpeed = 8; // Cambio variabile di scorrimento
        activeSelect = false; // Cambio variabile Slect

    }

}   }

系统创建了滑倒效果,但不知道角色的方向是什么。

【问题讨论】:

    标签: c# unity3d game-physics


    【解决方案1】:

    如果我理解正确,问题是当你想滑倒时你不知道前进是什么。像您在示例中那样在世界空间中使用 Vector3.forward 的问题在于,无论对象如何定向,它都将始终是相同的方向。在对象空间中使用 Vector3.forward 的问题是它对整个对象都是一样的,所以如果你有一个像你在图片中那样弯曲的对象,它也不会工作。

    您需要一些方法来了解对象是如何弯曲的,一个简单的例子是沿着轨道手动放置一个 vector3 点数组,然后您可以通过 points[i+1]-points 计算方向[i] 与您之间的两点。假设您并不总是从铁路的起点开始,您还需要一些方法来找到您在哪些点之间可能并不容易。

    像这样的

    public class AutoMove : MonoBehaviour 
    {
      public Vector3[] points;
      private int currentIndex = -1;
      ...
    
      void Update()
      {
        if(activeAutoMoveW == true)
        {
            Vector3 direction = points[i+1]-points[i];
            transform.position += direction * MoveSpeed * Time.fixedDeltaTime;
    
            if((transform.position - points[i+1]).sqrMagnitude < 0.1f) //check if we are at the next point
                currentIndex++;
        }
        if(activeAutoMoveS == true)
        {
            Vector3 direction = points[i+1]-points[i];
            transform.position -= direction * MoveSpeed * Time.fixedDeltaTime;
    
            if((transform.position - points[i]).sqrMagnitude < 0.1f) //check if we are at the next point
                currentIndex--;
        }
    
        if(currentIndex < 0 || currentIndex > points.Length) //check if we are at either edge of the line and then end the slip
        {
            activeAutoMoveW = false;
            activeAutoMoveS = false;
        {
    
        ...
      }
    
      public void BeginSlip(Vector3[] points) //called from the object when you want to begin the slip
      {
        this.points = points; //get the slip points from the object you slip on
    
        //find the closest point
        float minDist = float.MaxValue;
        for(int i=0; i<points.Length; i++)
        {
          float currentDist = (transform.position - points[i]).sqrMagnitude;
          if(currentDist  < minDist)
          {
            minDist = currentDist;
            currentIndex = i;
          }
        }
      }
    }
    

    【讨论】:

    • 好的,我理解代码,但如何创建点曲线?以及如何找到我是哪些点?对于曲线,我认为“对于方法”[for(int i = 0; i
    • 根据你的网格是如何制作的,你可以做一些聪明的事情来创建点,否则它是手动的方式。通过手动方式,我的意思是使用 Vector3[] 的检查器并简单地按顺序输入每个点的坐标。您拥有的点越多,曲线看起来就越平滑。
    • 要找到您之间的点,您可能必须遍历所有点并选择最近的点。要么捕捉到那个点,要么做一些插值。
    • 我手动创建了 3 个点。该方向仅适用于两点。系统忽略第三点。你能写代码来理解插值或对齐点吗?谢谢
    • 我已经更新了一些代码,BeginSlip 应该从另一个对象调用,当滑动开始给玩家用于滑动的点并找到最近的点在哪里
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多