Unity3d如何从另一个脚本中引用滑动控制

本文关键字:引用 控制 脚本 另一个 Unity3d | 更新日期: 2023-09-27 18:05:06

我已经尝试了所有可能的方法来让它工作,但没有成功。我有一个滑动脚本和一个游戏/控制脚本。我试图在我的控制脚本中引用滑动控制作为控制我的角色的手段。到目前为止,我只从Unity中得到一个错误。

Assets/RuinRunStarterKit/Scripts/csTempleRun.cs(307,25):错误CS1501: No overload for method UpdatePlayer' takes 0' arguments

我试过添加TouchDetector。UpdatePlayer括号内的enTouchType但这给了我三个错误,而不是一个。我联系了很多人,访问了很多网站寻找答案,但都没有得到一个有效的结果。我没有受过编程方面的教育。我只是在网上研究了一下,想知道如何在编程方面做点什么。我这里的第二个脚本来自我在Unity资产商店购买的资产。我联系了他们,希望他们支持添加滑动控制,他们给我发了触摸脚本,但没有告诉我如何实现它。我只想在手机上玩这个游戏,用滑动作为控制。这是私人用品。如果有人能帮我达到预期的效果,我会非常感激。再一次,我希望能够使用滑动来控制角色。谢谢你!以下是我的脚本:

TouchDetector.cs;

    using UnityEngine;
    using System.Collections;
    public class TouchDetector : MonoBehaviour {
public delegate void deTouchEvent
    (enTouchType touchType);
public static event
    deTouchEvent
    evTouchEvent;
public enum enTouchType
{
    SwipeLeft,
    SwipeRight,
    SwipeDown,
    SwipeUp,
}   
void Start ()
{   
}   
void Update ()
{   
    if (evTouchEvent == null)
        return;
    if (Input.GetKeyDown(KeyCode.UpArrow   )) evTouchEvent(enTouchType.SwipeUp   );
    if (Input.GetKeyDown(KeyCode.DownArrow )) evTouchEvent(enTouchType.SwipeDown );
    if (Input.GetKeyDown(KeyCode.LeftArrow )) evTouchEvent(enTouchType.SwipeLeft );
    if (Input.GetKeyDown(KeyCode.RightArrow)) evTouchEvent(enTouchType.SwipeRight);
    if (Input.touchCount > 0)
    {
        foreach (Touch t in Input.touches)
        {
            Vector3 swipe = t.deltaPosition * t.deltaTime;
            if (swipe.y >  0.5f) evTouchEvent(enTouchType.SwipeUp   );
            if (swipe.y < -0.5f) evTouchEvent(enTouchType.SwipeDown );
            if (swipe.x >  0.5f) evTouchEvent(enTouchType.SwipeRight);
            if (swipe.x < -0.5f) evTouchEvent(enTouchType.SwipeLeft );
        }
    }
}
    }

csTempleRun.cs;

    void Update () 
    {   
        UpdatePlayer();
        if (m_player != null)
        {
            // Make the camera follow the player (if active)
            SmoothFollow sF = (SmoothFollow)Camera.mainCamera.GetComponent(typeof(SmoothFollow));
            sF.target = m_player.transform;         
            // Check for collisions with interactive objects
            UpdateCoins();
            UpdateMines();
            // Dynamically update the track
            CreateNewCellsIfNeeded(false);
        }       
    }

    private void UpdatePlayer(TouchDetector.enTouchType T)
{
    // if the player is dead (replaced with ragdoll) then exit since none of this code should fire.
    if (m_player == null) 
    {
        return;     
    }

    // Gradually increase the players' running speed, and update the animation to match.
    m_playerRunSpeed += Time.deltaTime * 0.005f;
    m_playerRunSpeed = Mathf.Clamp(m_playerRunSpeed, 0.5f, 3.0f);
    m_player.animation["run"].speed = m_playerRunSpeed * 2.0f;
    // ****************************************************************************************
    // INPUT
    // Player can only turn if they are not already sliding / jumping.  
    // Equally, sliding / jumping are mutually exclusive.
    if (Input.GetKeyDown (KeyCode.LeftArrow)&& m_playerJump <= -1.0f && m_playerSlide <=0.0f)
    {
        if (m_playerDirection == enCellDir.North) m_playerNextDirection = enCellDir.West;
        if (m_playerDirection == enCellDir.East ) m_playerNextDirection = enCellDir.North;
        if (m_playerDirection == enCellDir.South) m_playerNextDirection = enCellDir.East;
        if (m_playerDirection == enCellDir.West ) m_playerNextDirection = enCellDir.South;
    }
    if (Input.GetKeyDown(KeyCode.RightArrow) && m_playerJump <= -1.0f && m_playerSlide <=0.0f)
    {
        if (m_playerDirection == enCellDir.North) m_playerNextDirection = enCellDir.East;
        if (m_playerDirection == enCellDir.East ) m_playerNextDirection = enCellDir.South;
        if (m_playerDirection == enCellDir.South) m_playerNextDirection = enCellDir.West;
        if (m_playerDirection == enCellDir.West ) m_playerNextDirection = enCellDir.North;
    }   
    if (T==TouchDetector.enTouchType.SwipeDown && m_playerJump <= -1.0f && m_playerSlide <=0.0f)
    {
        m_playerSlide = 1.0f;
        m_player.animation.Play("slide_fake");
    }            
    if ((Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space)) && m_playerJump <= -1.0f && m_playerSlide <=0.0f)
    {
        AudioSource.PlayClipAtPoint(m_jumpAudio, m_player.transform.position);
        m_playerJump = 1.0f;
        m_playerYvel = 0.0f;
        m_player.animation.Play("jump");
    }

我希望我没有做任何错误的张贴这个脚本,但我觉得我需要张贴这个,以得到我需要的帮助。你会注意到在csTempleRun.cs脚本中,我用TouchDetector.enTouchType.SwipeDown替换了一个KeyCode调用。但我还是得到了一个错误。提前感谢任何人的帮助。谢谢你的宝贵时间。

Unity3d如何从另一个脚本中引用滑动控制

看看你的错误- No overload for method UpdatePlayer' takes0' arguments。这意味着您需要向方法提供额外的数据。您需要提供哪些数据,智能感知可以告诉您

在你的代码中:

void Update () 
{   
    UpdatePlayer(); 
    ...

调用UpdatePlayer()时没有参数。但是,UpdatePlayer需要一个参数:

private void UpdatePlayer(TouchDetector.enTouchType T)
{

所以,当你调用UpdatePlayer()时,你需要发送一个TouchDetector.enTouchType类型的对象作为参数。这将是以下选项之一:

  • SwipeLeft
  • SwipeRight
  • SwipeDown
  • SwipeUp