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);