unity5.x从入门到精通-7章

sorting Layer Skprite 的分类层,层级越靠前,优先级越高,相同情况下后被渲染。

Order in Layer  Sprite所在层的顺序。数值越大优先级越高,相同情况下后被渲染。

float hatWidth=GetComponent<Renderer>().bounds.extents.x;   计算帽子的宽度.


物体随着鼠标的移动而移动的脚本:

using UnityEngine;
using System.Collections;
public class HatController : MonoBehaviour {
//实例化的对象.
public GameObject effect;
private Vector3 rawPosition;
private Vector3 hatPosition;
private float maxWidth;
// Use this for initialization
void Start () {
//将屏幕的宽度转换成世界坐标.
Vector3 screenPos = new Vector3(Screen.width,0,0);
Vector3 moveWidth=Camera.main.ScreenToWorldPoint(screenPos);
//计算帽子的宽度.
float hatWidth=GetComponent<Renderer>().bounds.extents.x;
//获得帽子的初始位置.
hatPosition=transform.position;
//计算帽子的移动宽度.
maxWidth=moveWidth.x-hatWidth;

}

// Update is called once per physics timestep
void FixedUpdate () {
//将鼠标的屏幕位置转换成世界坐标.
rawPosition=Camera.main.ScreenToWorldPoint(Input.mousePosition);
//设置帽子将要移动的位置,帽子移动范围控制.
hatPosition= new Vector3(rawPosition.x,hatPosition.y,0);
hatPosition.x=Mathf.Clamp(hatPosition.x,-maxWidth,maxWidth);
//帽子移动.
GetComponent<Rigidbody2D>().MovePosition(hatPosition);
}
//有碰撞体进入触发器时触发.
void OnTriggerEnter2D(Collider2D col){
GameObject neweffect=(GameObject)Instantiate(effect,transform.position,effect.transform.rotation);
neweffect.transform.parent=transform;
Destroy(col.gameObject);
Destroy(neweffect,1.0f);


}


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值