http://pixelnest.io/tutorials/2d-game-unity/player-and-enemies/
According to the aircraft tutorial on the official website
Build good scenes
Bind rigid body code to player
Control rigid body speed with arrow keys
using UnityEngine; using System.Collections; public class PlayerScript : MonoBehaviour { // Use this for initialization void Start () { print("Start!"); } public Vector2 speed = new Vector2(10, 10); public Vector2 movement; public Rigidbody2D rigidbodyComponent; // Update is called once per frame void Update () { float inputX = Input.GetAxis("Horizontal"); float inputY = Input.GetAxis("Vertical"); movement = new Vector2( speed.x * inputX, speed.y * inputY ); } void FixedUpdate() { if (rigidbodyComponent == null) rigidbodyComponent = GetComponent<Rigidbody2D>(); rigidbodyComponent.velocity = movement; } void Awake() { print("I am a constructor"); } }
Input.GetAxis is to capture the operation on the keyboard
Rigidbody2D.velocity Velocity
http://wiki.ceeger.com/script/unityengine/classes/rigidbody2d/rigidbody2d.velocityMobile script:
using UnityEngine; using System.Collections; public class MoveScript : MonoBehaviour { public Vector2 movement; public Vector2 speed = new Vector2(-2, 0); // Use this for initialization void Start () { } // Update is called once per frame void Update () { movement = new Vector2( speed.x * 1, speed.y * 0 ); } void FixedUpdate() { rigidbody2D.velocity = movement; } }
Blood volume script:
using UnityEngine; using System.Collections; public class HealthScript : MonoBehaviour { public int hp = 1; public bool isEnemy = true; public void Damage(int damageCount){ hp -= damageCount; if(hp <= 0){ Destroy(gameObject); } } void OnTriggerEnter2D(Collider2D otherCollider){ ShotScript shot = otherCollider.gameObject.GetComponent<ShotScript>(); if (shot != null) { if(shot.isEnemyShot != isEnemy){ Damage(shot.damage); Destroy(shot.gameObject); } } } }
Destroy(shot.gameObject);
Represents deletion of an in-game object
When starting up, there is a very strange BUG bullet will float up. .
The reason is found because the initial value displacement offset of 15,15 is assigned to the attribute in U3D
Start writing the fire script:
using UnityEngine; /// <summary> /// Launch projectile /// </summary> public class WeaponScript : MonoBehaviour { //-------------------------------- // 1 - Designer variables //-------------------------------- /// <summary> /// Projectile prefab for shooting /// </summary> public Transform shotPrefab; /// <summary> /// Cooldown in seconds between two shots /// </summary> public float shootingRate = 0.25f; //-------------------------------- // 2 - Cooldown //-------------------------------- private float shootCooldown; void Start() { shootCooldown = 0f; } void Update() { if (shootCooldown > 0) { shootCooldown - = Time.deltaTime; } } //-------------------------------- // 3 - Shooting from another script //-------------------------------- /// <summary> /// Create a new projectile if possible /// </summary> public void Attack(bool isEnemy) { if (CanAttack) { shootCooldown = shootingRate; // Create a new shot var shotTransform = Instantiate(shotPrefab) as Transform; // Assign position shotTransform.position = transform.position; // The is enemy property ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript>(); if (shot != null) { shot.isEnemyShot = isEnemy; } // Make the weapon shot always towards it MoveScript move = shotTransform.gameObject.GetComponent<MoveScript>(); if (move != null) { move.direction = this.transform.right; // towards in 2D space is the right of the sprite } } } /// <summary> /// Is the weapon ready to create a new projectile? /// </summary> public bool CanAttack { get { return shootCooldown <= 0f; } } }
Transform:
Transform of the game object
The Transform property can be set for position, rotation, and size
position: position
Rotate: rotate
size: localScale
If you want to manipulate the GameObject currently used by the script, you can omit the GameObject to be manipulated.
Time.deltaTime
https://blog.csdn.net/wdjhzw/article/details/73433658?locationNum=7&fps=1
Using a standard package will block your thoughts and won't make your game stand out.
This sentence should be remembered for every game developer
using UnityEngine; public static class RendererExtensions { public static bool IsVisibleFrom(this Renderer renderer, Camera camera) { Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera); return GeometryUtility.TestPlanesAABB(planes, renderer.bounds); } }
Extended camera
using UnityEngine; /// <summary> /// Creating instance of particles from code with no effort /// </summary> public class SpecialEffectsHelper : MonoBehaviour { /// <summary> /// Singleton /// </summary> public static SpecialEffectsHelper Instance; public ParticleSystem smokeEffect; public ParticleSystem fireEffect; void Awake() { // Register the singleton if (Instance != null) { Debug.LogError("Multiple instances of SpecialEffectsHelper!"); } Instance = this; } /// <summary> /// Create an explosion at the given location /// </summary> /// <param name="position"></param> public void Explosion(Vector3 position) { // Smoke on the water instantiate(smokeEffect, position); // Tu tu tu, tu tu tudu // Fire in the sky instantiate(fireEffect, position); } /// <summary> /// Instantiate a Particle system from prefab /// </summary> /// <param name="prefab"></param> /// <returns></returns> private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position) { ParticleSystem newParticleSystem = Instantiate( prefab, position, Quaternion.identity ) as ParticleSystem; // Make sure it will be destroyed Destroy( newParticleSystem.gameObject, newParticleSystem.startLifetime ); return newParticleSystem; } }
There is a bug in the code that calls the explosion particles, and I can't find the reason...
Add audio
using UnityEngine; using System.Collections; /// <summary> /// Create a sound effect instance /// </summary> public class SoundEffectsHelper : MonoBehaviour { /// <summary> /// static instance /// </summary> public static SoundEffectsHelper Instance; public AudioClip explosionSound; public AudioClip playerShotSound; public AudioClip enemyShotSound; void Awake() { // register static instance if (Instance != null) { Debug.LogError("Multiple instances of SoundEffectsHelper!"); } Instance = this; } public void MakeExplosionSound() { MakeSound(explosionSound); } public void MakePlayerShotSound() { MakeSound(playerShotSound); } public void MakeEnemyShotSound() { MakeSound(enemyShotSound); } /// <summary> /// Play the given sound effect /// </summary> /// <param name="originalClip"></param> private void MakeSound(AudioClip originalClip) { // Make a non-null judgment to prevent exceptions from causing the remaining operations to be aborted if (Instance.ToString() != "null") { // Since it's not a 3D audio clip, the position doesn't matter. AudioSource.PlayClipAtPoint(originalClip, transform.position); } } }
shooting sound
if (isEnemy) { SoundEffectsHelper.Instance.MakeEnemyShotSound(); } else { SoundEffectsHelper.Instance.MakePlayerShotSound(); }Assign: