unity3d-游戏物体控制方法笔记

键盘控制方向

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组件的内容

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存失败,源站可能有防盗链机制,建议将图片保存下来直接上传上传(im4DZEWpTQa4-1659449636991)(G:\md\pic\1644728416734.png)(G:\md\pic\1644728416734.png)]

GameObject.Find(“Canvas/playerBlood”).GetComponent ().text = “sss”;

按键执行攻击

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9GBZO0Qv-1659449636992)(G:\md\pic\1644232985516.png)]

攻击相关

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kWHY8eNp-1659449636994)(G:\md\pic\1644233251027.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yKnTgDdI-1659449636995)(G:\md\pic\1644233455078.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EQ5ozXTY-1659449636998)(G:\md\pic\1644233696157.png)]

怪物闪两下消失

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1KB334Ly-1659449637000)(G:\md\pic\1644234083669.png)]

动画帧绑定事件函数

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AlwXrPst-1659449637003)(G:\md\pic\1644234212278.png)]

触发器

两个物体都要添加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);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值