【发布时间】:2020-06-10 08:31:06
【问题描述】:
这个想法是让物体像直升机一样从地面上升。 我通过将 transform.position.y 保存到 y 变量来解决这个问题的方式,但是当我使用 transfrom.translate 更改其位置时它显示错误。 这是我正在使用的代码。请帮忙
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement: MonoBehaviour
{
[SerializeField] private float _speed = 5;
void Start()
{
transform.position = new Vector3(0, 0, 0);
}
void Update()
{
Movement();
}
public void Movement()
{
float y = transform.position.y;
float horizontalInput = Input.GetAxis("Horizontal");
float HorizontalInput = horizontalInput * _speed * Time.deltaTime;
float verticalInput = Input.GetAxis("Vertical");
float VerticalInput = verticalInput * _speed * Time.deltaTime;
transform.position = transform.position + new Vector3(HorizontalInput, y, VerticalInput);
if(Input.GetKey(KeyCode.Space))
{
y = transform.Translate(Vector3.up * _speed * Time.deltaTime);
y++;
}
}}
【问题讨论】:
标签: c# unity3d game-development