控制小球代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class lllll : MonoBehaviour
{
// Start is called before the first frame update
public Rigidbody rg;
private bool isground = true;
private Transform player;
void Start()
{
rg = GetComponent<Rigidbody>();
player = transform;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.W))
{
player.Translate(Vector3.forward * 5.0f * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
player.Translate(Vector3.back * 5.0f * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
player.Translate(Vector3.left * 5.0f * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
player.Translate(Vector3.right * 5.0f * Time.deltaTime);
}
//空格=space
if (Input.GetKeyDown(KeyCode.Space) && isground)
{
rg.AddForce(Vector3.up * 200);
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == "ground")
{
isground = true;
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.collider.tag == "ground")
{
isground = false;
}
}
}