编程语言
首页 > 编程语言> > unity2D小游戏rubyadventure练习记录

unity2D小游戏rubyadventure练习记录

作者:互联网

transform:https://blog.csdn.net/weixin_42446849/article/details/81669864

//移动代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class control : MonoBehaviour
{
    public float speed = 5f;

    Rigidbody2D rbody;
    // Start is called before the first frame update
    void Start()
    {
        rbody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        //transform.Translate(transform.right * speed * Time.deltaTime);
        float movex = Input.GetAxisRaw("Horizontal");
        float movey = Input.GetAxisRaw("Vertical");

        Vector2 position = transform.position;
        position.x += movex * speed * Time.deltaTime;
        position.y += movey * speed * Time.deltaTime;
        //transform.position = position;
        rbody.MovePosition(position);
    }
}

跟随人物:写脚本或者加载cinemachine的package,然后把follow改成人物
添加相机边界:给相机添加cinemachine,然后bouding到一个新加组件,在这个组件里建立polya collider 2D,把边拖到地图边界并且勾上istrigger不然会弹出人物

背景:添加composite collider 2D,把背景块合并提升性能
刚体设成静态不然会漂移

只读访问private变量:

动画帧率设置

Instantiate所有重载方法及其使用规则
Instantiate(Object original):克隆物体original,其Position和Rotation取默认值,何为默认值呢?就是预制体的position,这里的position是世界坐标,无父物体
Instantiate(Object original, Transform parent):克隆物体original,拥有父物体,其Position和Rotation取默认值,这里的position是localposition,也就是相对于父物体的坐标,父物体为坐标原点
Instantiate(Object original, Transform parent, bool instantiateInWorldSpace):
若instantiateInWorldSpace=false,代表克隆的物体的坐标是localposition,则与Instantiate(Object original, Transform parent)的结果一样。
若instantiateInWorldSpace=true,则代表克隆的物体的坐标为世界坐标
Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent):这个比较好理解了,克隆物体original,拥有父物体,其Position和Rotation由人为设置,而且这里设置的坐标是世界坐标,不是localposition

设置碰撞

纹理:

按e拾取/触发:

     if (Input.GetKeyDown(KeyCode.E))
        {
            RaycastHit2D hit = Physics2D.Raycast(rbody.position, lookDirection, 2f, LayerMask.GetMask("NPC"));
            if (hit.collider != null)
            {
                Debug.Log("HIT!!!!");
            }
        }

标签:Object,unity2D,物体,Instantiate,碰撞,小游戏,position,rubyadventure,original
来源: https://www.cnblogs.com/diakla/p/16548787.html