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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

World Space axis to Local Space

Discussion in 'Scripting' started by ddulshan, Oct 4, 2014.

  1. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Hi, so I'm trying to make a RPG launcher to shoot the projectile. I've attached a Rigidbody to projectile. The projectile is a child of the Main RPG prefab. I need it to move forward on its local axis (Way where the camera is directed) but it moves forward in the World Space. So I need to know how to move it through Local Axis. Here's the code, Thanks.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var projectile : Rigidbody;
    4. var speed : int = 500;
    5.  
    6. function Update ()
    7. {  
    8.      if(Input.GetButton("Fire1"))
    9.      {
    10.           projectile.transform.parent = null;
    11.           {
    12.                projectile.velocity = Vector3.forward;
    13.           }
    14.      }
    15. }
     
  2. aoe_labs

    aoe_labs

    Joined:
    Nov 4, 2013
    Posts:
    42
    Vector3.forward by itself is in world space. I think what you need is
    Code (JavaScript):
    1. projectile.velocity = (Vector3.forward, Space.Self)
     
  3. ddulshan

    ddulshan

    Joined:
    Mar 16, 2014
    Posts:
    190
    Hi, I tried it but I'm getting some errors.
    When I write the code like this, how you've written.
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var projectile : Rigidbody;
    4. var speed : int = 500;
    5.  
    6. function Update ()
    7. {  
    8.      if(Input.GetButton("Fire1"))
    9.      {
    10.           projectile.transform.parent = null;
    11.           {
    12.                projectile.velocity = (Vector3.forward, Space.Self);
    13.           }
    14.      }
    15. }
    I get 3 errors,
    1. Shooter.js(12,54): BCE0044: expecting ), found ','.
    2. Shooter.js(12,55): UCE0001: ';' expected. Insert a semicolon at the end.
    3. Shooter.js(12,66): BCE0043: Unexpected token: ).

    When I write like this,
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var projectile : Rigidbody;
    4. var speed : int = 500;
    5.  
    6. function Update ()
    7. {  
    8.      if(Input.GetButton("Fire1"))
    9.      {
    10.           projectile.transform.parent = null;
    11.           {
    12.                projectile.velocity = transform.Translate(Vector3.forward, Space.Self);
    13.           }
    14.      }
    15. }
    I get this,
    1. Shooter.js(12,57): BCE0022: Cannot convert 'void' to 'UnityEngine.Vector3'.

    Please help!