Search Unity

Shoot Bullet in the direction the canon is facing

Discussion in 'Scripting' started by gibson79, Feb 14, 2016.

  1. gibson79

    gibson79

    Joined:
    Feb 14, 2016
    Posts:
    4
    Hi, i´ve got a canon that i can rotate around its Z Axis. That Works fine.
    In the canons center i instantiate a bullet (just a ball). Now how to get the bullet to exactly the same direction like the canon.

    my Instantiation, where "this" is the canon and "go" is the bullet

    Code (CSharp):
    1. go=Instantiate(Resources.Load("Ball-blau"))asGameObject;
    2. go.transform.position=newVector3(this.gameObject.transform.position.x,
    3. this.gameObject.transform.position.y,
    4. this.gameObject.transform.position.z);
    i am adding relative force which works almost, but sometimes it goes a bit in a different direction:
    Code (CSharp):
    1. rbBall.AddRelativeForce (new Vector3(0f,touch0.position.y,0f));


    see Image below
    help.png
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    1st thing 1st, this:
    Code (CSharp):
    1. go.transform.position = new Vector3 (this.gameObject.transform.position.x,
    2. this.gameObject.transform.position.y,
    3. this.gameObject.transform.position.z );
    is identical to this:
    Code (CSharp):
    1. go.transform.position = transform.position;
    Now on to rotation. I am assuming this is a 2D game which is why the rotation occurs on the Z axis.
    You can use the cannon's transform.up to get your desired shooting direction. The script should look something like this:
    Code (CSharp):
    1. bulletRb.AddForce(cannon.transform.up * bulletForce);
    2. //OR
    3. bulletRb.velocity = cannon.transform.up * bulletSpeed;
    If it still does not shoot in the desired direction, the problem might have to do with the way you are rotating the cannon, or the actual art of the cannon being lopsided, among a whole other host of potential causes
     
    Last edited: Feb 14, 2016
    gibson79 likes this.
  3. Rida2

    Rida2

    Joined:
    Jan 11, 2016
    Posts:
    35
    I had this issue- solved it by creating an intermediary parent- which allows you to correct the direction freely to match, without upsetting the canon
     
  4. gibson79

    gibson79

    Joined:
    Feb 14, 2016
    Posts:
    4
    I cant believe it, it works!!! Why did you take the canons transform.up??
    Thx Laperen
     
  5. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    transform.up is basically a conversion of the object's green axis facing direction from Local to Global. Not that its changing the facing direction, but is calculating what the facing direction would be in Global. You can do the same with all the other pre-defined directions such as Down, Forward, Back, Left, and Right, if you need stuff to go in those other directions.

    AddRelativeForce should have done what you intended really. Maybe defining the direction yourself and applying it to AddForce is more precise.
     
  6. gibson79

    gibson79

    Joined:
    Feb 14, 2016
    Posts:
    4
    I tried many things like vector3.transform.forward etc..
    Maybe the mistake was that i tried vector3.transform.up?
     
  7. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Even if I give you a brief answer, you probably still will not understand. The entirety of the topic is "Euclidean Vector" in math. I highly recommend you to read up a little on it. even a Wikipedia search will be greatly helpful for you in the long run.
     
  8. gibson79

    gibson79

    Joined:
    Feb 14, 2016
    Posts:
    4
    Yes, i agree! Thank you!!