Search Unity

Moving Cube

Discussion in 'Scripting' started by tomowale, Feb 26, 2015.

  1. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Hey guys I'm currently working on a car game (http://forum.unity3d.com/threads/help-with-3d-car-game.299031/#post-1980207) if you want a description, I wanted to know if anyone has a better script for a cube to move forward in the direction of the player, I coded one , but the cube follows the player whenever the player moves, which is not what I want , I just want it to move in a straight line. ( I also don't want the misconception that I want people telling me everything)
     
    Last edited: Feb 27, 2015
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    What do you mean by "in a straight line"?
     
  3. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Try using MyCube.transform.forward.
     
  4. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    jtsmith1287 Thanks a lot , but I don't know if it would work. I re-scripted the cube movement and it's moving fine , onl
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveFoward : MonoBehaviour {
    5.     public float moveSpeed = 10f;
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.    
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.  
    15.         transform.position += transform.forward * Time.deltaTime * moveSpeed;
    16.     }
    17. }
    y thing is it's moving backwards. Here is my code if it helps.
     
  5. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Then your thing is backward. :) If it's a cube just rotate it 180 degrees.
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    To elaborate on @jtsmith1287's point, the standard convention in Unity 3D is for "forward" to mean "in the local +Z direction". Objects should generally be modeled and imported so that this is the case, i.e., the front face of whatever it is, is facing down the +Z axis. Then transform.forward will match what you think of as "forward" for your object.

    If you have a model where this is not the case, you can either fix the model; or stick it inside a GameObject, rotated so that +Z is forward, and do all your transforms on that GameObject; or just live with the fact that your object's forward direction doesn't match transform.forward, and adjust your code accordingly.
     
  7. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    @jtsmith1287 and @JoeStrout I've fixed it , I used the rotate option and rotated it all the way around like jt said and I got it to move foward.:)
     
  8. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    I also saw a lot of other threads on this problem , but does anyone know how you spawn objects, like I know how to create spawn points , but how do you actually spawn the cube?
     
    Last edited: Feb 27, 2015
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You either use GameObject.CreatePrimitive, or you instantiate a prefab. Read about both approaches here.
     
  10. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Here is my code for my spawn points, I tried to code without looking anything up . Tell me my errors please. Also Unity gives me the infamous error CS8025 and CS1525 , so if you can help that would be great.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Spawn : MonoBehaviour {
    5.     public GameObject Cube;
    6.     public float spawnTime = 3f;
    7.     public Transform [] spawnPoints;
    8.  
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.         InvokeRepeating ("Spawn", spawnTime, spawnTime);
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update ()
    17.     {
    18.     UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)
    19.     }
    20.     {
    21.         {
    22.         }
    23.  
     
  11. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    JoeStrout likes this.
  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yeah... don't do that. Have the documentation open at all times, and refer to it frequently as you code.
     
  13. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Cool, thanks guys.
     
  14. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Ha. I didn't even see that at first. Ya don't feel like you're "winning" if you don't have to look at documentation. The guy that taught me my first language had to double check the docs to make sure he was right on things he hadn't done in a while. I have 3 computer monitors. One for unity, one for visual studio, and the other? You guessed it. A browser open with the manual, tutorials, forums, docs, etc. Usually by the end of the night I have 20 tabs open as I've looked up various things. There's a ridiculous amount to know with programming, and you'll likely never know it all. If you wanna code, you have to also wanna read a lot. Heck I just bought a AI book and these last several nights I've just been sitting here reading my book instead of coding. Read read read. Then read. Read more, read read... anyway. My point is: you're not less of a programmer for checking docs.

    PS Spell checker didn't put a squiggly line on 'wanna', and that is interesting to me.
     
    Timelog likes this.
  15. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Dang , Ok won't do that again. Lol
     
  16. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    I Looked at the documentation invoke didnt have much about it though.