我的动作脚本有什么问题?
What am I doing wrong with my movement script?
所以我对这整个统一的事情有点陌生。我用 C# 编程已经有一段时间了,通常为了移动某些东西我会做一点“_playerPosition.x += 5;”但我在 Unity 中尝试过,但它似乎不起作用。
这是我当前的移动代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
Vector2 _playerPosition;
GameObject Player;
// Use this for initialization
void Start () {
_playerPosition = Vector2.zero;
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.W)) {
_playerPosition.y += 5f;
}
if (Input.GetKey(KeyCode.S)) {
_playerPosition.y -= 5f;
}
if (Input.GetKey(KeyCode.D)) {
_playerPosition.x += 5f;
}
if (Input.GetKey(KeyCode.A)) {
_playerPosition.x -= 5f;
}
Player.transform.position = _playerPosition;
}
}
_playerPosition
是一个包含您在评论中提到的 Vector2 的变量。确保在所有计算后使用该变量设置新位置。
编辑
检查一下。请记住将您的 Player 对象从场景分配到脚本中。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
Vector2 _playerPosition;
public GameObject Player; // assign your player prefab
void Start () {
_playerPosition = Vector2.zero;
}
void Update () {
if (Input.GetKey(KeyCode.W)) {
_playerPosition.y += 5f;
}
if (Input.GetKey(KeyCode.S)) {
_playerPosition.y -= 5f;
}
if (Input.GetKey(KeyCode.D)) {
_playerPosition.x += 5f;
}
if (Input.GetKey(KeyCode.A)) {
_playerPosition.x -= 5f;
}
Player.transform.position = _playerPosition;
}
}
在你的更新中试试这个
void Update(){
int d = 0;
int speed = 5;
if (Input.GetKeyDown(KeyCode.W)) {
d=90;
}
if (Input.GetKeyDown(KeyCode.S)) {
d = 270;
}
if (Input.GetKeyDown(KeyCode.D)) {
d = 0;
}
if (Input.GetKeyDown(KeyCode.A)) {
d=180;
}
this.transform.position += new Vector3((int)(speed * Mathf.Sin(d * Mathf.Deg2Rad)), 0, (int)(speed * Mathf.Cos(d * Mathf.Deg2Rad)));
}
这是 3d 的,但可以转换为 2d
你应该简单地移动你的播放器,脚本也已附加
public float speed = 5f;
void Update()
{
if (Input.GetKey(KeyCode.W))
{
transform.position += Vector3.up * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
transform.position += Vector3.down * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.D))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
}
您可以根据需要更改 speed
的值。
所以我对这整个统一的事情有点陌生。我用 C# 编程已经有一段时间了,通常为了移动某些东西我会做一点“_playerPosition.x += 5;”但我在 Unity 中尝试过,但它似乎不起作用。 这是我当前的移动代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
Vector2 _playerPosition;
GameObject Player;
// Use this for initialization
void Start () {
_playerPosition = Vector2.zero;
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.W)) {
_playerPosition.y += 5f;
}
if (Input.GetKey(KeyCode.S)) {
_playerPosition.y -= 5f;
}
if (Input.GetKey(KeyCode.D)) {
_playerPosition.x += 5f;
}
if (Input.GetKey(KeyCode.A)) {
_playerPosition.x -= 5f;
}
Player.transform.position = _playerPosition;
}
}
_playerPosition
是一个包含您在评论中提到的 Vector2 的变量。确保在所有计算后使用该变量设置新位置。
编辑
检查一下。请记住将您的 Player 对象从场景分配到脚本中。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
Vector2 _playerPosition;
public GameObject Player; // assign your player prefab
void Start () {
_playerPosition = Vector2.zero;
}
void Update () {
if (Input.GetKey(KeyCode.W)) {
_playerPosition.y += 5f;
}
if (Input.GetKey(KeyCode.S)) {
_playerPosition.y -= 5f;
}
if (Input.GetKey(KeyCode.D)) {
_playerPosition.x += 5f;
}
if (Input.GetKey(KeyCode.A)) {
_playerPosition.x -= 5f;
}
Player.transform.position = _playerPosition;
}
}
在你的更新中试试这个
void Update(){
int d = 0;
int speed = 5;
if (Input.GetKeyDown(KeyCode.W)) {
d=90;
}
if (Input.GetKeyDown(KeyCode.S)) {
d = 270;
}
if (Input.GetKeyDown(KeyCode.D)) {
d = 0;
}
if (Input.GetKeyDown(KeyCode.A)) {
d=180;
}
this.transform.position += new Vector3((int)(speed * Mathf.Sin(d * Mathf.Deg2Rad)), 0, (int)(speed * Mathf.Cos(d * Mathf.Deg2Rad)));
}
这是 3d 的,但可以转换为 2d
你应该简单地移动你的播放器,脚本也已附加
public float speed = 5f;
void Update()
{
if (Input.GetKey(KeyCode.W))
{
transform.position += Vector3.up * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
transform.position += Vector3.down * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.D))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
}
您可以根据需要更改 speed
的值。