火箭耗子 5.1 AddForce 加前进的速度 设置离子的层 Physics2D.OverlapCircleAll

来源:互联网 发布:手机制作奖状软件 编辑:程序博客网 时间:2024/05/02 01:06

当按下鼠标的时候给物理加力 5.0后要获取组件然后加AddForce
public float jetpackForce = 75.0f;
    void FixedUpdate () 
{
bool jetpackActive = Input.GetButton("Fire1");

if (jetpackActive)
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jetpackForce));
}

Vector2 newVelocity =  GetComponent<Rigidbody2D>().velocity;速度矢量传递
newVelocity.x = forwardMovementSpeed;
GetComponent<Rigidbody2D>().velocity = newVelocity;
}

GetComponent<ParticleSystemRenderer> ().sortingLayerName="Player";   设置粒子系统的层级
GetComponent<ParticleSystemRenderer> ().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<Animator>();
}


void FixedUpdate () 
{
if(forwardMovementSpeed <= 10)
{
forwardMovementSpeed += forwardAddSpeed;
}


bool jetpackActive = Input.GetButton("Fire1");


jetpackActive = jetpackActive && !dead;


//parallax.offset = transform.position.x;

if (jetpackActive)
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jetpackForce));
}


if(!dead){
Vector2 newVelocity = GetComponent<Rigidbody2D>().velocity;
newVelocity.x = forwardMovementSpeed;
GetComponent<Rigidbody2D>().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<AudioSource>().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);
}
}
0 0