首页 > 解决方案 > 尝试使用 C# 转换游戏对象统一的位置

问题描述

在尝试更改游戏对象的位置及其速度时,我需要一些帮助。我知道翻译游戏对象,但我不知道如何提高它的翻译速度。顺便说一句,我正在使用统一游戏引擎。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class lightning : MonoBehaviour
{
    //This is lightning
    private GameObject name;

    // Start is called before the first frame update
    void Start()
    {
       name=GameObject.Find("car");
    }

    // Update is called once per frame
    void Update()
    {
     if (Input.Keydone("r")
       door.transform.Translate(-Vector3.right * Time.deltaTime);
    }
}

标签: c#unity3dlight

解决方案


听起来您在速度和加速度方面遇到了问题。
我添加了一个速度变量和一个加速度变量,这两个变量可以使物体随着时间的推移越来越快。

由于这是物理学,我建议您在游戏对象上使用刚体并操纵其速度或施加力(力 = 质量 * 加速度),因此通过添加力它会给对象一个加速度。

//This is lightning
private GameObject name;
private float speedFactor = 20;
private float accelearationFactor = 5;

// Start is called before the first frame update
void Start()
{
   name=GameObject.Find("car");
}

// Update is called once per frame
void Update()
{
 if (Input.Keydone("r") {
   door.transform.Translate(-Vector3.right * speedFactor * Time.deltaTime);
   speedFactor += accelearationFactor * Time.deltaTime;
 }
}

推荐阅读