unity新手笔记
对2d物体施加一个y方向的力
rigibody2d rg rg.addForce(new vector2(0,100f))
可以自己填入数值,也可以使用默认的方向
vector.left
vector.right
vector.up
vector.down
物体的四个方向移动,但是斜向速度会把两个方向的速度结合,所以斜向运动需要单独写
if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Translate(Vector2.left * speed * Time.deltaTime);
        }
        if(Input.GetKey(KeyCode.RightArrow))
        {
            transform.Translate(Vector2.right * speed * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            transform.Translate(Vector2.up * speed * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.DownArrow))
        {
            transform.Translate(Vector2.down * speed * Time.deltaTime);
        }
物体移动的同时让图片转向
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour {
    //速度
    public float moveSpeed = 3;
    //获取对象的SpriteRenderer
    private SpriteRenderer sr;
    //新建一个数组存放四个方向的图片,数组内容可以在unity内拖入
    public Sprite[] tankSprite;
	void Start ()
    {
        //获取player的属性
        sr = GetComponent<SpriteRenderer>();
	}
	
	// Update is called once per frame
	void Update ()
    {
        //获取水平方向的移动
        float h = Input.GetAxisRaw("Horizontal");
        if (h < 0)//改变play的sprite 更换图片转向
        {
            //左
            sr.sprite = tankSprite[3];
        }
        else if (h > 0)
        {
            //右
            sr.sprite = tankSprite[1];
        }
        //x轴 * 移动方向 * 移动速度 * 按照时间移动,按照世界坐标
        transform.Translate(Vector3.right * h * moveSpeed * Time.deltaTime,Space.World);
        //获取垂直方向的移动
        float v = Input.GetAxisRaw("Vertical");
        transform.Translate(Vector3.up * v * moveSpeed * Time.deltaTime,Space.World);
        if (v < 0)
        {
            //下
            sr.sprite = tankSprite[2];
        }
        else if (v > 0)
        {
            //上
            sr.sprite = tankSprite[0];
        }
    }
}
 【来源:https://python.iitter.com/other/63611.html,转载请注明】
使用Math.Lerp让物体在一定范围内移动,该函数也适用于让数值在一定范围内浮动
using UnityEngine;
public class Example : MonoBehaviour
{
    // animate the game object from -1 to +1 and back
    public float minimum = -1.0F;
    public float maximum =  1.0F;
    // starting value for the Lerp
    static float t = 0.0f;
    void Update()
    {
        // animate the position of the game object...
        transform.position = new Vector3(Mathf.Lerp(minimum, maximum, t), 0, 0);
        // .. and increase the t interpolater
        t += 0.5f * Time.deltaTime;
        // now check if the interpolator has reached 1.0
        // and swap maximum and minimum so game object moves
        // in the opposite direction.
        if (t > 1.0f)
        {
            float temp = maximum;
            maximum = minimum;
            minimum = temp;
            t = 0.0f;
        }
    }
}
update函数更新的时间
边界 collider
触发器 得分等事件 Ontigger
生成object instantiate
void update(){ if(Time.time >nextTime) { nextTime = Time.time + colddown; vector3 changePosition = transform.position; changePosition.y += Random.Range(-3f,3f); Instantiate(objectx, changePosition, transform.rotation); } }
onColiision
audio source
Destroy 如果随机生成的物品太多记得销毁前面生成出来的,例如在10s后销毁
void start(){ Destroy(gameobject, 10f); }
 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号