简体   繁体   English

尝试使用 C# 转换游戏对象统一的 position

[英]Trying to transform position of gameobject unity using C#

I need some help in trying to change the position of an gameobject and its speed.在尝试更改游戏对象的 position 及其速度时,我需要一些帮助。 I know to translate the gameObject but I don't know how to increase the speed of how it translates.我知道翻译游戏对象,但我不知道如何提高它的翻译速度。 I'm using the unity game engine by the way.顺便说一句,我正在使用统一游戏引擎。

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);
    }
}

One workaround you can use is a black filter(preferably Image) on UI layer that covers the whole screen.您可以使用的一种解决方法是在覆盖整个屏幕的 UI 层上使用黑色滤镜(最好是图像)。 You can change filter's opacity accordingly to achieve the feeling of 'dimmed' screen.您可以相应地更改过滤器的不透明度以实现“变暗”屏幕的感觉。

It sounds like you are having trouble with speed and acceleration.听起来您在速度和加速度方面遇到了问题。
I added a velocity variable and an acceleration variable with the two you can make objects go faster and faster with time.我添加了一个速度变量和一个加速度变量,这两个变量可以使对象 go 随着时间的推移越来越快。

Since this is Physics I'd recommend you to use a rigidbody on your gameobject and manipulate its speed or to apply a force (force = mass * acceleration) so by adding a force it will give the object an acceleration.由于这是物理学,我建议您在游戏对象上使用刚体并操纵其速度或施加力(力 = 质量 * 加速度),因此通过添加力,它将为 object 提供加速度。

//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;
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM