游戏物体控制方法笔记
键盘控制方向
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public Rigidbody rd;
// Start is called before the first frame update
void Start()
{
Debug.Log("1111111111111");
//rd = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
//transform.Rotate(Vector3.up * Time.deltaTime * 100);
float f = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
rd.AddForce(new Vector3(f,0,v)*1);
}
}
给prefab添加标签,检测碰幢和销毁物体
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag=="food")
{
Destroy(collision.gameObject);
}
}
触发检测
勾选collider中的istriigger,就变成了触发器
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "food")
{
Destroy(other.gameObject);
}
}
相机跟随
新建一个脚本给相机
public class FollowTarget : MonoBehaviour
{
public Transform tf;
private Vector3 offset;
// Start is called before the first frame update
void Start()
{
offset = transform.position - tf.position;
}
// Update is called once per frame
void Update()
{
transform.position = tf.position + offset;
}
}
text在vs中的命名空间
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
public Rigidbody rd;
// Start is called before the first frame update
private float score = 0;
public Text scores;
void Start()
{
// Debug.Log("1111111111111");
//rd = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
//transform.Rotate(Vector3.up * Time.deltaTime * 100);
float f = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
rd.AddForce(new Vector3(f,0,v)*2);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "food")
{
Destroy(other.gameObject);
score = score + 1;
scores.text = score+"";
}
}
}
胜利显示
要定义为gameobject,不要text,要导入相关的额库,using UnityEngine.UI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
public Rigidbody rd;
// Start is called before the first frame update
private float score = 0;
public Text scores;
public GameObject success;
void Start()
{
// Debug.Log("1111111111111");
//rd = GetComponent<Rigidbody>();
success.SetActive(false);
}
// Update is called once per frame
void Update()
{
//transform.Rotate(Vector3.up * Time.deltaTime * 100);
float f = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
rd.AddForce(new Vector3(f,0,v)*2);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "food")
{
Destroy(other.gameObject);
score = score + 1;
scores.text = score+"";
if (score>4)
{
success.SetActive(true);
}
}
}
}
更改指定text组件的内容
GameObject.Find(“Canvas/playerBlood”).GetComponent
按键执行攻击
攻击相关
怪物闪两下消失
动画帧绑定事件函数
触发器
两个物体都要添加regibody才能出发OnTriggerEnter
转向敌人
Vector3 lookPos = target.transform.position - transform.position;
lookPos.y = 0;
Quaternion rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 30.0f);
同一水平面看向
Vector3 target=new Vector3(point.x,transform.position.y,point.z);
transform.LookAt(target);
寻找某一类敌人
respawns = GameObject.FindGameObjectsWithTag("Respawn");
判断小于某个距离时执行
Vector3 offset = target.position - transform.position;
float sqrLen = offset.sqrMagnitude;
if (sqrLen < 1 * 1)
{
monsterAnimator.SetBool("foundPlayer", false);
monsterAnimator.SetBool("attackPlayer", true);
}
else {
transform.Translate(Vector3.forward * Time.deltaTime * 1);
}
攻击执行
方法一
anim = GetComponent.<Animation>();
anim.CrossFade("Idle");
方法二
playerController.SetInteger("attack1",1);
每秒执行一次方法
private float hitPerSecond=0;
if (hitPerSecond>=1) {
Debug.Log("1");
hitPerSecond = 0;
}
hitPerSecond = hitPerSecond + Time.deltaTime;
寻找editor中的组件
GameObject.Find(“player”).GetComponent().SetBool(“hurt”,true);