简体   繁体   中英

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. 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. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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