无限亚军Unity2d

时间:2019-05-13 17:35:38

标签: c# unity3d

因此,当我开始游戏时,我的角色可以在第一个平台上跳跃(因为这是手动放置的平台),但是我不能在生成的地板上跳跃。顺便说一句,我能够在地板上奔跑,而且我知道自己的跳动作正常。

我已经尝试了多种方法来检测对撞机,这让我发疯了,而且我知道它是一个简单的解决方法,我无法弄清。

我希望角色能够在重复的平台上跳跃,但是角色根本什么也没做。 如果有人愿意看一下,那将非常有帮助。 -尼克

PS我知道我的代码很乱。

代码:

#Code that is on my player script#
using System;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using TouchControlsKit;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Text;
using System.IO;

public class Attack : MonoBehaviour
{


    const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded

    [SerializeField] private LayerMask m_WhatIsGround;

    [SerializeField] private Transform m_GroundCheck;

    private bool m_Grounded;

    public Collider2D objectCollider;

    public Collider2D anotherCollider;

    [Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f;

    private Timer t;

    private Timer a;

    private float timeStamp;

    private float die = 0;

    public GameObject bullet;

    private bool m_FacingRight = true;

    public float move;

    private Vector3 velocity = Vector3.zero;

    public GameObject idle_0;

    public playscript play;

    public Transform player;

    private Rigidbody2D m_Rigidbody2D;

    [SerializeField] private float m_JumpForce = 200f;

    bool swing = false;

    bool isgrounded = false;

    public bool canJump = false;

    bool slide = false;

    public Transform groundLayer; // Insert the layer here.

    public Vector2 jumpHeight;

    private Vector2 touchOrigin = -Vector2.one;

    public Vector2 moveSpeed;

    public bool run;

    Collider2D m_Collider;

    // variable to hold a reference to our SpriteRenderer component
    private SpriteRenderer mySpriteRenderer;

    // This function is called just one time by Unity the moment the component loads
    private void Awake()
    {
        // get a reference to the SpriteRenderer component on this gameObject
        mySpriteRenderer = GetComponent<SpriteRenderer>();
        animator.SetBool("death", false);
    }



    public Animator animator;

    Animator anim;
    int swingHash = Animator.StringToHash("swing");


    // Use this for initialization
    void Start()
    {



        timeStamp = Time.time + 5;

        m_Collider = GetComponent<Collider2D>();

        run = false;

        m_Rigidbody2D = GetComponent<Rigidbody2D>();

        anim = GetComponent<Animator>();

        animator.SetBool("isgrounded", false);

        isgrounded = false;

        canJump = false;

        animator.SetBool("swing", false);
    }

    private void FixedUpdate()
    {
        m_Grounded = false;

        // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
        // This can be done using layers instead but Sample Assets will not overwrite your project settings.
        Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
        for (int i = 0; i < colliders.Length; i++)
        {

            if (colliders[i].gameObject != gameObject)

                animator.SetBool("isgrounded", true);

                m_Grounded = true;
        }
    }
    // Update is called once per frame
    void Update()
    {

        anotherCollider = GameObject.FindGameObjectWithTag("Ground").GetComponent<BoxCollider2D>();

        objectCollider = GameObject.FindGameObjectWithTag("Player").GetComponent<CapsuleCollider2D>();

        Vector3 targetVelocity = new Vector2(move * 2f, m_Rigidbody2D.velocity.y);
        m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref velocity, m_MovementSmoothing);
        animator.SetBool("run", true);



        if (move > 0 && !m_FacingRight)
        {
            // ... flip the player.
            Flip();
        }
        // Otherwise if the input is moving the player left and the player is facing right...
        else if (move < 0 && m_FacingRight)
        {
            // ... flip the player.
            Flip();
        }


        int horizontal = 0;     //Used to store the horizontal move direction.
        int vertical = 0;       //Used to store the vertical move direction.

#if UNITY_STANDALONE || UNITY_WEBPLAYER

        //Check if we are running on iOS, Android, Windows Phone 8 or Unity iPhone
#elif UNITY_IOS || UNITY_ANDROID || UNITY_WP8 || UNITY_IPHONE

        //Check if Input has registered more than zero touches
        if (Input.touchCount > 0)
        {
            //Store the first touch detected.
            Touch myTouch = Input.touches[0];

            //Check if the phase of that touch equals Began
            if (myTouch.phase == TouchPhase.Began)
            {
                //If so, set touchOrigin to the position of that touch
                touchOrigin = myTouch.position;
            }

            //If the touch phase is not Began, and instead is equal to Ended and the x of touchOrigin is greater or equal to zero:
            else if (myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0)
            {
                //Set touchEnd to equal the position of this touch
                Vector2 touchEnd = myTouch.position;

                //Calculate the difference between the beginning and end of the touch on the x axis.
                float x = touchEnd.x - touchOrigin.x;

                //Calculate the difference between the beginning and end of the touch on the y axis.
                float y = touchEnd.y - touchOrigin.y;

                //Set touchOrigin.x to -1 so that our else if statement will evaluate false and not repeat immediately.
                touchOrigin.x = -1;

                //Check if the difference along the x axis is greater than the difference along the y axis.
                if (Mathf.Abs(x) > Mathf.Abs(y))
                    //If x is greater than zero, set horizontal to 1, otherwise set it to -1
                    horizontal = x > 0 ? 1 : -1;
                else
                    //If y is greater than zero, set horizontal to 1, otherwise set it to -1
                    vertical = y > 0 ? 1 : -1;
            }
        }

#endif
        if (TCKInput.GetAction("jumpBtn", EActionEvent.Up))
        {
            animator.SetBool("jump", false);
        }


        if (TCKInput.GetAction("jumpBtn", EActionEvent.Down) && m_Grounded == true)
        {
            animator.SetBool("jump", true);
            m_Grounded = false;
            m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
        }



        if (TCKInput.GetAction("fireBtn", EActionEvent.Down))
        {
            animator.SetBool("swing", true);
            m_Collider.enabled = !m_Collider.enabled;
        }

        if (TCKInput.GetAction("fireBtn", EActionEvent.Up))
        {
            animator.SetBool("swing", false);
            m_Collider.enabled = !m_Collider.enabled;
        }

        if (TCKInput.GetAction("slideBtn", EActionEvent.Down))
        {
            if (timeStamp <= Time.time)
            {
                animator.SetBool("slide", true);
                GameObject b = (GameObject)(Instantiate(bullet, transform.position + transform.right * 1.5f, Quaternion.identity));
                b.GetComponent<Rigidbody2D>().AddForce(transform.right * 1000);
                timeStamp = Time.time + 5;
            }
        }




        if (TCKInput.GetAction("slideBtn", EActionEvent.Up))
        {
            animator.SetBool("slide", false);
        }

        if (TCKInput.GetAction("right", EActionEvent.Press))
        {
            move = -1;

        }
        if (TCKInput.GetAction("right", EActionEvent.Up))
        {
            animator.SetBool("run", false);
        }


        if (TCKInput.GetAction("left", EActionEvent.Press))
        {
            move = 1;

        }
        if (TCKInput.GetAction("left", EActionEvent.Up))
        {
            animator.SetBool("run", false);
        }

        if (objectCollider.IsTouching(anotherCollider))
        {
            canJump = true;
        }
        else
        {
            canJump = false;
        }



    }

    private void Flip()
    {
        // Switch the way the player is labelled as facing.
        m_FacingRight = !m_FacingRight;

        // Multiply the player's x local scale by -1.
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }



    void Hurt()
    {
        move = 4;
        SceneManager.LoadScene(0);
    }


    protected void OnCollisionEnter2D(Collision2D collision)
    {

        EnemyHealth3 enemy = collision.collider.GetComponent<EnemyHealth3>();

        if (enemy != null)
        {
            move = 0;
            animator.SetBool("death", true);
            m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
            StartCoroutine(ExecuteAfterTime(.1));
        }

    }


    IEnumerator ExecuteAfterTime(double time)
    {
        yield return new WaitForSeconds((float)time);
        Hurt();
    }

}
#Code that is on the floor spawner script#
using UnityEngine;
using System.Collections;

public class Floor_Spawn_Script : MonoBehaviour
{

    public GameObject[] obj;

    private float oldPosition;
    private float currentPosition;
    private float ctr = 0;
    private float inte = 10.19f;

    // Use this for initialization
    private void Start()
    {
        oldPosition = transform.position.x;
        AddRoom(ctr * inte);
        ctr += 1;
        AddRoom(ctr * inte);
        ctr += 1;
        AddRoom(ctr * inte);
    }

    // Update is called once per frame
    void Update()
    {
        currentPosition = transform.position.x;
        if ((currentPosition - oldPosition) <= 9.595f)
        {
            AddRoom(ctr * inte);
            oldPosition = transform.position.x;
            ctr += 1;
        }
    }

    void AddRoom(float roomCenter)
    {
        GameObject room = (GameObject)Instantiate(obj[Random.Range(0, obj.Length)]);
        room.transform.position = new Vector3(roomCenter, 0f, 10f);

    }

}```

0 个答案:

没有答案