【第22期】观点:IT 行业加班,到底有没有价值?

火箭耗子 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);
}
}
版权声明: 举报

相关文章推荐

关于Physics2D

Physics2D 从一个点发射一条射线,

Physics2D.Linecast的使用注意事项

今天做2D游戏使用了Physics2D.Linecast函数,为了使物体不会撞进墙。 RaycastHit2D hit = Physics2D.Linecast(targetPosition, ta...

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

Unity3D 4.3新特性 —— 2D

Unity3D 终于发布了4.3版本。先来看看有啥特性: 这次发布的包主要包括这些特性:     一个完整的2D工具集     动画:混合变形和用于动画驱动的动画事件     MonoDevelo...

Physics2D.Linecast

grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 以上程式碼是用來測試是否在地面上 指的是投射一條...

Physics2D 检测圆形区域内的所有碰撞器

Physics2D.OverlapCircleNonAlloc 重叠圆形不分配 static int OverlapCircleNonAlloc(Vector2 point, float ra...

【Unity】技巧集合

转发,请保持地址:http://blog.csdn.net/stalendp/article/details/17114135 这篇文章将收集unity的相关技巧,会不断地更新内容。 1)保存运行中...

Unity 2D射线与 3D射线 UI射线

//3D射线 if (Input.GetMouseButton(0)) { Ray ray = Camera.main.ScreenPointToRay(Inp...

关于Unity3d中的LayerMask使用

最近看别人写的Demo,里面这个Linecast字段的LayerMask参数看了半天也没看懂 void Update()     {         // The player is gro...

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...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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