Jump to content

Skrypty do kursu Unity w CD-Action


CormaC

Recommended Posts

#############SKRYPT WEAPON

using UnityEngine;
using System.Collections;

public class Weapon : MonoBehaviour
{
    public GameObject m_Bullet;
    public Transform m_BulletSpawnPoint;
    public int m_MagazineCapacity = 10;
    public int m_MaxAmmo = 30;

    private int m_CurrentAmmoAmount;
    private int m_CurrentMagazineAmount;

    void Start()
    {
        m_CurrentMagazineAmount = m_MagazineCapacity;
        m_CurrentAmmoAmount = m_MaxAmmo;
    }

    void Update()
    {
        if (Input.GetButtonDown("Reload"))
        {
            int bulletsToAdd = m_MagazineCapacity - m_CurrentMagazineAmount;

            if (m_CurrentAmmoAmount < bulletsToAdd)
            {
                bulletsToAdd = m_CurrentAmmoAmount;
            }

            m_CurrentAmmoAmount -= bulletsToAdd;
            m_CurrentMagazineAmount += bulletsToAdd;
        }

        if (Input.GetButtonDown("Fire1") && m_CurrentMagazineAmount > 0)
        {
            Instantiate(m_Bullet, m_BulletSpawnPoint.position, m_BulletSpawnPoint.rotation);
            m_CurrentMagazineAmount--;
        }
    }

    public void AddBullets(int amount)
    {
        m_CurrentAmmoAmount = Mathf.Clamp(m_CurrentAmmoAmount + amount, 0, m_MaxAmmo);
    }
}


#############SKRYPT WAYPOINT

using UnityEngine;
using System.Collections;

public class WayPoint : MonoBehaviour
{
    public float m_WaitTime = 3;
}


#############SKRYPT PLAYERMOVEMENT

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{

    private Rigidbody2D m_Rigidbody;
    private bool m_Grounded = false;

    public float m_Speed = 10;
    public float m_JumpForce = 100;

    void Start()
    {
        m_Rigidbody = GetComponent<Rigidbody2D>() as Rigidbody2D;
    }

    void Update()
    {

    }

    void OnTriggerEnter2D(Collider2D other)
    {
        m_Grounded = true;
    }

    void OnTriggerExit2D(Collider2D other)
    {
        m_Grounded = false;
    }

    void FixedUpdate()
    {
        if (Input.GetButton("Horizontal"))
        {
            m_Rigidbody.velocity = new Vector2(Input.GetAxis("Horizontal") * m_Speed, m_Rigidbody.velocity.y);

            if (m_Rigidbody.velocity != Vector2.zero)
            {
                if (m_Rigidbody.velocity.x < 0)
                {
                    transform.right = Vector2.left;
                }
                else {
                    transform.right = Vector2.right;
                }
            }
        }

        if (Input.GetButton("Jump") && m_Grounded)
        {
            m_Rigidbody.AddForce(new Vector2(0, Input.GetAxis("Jump") * m_JumpForce));
        }
    }
}


#############SKRYPT GAMECONTROLLER

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class GameController : MonoBehaviour
{

    private static GameController m_Instance;

    void Start()
    {
        if (m_Instance == null)
        {
            m_Instance = this;
        }
        else {
            Destroy(gameObject);
        }

        DontDestroyOnLoad(gameObject);
    }

    void Update()
    {
        if (Input.GetButton("Cancel"))
        {
            Application.Quit();
        }
    }

    public static void NextLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
}


#############SKRYPT EXIT

using UnityEngine;
using System.Collections;

public class Exit : MonoBehaviour
{
    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.name == "Player")
        {
            GameController.NextLevel();
        }
    }
}


#############SKRYPT ENEMY

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

public class Enemy : MonoBehaviour
{
    public int m_Health = 3;
    public float m_JumpForce = 350;
    public List<WayPoint> m_WayPoints;
    public float m_Speed = 5;
    public float m_SnapDistance = 0.4f;
    public GameObject m_Target;
    public float m_TargetDistance = 3;

    private float m_WayPointTime = 0;
    private float m_CurrentWayPointTime = 0;
    private int m_CurrentWayPointIndex = 0;
    private bool m_DestinationReached = false;
    private Rigidbody2D m_Rigidbody;

    void Start()
    {
        m_Rigidbody = GetComponent<Rigidbody2D>() as Rigidbody2D;
    }

    void Update()
    {
        if (m_Target == null)
        {
            if (!m_DestinationReached)
            {
                if (Vector3.Distance(transform.position, m_WayPoints[m_CurrentWayPointIndex].transform.position) > m_SnapDistance)
                {
                    Vector2 direction = m_WayPoints[m_CurrentWayPointIndex].transform.position - transform.position;
                    direction.Normalize();
                    m_Rigidbody.velocity = direction * m_Speed;
                }
                else {
                    m_Rigidbody.position = m_WayPoints[m_CurrentWayPointIndex].transform.position;
                    m_WayPointTime = m_WayPoints[m_CurrentWayPointIndex].m_WaitTime;
                    m_DestinationReached = true;
                    m_CurrentWayPointIndex = (++m_CurrentWayPointIndex) % m_WayPoints.Count;
                    m_Rigidbody.velocity = Vector3.zero;
                }
            }
            else {
                m_CurrentWayPointTime += Time.deltaTime;

                if (m_CurrentWayPointTime >= m_WayPointTime)
                {
                    m_CurrentWayPointTime = 0;
                    m_DestinationReached = false;
                }
            }
        }
        else {
            if (Vector3.Distance(transform.position, m_Target.transform.position) > m_TargetDistance)
            {
                Vector2 direction = m_Target.transform.position - transform.position;
                direction.Normalize();
                m_Rigidbody.velocity = direction * m_Speed;
            }
        }
    }

    void OnCollisionEnter2D(Collision2D coll)
    {
        Bullet bullet = coll.collider.GetComponent<Bullet>() as Bullet;

        if (bullet != null)
        {
            m_Health--;

            if (m_Health <= 0)
            {
                Destroy(gameObject);
            }
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        PlayerMovement player = other.GetComponent<PlayerMovement>();

        if (player != null)
        {
            m_Target = player.gameObject;
        }
    }

    void OnTriggerExit2D(Collider2D other)
    {
        PlayerMovement player = other.GetComponent<PlayerMovement>();

        if (player != null)
        {
            m_Target = null;
        }
    }
}


#############SKRYPT CAMERAFOLLOW

using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour {

    public GameObject m_Target;

    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {

        transform.position = new Vector3(m_Target.transform.position.x,
                                 m_Target.transform.position.y,
                                 transform.position.z);

    }
}


#############SKRYPT BULLET

using UnityEngine;
using System.Collections;

public class Bullet : MonoBehaviour
{
    public float m_Speed = 10;
    public float m_LifeTime = 5;
    private Rigidbody2D m_Rigidbody;

    void Start()
    {
        m_Rigidbody = GetComponent<Rigidbody2D>() as Rigidbody2D;
        m_Rigidbody.velocity = new Vector2(m_Speed * transform.right.x, 0);

        Destroy(gameObject, m_LifeTime);
    }

    void OnCollisionEnter2D(Collision2D coll)
    {
        Destroy(gameObject);
    }
}


#############SKRYPT AMMO

using UnityEngine;
using System.Collections;

public class Ammo : MonoBehaviour
{
    public int m_Amount = 10;

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.name == "Player") {
            Weapon weapon = other.GetComponent<Weapon>() as Weapon;
            weapon.AddBullets(m_Amount);
            Destroy(gameObject);
        }
    }
}
 

Link to comment
Share on other sites

  • 1 month later...

zabralem sie dzis za to UNITY i wlasnie podarzajac za 1 czescia z nr 2/2016 nie moge dodac bohatera w pole "Target" w komponencie "CameraFollow" w oknie Inspector !!! Probuje przeciagnac go z okna "Hierarchy" i upuscic na polu "Target" i nic :( Co moze byc przyczyna ?? Dodam ze postac jest z przezroczystym tlem zapisana w formacie *png . Prosze o jakies wskazowki ... z gory dzieki Pozdrawiam

Link to comment
Share on other sites

Prosimy o tworzenie zapytań w temacie poniżej: 

 

PS Jutro będę pomagał innemu użytkownikowi (live#skype) - mogę również Tobie. Wyślij mi proszę w wiadomości prywatnej swoją nazwę konta skype. (czemu skype? Udostępnianie ekranu)

Link to comment
Share on other sites

  • 1 month later...

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...