6

我调用了脚本PlayerCharacter来控制 Unity 2D 平台上的播放器。这是完美的,像往常一样工作。

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

[RequireComponent(typeof (Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
public class PlayerCharacter : MonoBehaviour
{
    public float speed = 1.0f;
    public string axisName = "Horizontal";
    private Animator anim;
    public string jumpButton = "Fire1";
    public float jumpPower = 10.0f;
    public float minJumpDelay = 0.5f;
    public Transform[] groundChecks;

    private float jumpTime = 0.0f;
    private Transform currentPlatform = null;
    private Vector3 lastPlatformPosition = Vector3.zero;
    private Vector3 currentPlatformDelta = Vector3.zero;


    // Use this for initialization
    void Start ()
    {
        anim = gameObject.GetComponent<Animator>();
    }


    // Update is called once per frame
    void Update ()
    {
        //Left and right movement
        anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis(axisName)));
        if(Input.GetAxis(axisName) < 0)
        {
        Vector3 newScale = transform.localScale;
        newScale.x = -1.0f;
        transform.localScale = newScale;
        Debug.Log("Move to left");
        }
        else if(Input.GetAxis(axisName) > 0)
        {
        Vector3 newScale = transform.localScale;
        newScale.x = 1.0f;
        transform.localScale = newScale;
        Debug.Log ("Move to Right");
        }
        transform.position += transform.right*Input.GetAxis(axisName)*speed*Time.deltaTime;

        //Jump logic
        bool grounded = false;
        foreach(Transform groundCheck in groundChecks)
        {
            grounded |= Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
        }
        anim.SetBool("Grounded", grounded);
        if(jumpTime > 0)
        {
            jumpTime -= Time.deltaTime;
        }
        if(Input.GetButton("jumpButton")  && anim.GetBool("Grounded") )

        {
            anim.SetBool("Jump",true);
            rigidbody2D.AddForce(transform.up*jumpPower);
            jumpTime = minJumpDelay;
        }
        if(anim.GetBool("Grounded") && jumpTime <= 0)
        {
            anim.SetBool("Jump",false);
        }
        //Moving platform logic
        //Check what platform we are on
        List<Transform> platforms = new List<Transform>();
        bool onSamePlatform = false;
        foreach(Transform groundCheck in groundChecks)
        {
            RaycastHit2D hit = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
            if(hit.transform != null)
            {
                platforms.Add(hit.transform);
                if(currentPlatform == hit.transform)
                {
                    onSamePlatform = true;
                }
            }
        }

        if(!onSamePlatform)
        {
            foreach(Transform platform in platforms)
            {
                currentPlatform = platform;
                lastPlatformPosition = currentPlatform.position;
            }
        }
    }

    void LateUpdate()
    {
        if(currentPlatform != null)
        {
            //Determine how far platform has moved
            currentPlatformDelta = currentPlatform.position - lastPlatformPosition;

            lastPlatformPosition = currentPlatform.position;
        }
        if(currentPlatform != null)
        {
            //Move with the platform
            transform.position += currentPlatformDelta;
        }
    }   
}

当我尝试使用可触摸控制器修改脚本时出现问题。我已经用谷歌搜索了很多次并尽可能地修改了脚本,但它仍然没有给我任何结果(顺便说一句,我是 Unity 的新手)。TouchControls然后我从一个网站上找到了一个关于使用 GUI 纹理 ( )制作触摸控制器的教程。我认为该教程很容易学习。这是脚本

using UnityEngine;
using System.Collections;

[RequireComponent(typeof (Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
public class TouchControls : MonoBehaviour {

    // GUI textures
    public GUITexture guiLeft;
    public GUITexture guiRight;
    public GUITexture guiJump;
    private Animator anim;
    // Movement variables
    public float moveSpeed = 5f;
    public float jumpForce = 50f;
    public float maxJumpVelocity = 2f;

    // Movement flags
    private bool moveLeft, moveRight, doJump = false;

    void Start ()
    {
        anim = gameObject.GetComponent<Animator>();
    }
    // Update is called once per frame
    void Update () {

        // Check to see if the screen is being touched
        if (Input.touchCount > 0)
        {
            // Get the touch info
            Touch t = Input.GetTouch(0);

            // Did the touch action just begin?
            if (t.phase == TouchPhase.Began)
            {
                // Are we touching the left arrow?
                if (guiLeft.HitTest(t.position, Camera.main))
                {
                    Debug.Log("Touching Left Control");
                    moveLeft = true;
                }

                // Are we touching the right arrow?
                if (guiRight.HitTest(t.position, Camera.main))
                {
                    Debug.Log("Touching Right Control");
                    moveRight = true;
                }

                // Are we touching the jump button?
                if (guiJump.HitTest(t.position, Camera.main))
                {
                    Debug.Log("Touching Jump Control");
                    doJump = true;
                }
            }

            // Did the touch end?
            if (t.phase == TouchPhase.Ended)
            {
                // Stop all movement
                doJump = moveLeft = moveRight = false;
            }
        }

        // Is the left mouse button down?
        if (Input.GetMouseButtonDown(0))
        {
            // Are we clicking the left arrow?
            if (guiLeft.HitTest(Input.mousePosition, Camera.main))
            {
                Debug.Log("Touching Left Control");
                moveLeft = true;
            }

            // Are we clicking the right arrow?
            if (guiRight.HitTest(Input.mousePosition, Camera.main))
            {
                Debug.Log("Touching Right Control");
                moveRight = true;
            }

            // Are we clicking the jump button?
            if (guiJump.HitTest(Input.mousePosition, Camera.main))
            {
                Debug.Log("Touching Jump Control");
                doJump = true;
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            // Stop all movement on left mouse button up
            doJump = moveLeft = moveRight = false;
        }
    }

    void FixedUpdate()
    {
        //anim.SetFloat("Speed", Mathf.Abs);
        // Set velocity based on our movement flags.
        if (moveLeft)
        {

            rigidbody2D.velocity = -Vector2.right * moveSpeed;
        }

        if (moveRight)
        {
            rigidbody2D.velocity = Vector2.right * moveSpeed;
        }

        if (doJump)
        {
            // If we have not reached the maximum jump velocity, keep applying force.
            if (rigidbody2D.velocity.y < maxJumpVelocity)
            {
                rigidbody2D.AddForce(Vector2.up * jumpForce);
            } else {
                // Otherwise stop jumping
                doJump = false;
            }
        }
    }
}

但我不知道如何实现教程 ( TouchControls) 中的脚本并将其分配给我的播放器控制脚本 ( PlayerCharacter)。如何结合这两个脚本,以便玩家可以通过可触摸控件来控制它?

4

2 回答 2

1

您可以做的最好的事情是不要将触摸控件从触摸控件教程拖到播放器控制器,而是相反,使用触摸控件教程脚本作为模板。

由于您的播放器控制器在其输入中使用浮点数,例如 moveleft = 50.0f; 并且触摸控件使用 moveleft = true;

这些脚本彼此非常不同,只能合并和工作。

因此,触摸控件中的更新功能保持原样,并且仅使用您的控件逻辑更新固定更新功能,因为更新无效,可以说是右,左,上和下的条件控制器。它还将处理触摸的实际输入。

然后,固定更新可以控制播放器控制器拥有的一些东西,例如在触摸标记的对象或类似的东西时施加力。并且更新只是输入条件,好的建议是将更新触摸代码包装在它自己的函数中,这样更新不仅是触摸,还有其他游戏逻辑相关的代码。

于 2014-01-30T12:54:47.643 回答
0

您应该搜索使用复制播放器控制器内的触摸控制脚本,同时更改正确的部分。例如,Input.GetKeyDown您应该使用 the而不是使用,Input.GetTouch但这取决于您正在创建的游戏。您应该注意该代码并更改某些部分

于 2020-12-01T18:25:50.780 回答