Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

The bullet shoots from the wrong place

Discussion in 'Scripting' started by DrBibop, Oct 13, 2015.

  1. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    I followed a script tutorial for bullets. It shoots well but shoots from the left of my gun. I tried to fix this by changing the transform.position but it won't follow the rotation. For example, I said to move it slightly to the right so that the bullet shoots from the gun (Line 12). But when I turn to the left, the bullet will shoot from behing the gun and when I turn to the right, the bullet will shoot from the front of the gun and etc. I want that the position change follows my rotation so it always shoot from the gun.

    Here's the script :

    Code (JavaScript):
    1. var projectile : Rigidbody;
    2. var speed = 20;
    3. function Start () {
    4. }
    5. function DestroyProjectile ()
    6. {
    7.      yield WaitForSeconds(6);
    8.      Destroy(gameObject.Find("rubberBullet(Clone)"));
    9. }
    10. function Update ()
    11. {
    12.      shootLocation = transform.position - Vector3(-0.95, 0.1, 0);
    13.      if(Input.GetMouseButtonDown(0))
    14.      {
    15.          var InstantiatedProjectile : Rigidbody = Instantiate(projectile, shootLocation, transform.rotation);
    16.          InstantiatedProjectile.velocity = transform.TransformDirection(Vector3(0, 0, speed));
    17.          DestroyProjectile();
    18.      }
    19. }
    Can you help me?
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Is the shoot location an empty game object that is childed to the character/gun?
     
    DrBibop likes this.
  3. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    the shoot location is the transform position of the gun. I'll try the empty game object trick.
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Make sure it is out from the gun a bit so the collider on the bullet doesn't overlap with the gun otherwise it will react with the collider on the player/gun
     
  5. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    Yes! The empty game object trick worked! By the way the script says to ignore the player collider. My gun doesn't have a collider yet but I'll do the same thing for the gun collider.