Input gameobject vector3 c#

 Input类中的常用方法

bool w=Input.GetKey(KeyCode.W);//检测是否按下键盘W

Input.GetKeyDown(KeyCode.W);//表示检测按下时

    Input.GetKeyUp(KeyCode.W);//表示检测按键弹起时

Input.GetMouseButton(0);//参数0表示左键,1表示右键,2表示中键

//GetMouseUp和GetMouseDown的方法与键盘的原理类似

 if (Input.GetKeyDown("space")) print("space key was pressed");

 

 if (Input.GetKeyDown(KeyCode.Space)) print("space key was pressed");
当处理输入时推荐使用Input.GetAxis和Input.GetButton,它们允许最终用户自定义按键

 

gameObject本身上的一些常用方法

gameObject.name="old wang";

gameObject.tag = "Player";

bool active = gameObject.activeSelf;//获取游戏对象的激活状态  

gameObject.SetActive(false); //将当前游戏对象不启用

gameobj0 obj0 = gameObject.GetComponent<gameobj0>();//获取对象本身的一个其他组件

print(obj0.age);

Light l=gameObject.AddComponent<Light>(); //为当前对象添加一个组件

 

如何在一个游戏对象中查找另一个游戏对象

GameObject a;

    a = GameObject.FindGameObjectWithTag("Cube");

    print(a.name);//通过Tag来查找游戏对象

    GameObject b = GameObject.Find("Main Camera");

    b.name = "主摄像机";//通过对象名来查找游戏对象

GameObject.Destroy(a,2f); //销毁某游戏对象,2f表示销毁的延迟(2秒)

    

关于Vector3

Vector3 v=new Vector3();//vector3是用来表示三维向量的一个类

    float x= v.x;

    float y= v.y;

    float z= v.z;//xyz分别为三个方向上的分量

    v=Vector3.up;//表示世界坐标系中 y轴正方向的单位向量

    v = Vector3.down;//表示世界坐标系中 y轴负方向的单位向量

    v = Vector3.right;//表示世界坐标系中 x轴正方向的单位向量

    v = Vector3.left;//表示世界坐标系中 x轴负方向的单位向量

    //forward为z轴正方向,back为z轴负方向,zero表示原点

    v.Normalize();//将v变为v的单位向量

    v=v.normalized;//返回一个v方向的单位向量,v本身不变

    float l = v.magnitude;//获取v的长度

    float a = Vector3.Angle(v1, v2);//求两个向量的夹角,返回值无正负

    float d=Vector3.Distance(v1,v2);//求两个向量终点的距离

    Vector3.Dot(v1, v2);//点乘两个向量

    Vector3.Cross(v1,v2);//叉乘两个向量

转载于:https://www.cnblogs.com/wshyj/p/6094530.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public void Move() { int stt = sun; scoreText.text = stt.ToString(); if (Input.GetKey(KeyCode.W)) { direction = 0; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[2]; this.gameObject.transform.Translate(Vector3.up * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.S)) { this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[0]; direction = 2; this.gameObject.transform.Translate(Vector3.down * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.D)) { direction = 3; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[1]; this.gameObject.transform.Translate(Vector3.right * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.A)) { direction = 1; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[3]; this.gameObject.transform.Translate(Vector3.left * speed * Time.deltaTime); } // 播放脚步声 if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) { if (!audioSource.isPlaying) { audioSource.clip = footstepSound; audioSource.loop = true; audioSource.Play(); } } else { audioSource.Stop(); } // 攻击 if (Input.GetMouseButtonDown(0)) { // 处理鼠标左键点击事件 GameObject ins = Instantiate(bulletPrefab); Vector2 clickPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); ins.GetComponent<Blogs>().clickPosition = clickPosition; if (direction == 0) { ins.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y + 0.15f); } else if (direction == 2) { ins.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y - 0.15f); } else if (direction == 3) { ins.transform.position = new Vector3(this.gameObject.transform.position.x + 0.15f, this.gameObject.transform.position.y); } else if (direction == 1) { ins.transform.position = new Vector3(this.gameObject.transform.position.x - 0.15f, this.gameObject.transform.position.y); } } }public void OnCollisionEnter2D(Collision2D collision) { if(collision.gameObject.tag == "GoldCoin") { Destroy(collision.gameObject); sun = sun + 1; if (!audioSource.isPlaying) { audioSource.clip = shique; audioSource.loop = true; audioSource.Play(); } } else { audioSource.Stop(); } //判断是否遭受攻击 if (collision.gameObject.tag == "zhi") { Destroy(collision.gameObject); hp = hp - 1; } // 检测碰撞物体的标签 if (collision.gameObject.tag == "zhi2") { // 忽略该碰撞 Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>()); } }优化代码
05-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值