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

原创 2015年07月10日 19:01:39

当按下鼠标的时候给物理加力 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);
}
}
版权声明:

相关文章推荐

Away3D Bullet Physics 和MD2以及波浪

在Stage3D平台上,选择处理有关3D方面的内容时,Away3D是一把利器。Away3D对很多模型格式提供了强大的支持,并且它与其它的很多as3库可以很好的兼容。你会发现将Away3D和当前几大主流...

Libgdx Box2D实战---放开那小球(四:不规则body创建--physics-body-editor的使用)

如果遇到不规则的图形怎么办?难不成要一个个组装吗?这里就要提一个叫做“关节”的名词了,它可以将一个个部分进行组装成自己的形状,特别是各部件独自运转时,比如一辆自行车,它的轮子和车身都是各自运转的。但是...

关于cocos2d-X 3.x版本使用引擎自带的物理引擎Physics

今天本来打算使用BOX2D物理引擎的, 想到前段时间听说cocos2d-X在3.0版本中封装了一个自己的物理引擎Physics, 听名字就很霸气额, 用起来确实是比BOX2D简单很多( 毕竟是基于BO...

Cocos2d-x中Physics物理引擎碰撞检测,setCategoryBitmask,setContactTestBitmask,setCollisionBitmask

kan 物理世界的创建 Scene* TestPhysics::createScene(){ auto scene=Scene::createWithPhysics();//需要创建...

[译]A Verlet based approach for 2D game physics-Part One

翻译的一篇来自GameDev上面关于使用Verlet积分器创建简单2D游戏物理效果的文章

Physics Engine Box2D(CrossBridge(FlasCC) Version) Demo

1 修改命令行: FLASCC:=/cygdrive/C/Crossbridge_1.0.1/sdk FLEX:=/cygdrive/C/Program Files (x86)/Adobe/Adobe...

Adding Open Source 3D Physics to Your iOS Applications(2) Moving Bullet to iOS

Moving Bullet to iOS Now that everything works on Mac OS X, the next step is to get relevant portio...

cocos2d-x lua physics

在cocos2d-x 3.x封装了物理引擎 在layer中添加几个精灵 require("Cocos2d") require("Cocos2dConstants") require("bi...
  • qqqGL
  • qqqGL
  • 2014-12-03 10:40
  • 1857

[译]A Verlet based approach for 2D game physics - Part Three

翻译GameDev上关于使用Verlet创建2D物理效果的文章中关于碰撞反应的部分

[译]A Verlet based approach for 2D game physics-Part Two

翻译GameDev上的基于Verlet积分器创建2D物理效果中的碰撞检测部分的内容
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)