在Unity中使用C#通过键更改玩家方向 [英] Changing player direction with keys using C# in Unity

查看:63
本文介绍了在Unity中使用C#通过键更改玩家方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用C#统一按下向左/向右键,则我一直试图使此对象自行向前移动并以圆形方式向左或向右旋转,此图将使它更清晰:

Ive been trying to make this Object move forward on its own and turn left or right in circular fashion if the left/right keys are pressed with unity using C#, this picture will make it clearer: http://prnt.sc/avmxbn

我只能使其自行移动,这是到目前为止的代码:

I was only able to make it move on its own and this is my code so far:

using UnityEngine;
using System.Collections;

public class PlayerBehaviour : MonoBehaviour {

Update(){ transform.localPosition += transform.forward *speed *Time.deltaTime

float speed = 5.0f;
// Use this for initialization
void Start () {
    Debug.Log ("it's working?");    

}

// Update is called once per frame
void Update () {

    transform.localPosition += transform.forward * speed * Time.deltaTime;


}

void FixedUpdate(){

}

}

但是,我不知道如何在圆形路径中更改左或右方向,如链接的图片所示.有人可以帮助我吗?谢谢.

I have no idea, however, how to change left or right directions in circular paths like the picture linked demonstrates. Anyone able to help me? Thank you.

推荐答案

您的代码中存在明显的错误.您调用Update()两次.第一次没有返回类型.你不应该那样做.首先删除第一个Update()和同一行中的代码.

There are obvious errors in your code. You call Update() twice. The first time without a return type. You shouldn't do that. Start by deleting the first Update() and the code on the same line.

然后,要回答您的问题,您将不得不考虑从用户那里获取输入.使用Input.GetKey()并将其传递给KeyCode值,例如KeyCode.A作为'A'键.所以你可以说:

Then to answer your question you will have to look into getting input from the user. Use Input.GetKey() and pass it a KeyCode value such as KeyCode.Afor the 'A' key. So you can say:

if(Input.GetKey(KeyCode.A))
{
    //do something(for example, Turn the player right.)
}

然后考虑使用transform.Roate旋转播放器来旋转对象.

Then look into rotating the object using transform.Roate to rotate the player.

这篇关于在Unity中使用C#通过键更改玩家方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆