获取组件与移动
程序员文章站
2022-03-21 23:46:19
...
获取组件
移动脚本
移动原理
移动原理02
移动脚本2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour {
private Rigidbody ri;
public float speed = 5.0f;
// Use this for initialization
void Start () {
ri = this.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update ()
{
if (Input.GetKey(KeyCode.W))
{
ri.MovePosition(transform.position + Vector3.forward * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
ri.MovePosition(transform.position + Vector3.back * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
ri.MovePosition(transform.position + Vector3.left * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
ri.MovePosition(transform.position + Vector3.right * speed * Time.deltaTime);
}
}
void OnCollisionEnter(Collision coll)//被碰撞的形参
{
if (coll.gameObject.tag == "Player")//如果碰到标签为"Player"的物体,就销毁它
{
Destroy(coll.gameObject);
}
}
}
上一篇: 设计模式(三)The_Decorator_Pattern(装饰者模式)
下一篇: 初探埋点系统