Search Unity

How to move the position of the Z axis of my ship?

Discussion in 'Scripting' started by lucasmees, Oct 19, 2014.

  1. lucasmees

    lucasmees

    Joined:
    Sep 21, 2014
    Posts:
    18
    Hello I would like to know how to move the position of the Z axis of my ship?
    I'm using:
    transform.Translate(0,0,speed * Time.deltaTime);​
    But he did not throw in the position where my ship should point in front .

    Please i am beginner and the examples I got did not help me .

    image of my project
    http://prntscr.com/4xth63
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I think you're asking: how can you make your ship move forward, no matter what direction your ship may be pointing?

    But in that case, your code should already do it; Transform.Translate by default interprets the parameters relative to the object.

    So maybe you want the opposite — you want to always move in the +Z direction, no matter which way your ship is facing? In that case, just add one more parameter:

    Code (csharp):
    1.    transform.Translate(0, 0, speed * Time.deltaTime, Space.World);
    and this tells Unity that you're trying to move it in world coordinates, rather than local coordinates.
     
  3. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    transform.forward ought to do the trick?
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    No, he's already translating forward. I think what he wants is to not translate forward, but to always translate in the +Z direction regardless of which way it's facing. There are lots of ways to do that, of course; the one I showed above I think is the simplest fix for his current code, but if it were me, I'd probably just do:
    Code (CSharp):
    1. transform.position += Vector3.forward * speed * Time.deltaTime;
    which would accomplish the same thing.
     
  5. lucasmees

    lucasmees

    Joined:
    Sep 21, 2014
    Posts:
    18
    Well , I just want to do my object which is actually the bullet my ship shoot in the direction where the spacecraft pointing .

    the beak of the ship must shoot a bullet forward independent of the rotation of my ship .


    and referring to your code I'll take a look here .
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Ah. The easiest way to do this is to first copy the transform from the ship into the bullet, and then move the bullet forward a bit (so it appears in front of the ship instead of in the middle of the ship). Something like:

    Code (CSharp):
    1. GameObject bullet = bulletPrefab.Instantiate() as GameObject;
    2. bullet.transform.position = transform.position;
    3. bullet.transform.rotation = transform.rotation;
    4. bullet.Translate(0, 0, 2);
    That's off the top of my head, and may contain errors, but it should be approximately correct.
     
  7. lucasmees

    lucasmees

    Joined:
    Sep 21, 2014
    Posts:
    18
    code the direction of cloned projectiles .
    Code (JavaScript):
    1. #pragma strict
    2. var speed : int = 10;
    3.  
    4. function Start () {  
    5. }
    6.  
    7. function Update () {      
    8.         transform.Translate(0, 0, speed * Time.deltaTime, Space.World);
    9.         Destroy(gameObject,4);
    10.    
    11. }
    the methods you told me he did shoot forward but only shoots forward , he still shoots where my ship is pointing , just follow where it goes but it does not shoot where you aim.

    this is really weird cuz all the examples I got the scripts are the same thing and shoot .

    code instantiation of missiles on the ship .
    Code (JavaScript):
    1. #pragma strict
    2. var PTiro: GameObject; // Ptiro = prefap of bullet
    3. function Start () {
    4.  
    5. }
    6.  
    7. function Update () {
    8.     if (Input.GetKeyDown("space")){
    9.         Tiro();
    10.     }
    11. }
    12.  
    13. function Tiro(){
    14.     Instantiate(PTiro,transform.position,transform.rotation);
    15.    
    16. }
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Sorry, I'm having trouble understanding you. Which is it that you want?

    1. The bullets go in the +Z direction, no matter which way the ship is facing. The ship could be pointing up, left, sideways, it doesn't matter; the bullets always advance down +Z.

    ...OR...

    2. The bullets go in the direction the ship is facing. So if the ship is facing up, the bullets fly up; if the ship is facing to the left, the bullets fly to the left, etc.

    Please specify which you're trying to achieve, and then maybe we can help you make that happen!
     
  9. lucasmees

    lucasmees

    Joined:
    Sep 21, 2014
    Posts:
    18
    2. The bullets go in the direction the ship is facing.
     
  10. lucasmees

    lucasmees

    Joined:
    Sep 21, 2014
    Posts:
    18
    currently my code like this :
    Code (JavaScript):
    1. #pragma strict
    2. var PTiro: GameObject;
    3. var speed : int = 10;
    4. function Start () {
    5.  
    6. }
    7.  
    8. function Update () {
    9.     if (Input.GetKeyDown("space")){
    10.         Tiro();  
    11.         transform.position += Vector3.forward * speed * Time.deltaTime;
    12.     }
    13. }
    14.  
    15. function Tiro(){
    16.     Instantiate(PTiro,transform.position,transform.rotation);
    17. }
     
  11. lucasmees

    lucasmees

    Joined:
    Sep 21, 2014
    Posts:
    18
    Hello JoeStrout, posted shortly after I did the update of my Unity and done an update of Windows7 that I have until now a Feedback.
    "Your 64 bit Windows installation is missing an important service pack patch. Please apply http://support.microsoft.com/kb/976038 to Ensure stability."

    But now the shot is running after you modify some things.

    here is the code below:
    Code (JavaScript):
    1. #pragma strict
    2. var speed : int = 10;
    3. function Start () {  
    4. }
    5.  
    6. function Update () {
    7.         transform.Translate(0,0,speed * Time.deltaTime);
    8.        
    9.         Destroy(gameObject,4);
    10. }
    I have to apologize for the inconvenience, maybe I've confused me in the play scripts, since this code is to move the bullet. But do not know why with the other method did not work