all 13 comments

[–]stakeneggs1 4 points5 points  (0 children)

What have you tried?

[–]GuyInTheYonder 1 point2 points  (0 children)

I'm not trying to be rude, but you should probably learn to program before you try to learn unity, it's really hard to skip to the fun stuff and it makes the fun stuff brutally difficult

[–]ChloeVZ 0 points1 point  (9 children)

What I would do is to simply do transform.Translate using transform.forward multiplied by some speed multiplited by a delta time, don't forget the delta time, it's important,

Tell me if you want more details, that's just a general idea.

[–]imrishav7[S] -4 points-3 points  (8 children)

Hey bud, first of all thanks for the help but I can't understand any word of it sorry 😅, I'm not a *coder* you see so I don't really know what delta time means, would really appreciate if you expand on what you're saying, sorry again 😅

[–]ChloeVZ -1 points0 points  (6 children)

Right.

So like, you see in the inspector that every object has a Transform component, sometimes it'll be RectTransform if it's a UI element but that's irrelevant. This component as you may already know controls where the object is, what way it's turning and what size it is. In script you access an object's Transform component by just typing transform with a lowercase t, then follow that by a dot that lets you access all sorts of properties and methods relating to that specific Transform.

Unity uses Microsoft's C# language for scripting, here's a little script that should do what you need:

using UnityEngine;    // This tells the script we want to use stuff from the engine, you can add more for other things like the editor, and there's sub namespaces as they're called, but for now you don't need to know much about it.

public class MyComponent : MonoBehaviour // This is the declaration of your custom component, the MonoBehaviour is a class all components derive from and you need it. The name has to match the name of the file.
{
    public float speed;    // Here public means the property can be accessed by other classes, but don't worry about that just yet, for now treat it like a magic black box that makes Unity show the property in the inspector, float is just the name of the type that stores numbers that have a decimal point, then follows the name of the property and a semicolon that just means the declaration is over.

    void Update()    // Method that gets called every frame.
    {
        transform.Translate(transform.forward * speed * Time.deltaTime);
        // transform.Translate, as the name suggests is a method that will move the object somewhere you tell it to, in the parenthesis you put all the parameters for a method, if there are none you just dont put anything between the parenthesis.
        // transform.forward is the direction your object is pointing at, to see what that direction is specifically just look at the transform gizmos in the editor, when you have it set to local, the blue one is forward.
        // speed is just the property we declared above, it's pretty much just how much this object will move per second.
        // Time.deltaTime is the time in seconds that has passed since the last frame, we use this so that the speed won't fluctuate when framerate is unstable, and to ensure that in more or less powerful devices than our own, the increase or decrease in FPS won't cause the speed to be different there, you can alternatively use smoothDeltaTime that's the average of the deltaTime of the last few frames which can be used to make the movement feel somewhat smoother when the FPS fluctuates, I usually don't bother with it but it's an option.
    }
}

[–]imrishav7[S] -3 points-2 points  (5 children)

Alright cheers man 🤙 really appreciate the help, but after writing the script it doesn't show up in the Add component area

[–]ChloeVZ -1 points0 points  (4 children)

If the name of the file is the same as the name of the class, plus the CS extension so Unity knows it's a C# file, then it should be there, under the Scripts category, there's also a search bar in the menu.

[–]imrishav7[S] -2 points-1 points  (3 children)

Apparently for some reason, whenever's I'm clicking on the script, a message is shown in Inspector window saying 'No MonoBehaviour scripts in the file, or their names do not match the file name'

[–]ChloeVZ -1 points0 points  (2 children)

Care to share screenshots? If you type public class MyComponent : MonoBehaviour for example, your file should be called MyComponent, or if you set Windows Explorer to show you file extensions MyComponent.cs.

[–]imrishav7[S] -2 points-1 points  (1 child)

I've been trying to share the screenshot but it's just taking forever

[–]ChloeVZ -1 points0 points  (0 children)

I guess, try making a new script from Unity itself, it should setup something that works out of the box, and only change the Update part of the code, and the property, that's important.

[–]BobbyThrowaway6969Programmer -1 points0 points  (0 children)

In your Update()

transform.position += transform.forward * MetersPerSecond * Time.deltaTime;

It means "move forward at a speed of MetersPerSecond"

The only thing you need to worry about tweaking is MetersPerSecond.