当按下鼠标的时候给物理加力 5.0后要获取组件然后加AddForce
public float jetpackForce = 75.0f;
void FixedUpdate ()
{
bool jetpackActive = Input.GetButton("Fire1");
if (jetpackActive)
{
GetComponent().AddForce(new Vector2(0, jetpackForce));
}
Vector2 newVelocity = GetComponent().velocity;速度矢量传递
newVelocity.x = forwardMovementSpeed;
GetComponent().velocity = newVelocity;
}
GetComponent ().sortingLayerName="Player"; 设置粒子系统的层级
GetComponent ().sortingOrder = -1;
Collider2D[] enemies = Physics2D.OverlapCircleAll(transform.position, bombRadius, 1 << LayerMask.NameToLayer("Enemies"));
这个方法定义了一个2D碰撞数组,以指定的顶点为圆心(transform.position),设置圆的半径(bombRadius),在指定的层(1 << LayerMask.NameToLayer("Enemies"))作用。
在这个层里指定圆的范围内添加所有包涵collider的个体。返回数组中的个体按照Z递增排列。
public class MouseController : MonoBehaviour {
public float jetpackForce = 75.0f;
public float forwardMovementSpeed = 3.0f;
public float forwardAddSpeed = 0.03f;
public Transform groundCheckTransform;
private bool grounded;
public LayerMask groundCheckLayerMask;
Animator animator;
public ParticleSystem jetpack;
private bool dead = false;
private uint coins = 0;
public AudioClip coinCollectSound;
public AudioSource jetpackAudio;
public AudioSource footstepsAudio;
//public ParallaxScroll parallax;
public Text coinsLabel;
void Start (){
animator = GetComponent();
}
void FixedUpdate ()
{
if(forwardMovementSpeed <= 10)
{
forwardMovementSpeed += forwardAddSpeed;
}
bool jetpackActive = Input.GetButton("Fire1");
jetpackActive = jetpackActive && !dead;
//parallax.offset = transform.position.x;
if (jetpackActive)
{
GetComponent().AddForce(new Vector2(0, jetpackForce));
}
if(!dead){
Vector2 newVelocity = GetComponent().velocity;
newVelocity.x = forwardMovementSpeed;
GetComponent().velocity = newVelocity;
}
UpdateGroundedStatus();
AdjustJetpack (jetpackActive);
AdjustFootstepsAndJetpackSound(jetpackActive);
}
void UpdateGroundedStatus()
{
//1
grounded = Physics2D.OverlapCircle(groundCheckTransform.position, 0.4f, groundCheckLayerMask); //返回布尔,检测0.4为半径的园,到地面的距离
//2
animator.SetBool("grounded", grounded);
}
void AdjustJetpack (bool jetpackActive)
{
if(!dead){
jetpack.enableEmission = !grounded;
jetpack.emissionRate = jetpackActive ? 400.0f : 50.0f;
}
}
void OnTriggerEnter2D(Collider2D collider)
{
if (collider.gameObject.CompareTag("Coins"))
CollectCoin(collider);
else
HitByLaser(collider);
}
void HitByLaser(Collider2D laserCollider)
{
if (!dead)
laserCollider.gameObject.GetComponent().Play();
dead = true;
jetpack.enableEmission = false;
animator.SetBool("dead", true);
}
void CollectCoin(Collider2D coinCollider)
{
coins++;
Destroy(coinCollider.gameObject);
AudioSource.PlayClipAtPoint(coinCollectSound, transform.position);
coinsLabel.text = coins.ToString();
}
void AdjustFootstepsAndJetpackSound(bool jetpackActive)
{
footstepsAudio.enabled = !dead && grounded;
jetpackAudio.enabled = !dead && !grounded;
jetpackAudio.volume = jetpackActive ? 1.0f : 0.5f;
}
}
-------------------------------------------
using UnityEngine;
using System.Collections;
// 背景移动 设置主相机背景为景深 挂到副相机上
public class ParallaxScroll : MonoBehaviour {
public Renderer background;
public Renderer foreground;
public float backgroundSpeed = 0.02f;
public float foregroundSpeed = 0.06f;
public float offset = 0;
// Update is called once per frame
void Update () {
float backgroundOffset = Time.timeSinceLevelLoad * backgroundSpeed;
float foregroundOffset = Time.timeSinceLevelLoad* foregroundSpeed;
background.material.mainTextureOffset = new Vector2(backgroundOffset, 0);
foreground.material.mainTextureOffset = new Vector2(foregroundOffset, 0);
}
}