Search Unity

Shooting bullet

Discussion in 'Scripting' started by Surgery, Dec 1, 2010.

  1. Surgery

    Surgery

    Joined:
    Dec 1, 2010
    Posts:
    3
    So, I'm trying to do a simple shooting thing. All I need it to do is send a sphere forward from the camera at left click. I have the bullet and the target both set as rigid bodies with gravity and angular drag off. Here's the script for the bullet:
    Code (csharp):
    1. var isActive:boolean = false;
    2. var playerT:Transform;
    3.  
    4. function Update () {
    5.     getInput();
    6.    
    7.     if(isActive) {
    8.         transform.position += transform.forward * 10;
    9.     }
    10. }
    11.  
    12. function getInput() {
    13.     if(Input.GetButton("Fire1")) {
    14.         transform.forward = playerT.forward;
    15.         transform.position = playerT.position;
    16.         isActive = true;
    17.     }
    18. }
    19.  
    20. function OnCollisionEnter(other : Collision) {
    21.     print("This collider is named: " + other.contacts[0].otherCollider.name);
    22. }
    playerT is the transform from the MainCamera. The problem is that after the first collision if I fire the bullet again its forward vector starts to change mid-flight. It starts correct, but then it will start curving. Sometimes it's so extreme the bullet corkscrews down the range. I'm thinking there is something about rigid bodies that I don't know about, because it didn't do this before I made the bullet a rigid body. Thanks.
     
  2. mightymao

    mightymao

    Joined:
    Oct 21, 2009
    Posts:
    108
    I think that you have to apply a forward force instead of translate. Try something like this:

    Code (csharp):
    1.  
    2. var explosionPower: float = 2000.0;
    3. //bullet.rigidbody.AddForce(transform.forward * explosionPower);
    4. rigidbody.AddForce(transform.forward * explosionPower);
    5.  
    instead of

    Code (csharp):
    1.  
    2. transform.position += transform.forward * 10;
    3.  
     
  3. Surgery

    Surgery

    Joined:
    Dec 1, 2010
    Posts:
    3
    That didn't do it. But I fixed it another way. I just reset the location of the bullet to the camera's location and set isActive to false on collision. Not exactly sure what happened, it's kind of a dead rat, but whatever. Thanks anyways.
     
  4. Surgery

    Surgery

    Joined:
    Dec 1, 2010
    Posts:
    3
    Ok, more issues I could use some help with.

    Script is attached to bullet object. I have a field variable called muzzleVel, it's a float with value 2820.

    I move the bullet via this line:
    transform.position -= transform.forward * (muzzleVel * Time.deltaTime);

    The issue I'm having is that changing the value of muzzleVel doesn't do anything. I can't imagine why this would be, there must be something I'm missing. I'll post the whole script below for reference. Thanks.

    Code (csharp):
    1. var timeDilation:float = 0.33;
    2.  
    3. var isActive:boolean = false;
    4. var playerT:Transform;
    5. var hitMarkerObj:GameObject;
    6.  
    7. var bulletCam:GameObject;
    8. var mainCam:GameObject;
    9.  
    10. var muzzleVel:float = 2820; //fps
    11. var flightDistance:float = 0.0;
    12. var RPS:float = 0.0;
    13. var twist:float = 10.0;
    14.  
    15. function Start() {
    16.     bulletCam.active = false;
    17. }
    18.  
    19. function Update () {
    20.     getInput();
    21.    
    22.     if(isActive) {
    23.         transform.position -= transform.forward * (muzzleVel * Time.deltaTime);
    24.         flightDistance += (muzzleVel * Time.deltaTime);
    25.         transform.Rotate(0, 0, (360*(RPS*Time.deltaTime)));
    26.         bulletCam.active = true;
    27.         mainCam.active = false;
    28.         bulletCam.transform.position = transform.position - Vector3(1, 0, 1);
    29.         bulletCam.transform.rotation.y = 0;
    30.         bulletCam.transform.Rotate(0, 45, 0);
    31.         if(flightDistance > 3500) {
    32.             isActive = false;
    33.             transform.position = playerT.position;
    34.         }
    35.     } else {
    36.         bulletCam.active = false;
    37.         mainCam.active = true;
    38.     }
    39. }
    40.  
    41. function getInput() {
    42.     if(Input.GetButton("Fire1")) {
    43.         transform.forward = -playerT.forward;
    44.         transform.position = playerT.position;
    45.         isActive = true;
    46.         flightDistance = 0;
    47.         RPS = (12/twist)*muzzleVel;
    48.     }
    49. }
    50.  
    51. function OnCollisionEnter(other : Collision) {
    52.     print("This collider is named: " + other.contacts[0].otherCollider.name);
    53.     var hitMarkerGO:GameObject = Instantiate(hitMarkerObj, other.contacts[0].point, Quaternion.AngleAxis(180, Vector3.up));
    54.     hitMarkerGO.transform.position.z -= 20;
    55.     transform.position = playerT.position;
    56.     isActive = false;
    57. }