Unity 3D 小球移动
1.创建一个小球命名为player
2.创建c#脚本
3.将c#脚本命名为move1双击打开输入以下代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class move1 : MonoBehaviour
{
public Rigidbody rd;
public Text wintext;
public Text scoretext;
public int score = 0;
private bool isground = false;
private Transform player;
void Start()
{
rd = GetComponent<Rigidbody>();
player = transform;
}
void Update()
{
if (Input.GetKey(KeyCode.W))
{
player.Translate(Vector3.right * 5.0f * Time.deltaTime);
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.LeftShift))
{
player.Translate(Vector3.right * 8.0f * Time.deltaTime);
}
}
if (Input.GetKey(KeyCode.S))
{
player.Translate(Vector3.left * 5.0f * Time.deltaTime);
if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.LeftShift))
{
player.Translate(Vector3.left * 8.0f * Time.deltaTime);
}
}
if (Input.GetKey(KeyCode.A))
{
player.Translate(Vector3.forward * 5.0f * Time.deltaTime);
if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.LeftShift))
{
player.Translate(Vector3.forward * 8.0f * Time.deltaTime);
}
}
if (Input.GetKey(KeyCode.D))
{
player.Translate(Vector3.back * 5.0f * Time.deltaTime);
if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.LeftShift))
{
player.Translate(Vector3.back * 8.0f * Time.deltaTime);
}
}
4.给小球添加组件Rigidbody
5.给小球添加组件Move1(脚本)并在脚本、Rd选好脚本和Rd