Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Basic question about moving an object at a constant speed.

Discussion in 'Scripting' started by saucer78, Aug 17, 2014.

  1. saucer78

    saucer78

    Joined:
    Jul 6, 2014
    Posts:
    66
    So new to scripting, so lost.

    I'm sure this is day 1 stuff, but can someone help me apply this Transform.Forward script to some mesh clouds so that they slowly coast along the sky?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour {
    5.     void Example() {
    6.         rigidbody.velocity = transform.forward * 10;
    7.     }
    8. }
    I've tried using it as-is, but get no effect when I hit play. I do have a rigidbody assigned to the mesh, with no gravity, no drag, and is kinematic. I would assume I simply change the "10" variable in the script to adjust the speed... but that does nothing. Am I supposed to be changing other pieces of the code to suit my needs?

    (I can move the clouds with the appropriate combo of Constant Force and Drag but have read that the less physics one uses, the better on performance. I have several pieces I'll be wanting to move like this, so an understanding of the Transform scripting will be beneficial to me.)

    Thanks!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    You need to use Update, and Time.deltaTime for the speed calculation. The "Example" function never runs because nothing ever calls it. The mesh shouldn't have a rigidbody because presumably nothing would actually collide with the clouds, so they wouldn't need a collider. You should make "10" a public variable so you can adjust the speed in the inspector instead of having to change and recompile the code.

    --Eric
     
  3. saucer78

    saucer78

    Joined:
    Jul 6, 2014
    Posts:
    66
    Thanks for the reply Eric, but you're talking a little over my head. I guess what I'm most confused about is why the Unity Scripting API provides snippets like this if they're incomplete or don't highlight the necessary variables. (I'm sure more knowledgeable people - like yourself - can look at the code and immediately figure out what needs to be adapted to each project, but for guys like me, it took 30 minutes of researching just to figure out that I had to change ExampleClass to the name of the script file to have the darn thing even apply to an object.) :confused:

    If Time.deltaTime is necessary for this script to work, then why isn't it found in the Transform.Forward page? I guess I'm just so far behind, I can't understand how the Scripting API functions in itself. I assumed it was a bit of a snippet library, with pieces of code ready to be inserted into projects for the desired effect.
     
  4. saucer78

    saucer78

    Joined:
    Jul 6, 2014
    Posts:
    66
    Okay, I'm getting this now... Transform.Forward is simply a bit of shorthand in place of a full vector input. I thought it was a command for moving an object along the Z axis, but that will take more scripting! The examples shown on the page I linked to are just that: examples, not ready-to-go scripts.

    So I guess I need to find/learn a script that can utilize Transform.Forward... but even that is not necessary, since Vector3(0, 0, 1) could be used just as easily.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Code snippets are there to provide an example of usage; by nature they aren't usually complete scripts.

    Because it's not necessary for Transform.forward to work. The first example on the page just moves the object forward by 10 units. It's necessary for what you want—moving an object at a constant speed—but sadly the docs don't know what you want to do. ;) (Similar to the eternal coder's complaint: "Do what I want, you stupid computer, not what I'm telling you to do!!!!!")

    --Eric