我正在创建一个应用程序,当按下w来加速它时,转动一个球体就是地球,而s用于减速它。但是我无法知道如何提高速度。这是我的代码:
using UnityEngine;
using System.Collections;
public class Spin : MonoBehaviour{
public float speed;
void Update ()
{
transform.Rotate(Vector3.up,speed * Time.deltaTime);
if (Input.GetKey ("escape")) {
Application.Quit();
}
if (Input.GetKey ("w")) {
transform.Rotate(Vector3.up,speed + 1);
}
if (Input.GetKey ("s")) {
transform.Rotate(Vector3.up,speed - 1);
}
}
}
有人知道如何提高按钮按下的时间吗?
P.S我希望用c#写
答案 0 :(得分:1)
请原谅我对Unity缺乏了解,但看起来你的if语句不会更新speed
的值。就像现在一样,如果按下键,则只能以speed + 1
的值旋转。但速度永远不会改变。
也许尝试这样的事情?
if (Input.GetKey("w"))
{
transform.Rotate(Vector3.up, speed + 1);
speed++;
}
答案 1 :(得分:1)
void Update ()
{
transform.Rotate(Vector3.up,speed * Time.deltaTime);
if (Input.GetKey ("escape")) {
Application.Quit();
}
if (Input.GetKey ("w")) {
transform.Rotate(Vector3.up,speed++);
}
if (Input.GetKey ("s")) {
transform.Rotate(Vector3.up,speed--);
}
}
如果您使用speed++
,则speed
将使用transform.Rotate
作为++speed
方法的参数,只会将增加1 强>
另一方面,如果您使用speed--
,则首先增加它,然后将其用作参数。
同样适用于--speed
和import pygame
pygame.joystick.init()
while True:
joystick_count = pygame.joystick.get_count()
for i in range(joystick_count):
joystick = pygame.joystick.Joystick(i)
joystick.init()
name = joystick.get_name()
axes = joystick.get_numaxes()
hats = joystick.get_numhats()
button = joystick.get_numbuttons()
joy = joystick.get_axis(0)
print (name,joy)
。
答案 2 :(得分:1)
你需要的是一种加速。请记住,每个帧都会调用Update
,并且帧速率会发生变化。
因此,在脚本中添加acceleration
字段。然后将速度增加两帧之间deltaTime
的加速度。
void Update () {
if (Input.GetKey ("escape")) {
Application.Quit();
}
// calculate new speed
if (Input.GetKey ("w")) {
speed += acceleration*Time.deltaTime;
}
else if (Input.GetKey ("s")) {
speed -= acceleration*Time.deltaTime;
}
// apply speed
transform.Rotate(Vector3.up, speed*Time.deltaTime);
}
请注意,由于您使用此速度进行轮换,因此speed
的单位为度/秒,acceleration
的单位为度/(s ^ 2)。