unity -- 键盘控制+触发器

1.给玩家套上--Capsule Collider触发器 -- 最重要的是玩家需要刚体(Rigidbody)

代码方面

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private const float SPEED = 10f;
    // Start is called before the first frame update
    // Update is called once per frame
    private void Update()
    {
        HandleMovement();
    }
    private void HandleMovement()
    {
        float movex = 0f;
        float movey = 0f;

        if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
        {
            movey = +1f;
        }
        if(Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
        {
            movey = -1f;
        }
        if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            movex = -1f;
        }
        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            movex = +1f;
        }
        Vector3 moveDir = new Vector3(movex, movey).normalized;

        transform.position += moveDir * SPEED * Time.deltaTime;
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log("Trigger!");
    }
}

2.材质球---先找贴图--然后创建材质球--在反射率选择贴图 --最终将材质球给物体

3. 查找其他的组件--- 起名字 = gameObject.GetComponent<组件>()

start上方要写一个 private 组件 起个名字

update下面调用的时候-- 起的名字.函数

Transform.Translate(Vector3,space)

Transform.Rotation(Vector.up, 90)

Ridgidbody.Moveposition(transform.position+vactor3)

4.刚体

1).Rigidbody.ADDForce(Vector3, ForceMode)--力量方向--世界坐标系

2).Rigidbody.AddRelativeForce(Vector3.forward*10 , ForceMode.Force);--角色自身坐标系

5.Fixupdate--所有物理相关的都要在fixupdate--区别就是一秒50次--update是一帧一次

6.刚体碰撞显示 private void OnCollisionEnter(Collision collision)
    {
        //只要碰撞就会调用
    }

同理--还有OnCollisionExit

还有 OnCollisionStay--相撞状态--可以使用If(collision.gameobject.name == "")

7.触发器--OnTriggerEnter 

8.查找其他游戏物体的函数

GameObject.Find("物体对象").GetComponet<这个对象上的组件>();

9.通过标签查找模型

前面要 private GameObject[] m_标签

m_标签 = GameObject.FindGameObjectsWithTag("标签")

用for循环遍历返回的数组

for(int i = 0; i< m_标签.Lengyh;  i++)

{

        Debug.Log(m_标签[i].name);}

当你找到标签--你可以用一个函数直接控制标签里面的东西--比如

m_标签[i].GetComponet<组件>().函数 Translate(Vector3.up, spcae.self);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值