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

Shooting at player

Discussion in 'Scripting' started by Izzy4me, Aug 14, 2014.

  1. Izzy4me

    Izzy4me

    Joined:
    Aug 14, 2014
    Posts:
    8
    Hello! I wanted to make basic visual effect of incoming bullets (of course slower than bullets from machine gun in real world :) ) coming from bad cubes to player.

    Here is my "try code" - moving and shooting part:

    Code (csharp):
    1.  
    2. public float minDistance = 3;
    3. public float maxDistance = 15;
    4.  
    5. void Update () {
    6.  
    7.      float distance = Vector3.Distance(myCharacter.position, player.position);
    8.    
    9.      if (distance <= maxDistance && distance >= minDistance) {
    10.  
    11.          transform.LookAt (player);  
    12.          myCharacter.rotation = Quaternion.Slerp (myCharacter.rotation, Quaternion.LookRotation  (player.position - myCharacter.position), rotationSpeed * Time.deltaTime);
    13.      transform.Translate (moveSpeed * Vector3.forward * Time.deltaTime);
    14.      }
    15.  
    16.      if (distance <= 20 && distance >= 7 ) {
    17.               Shoot ();
    18.      }
    19. }
    20.  
    21.  
    22. public int count = 1;
    23. public float forceValue = 1200;
    24. public Rigidbody bulletPref;
    25. Transform bulletPos;
    26.  
    27. void Shoot(){
    28.      int freq = 90;
    29.      if (count % freq == 0) {
    30.             transform.LookAt(player);
    31.             Rigidbody bullet = Instantiate (bulletPref, myCharacter.position, myCharacter.rotation) as Rigidbody;
    32.             bullet.rigidbody.AddForce (myCharacter.forward* forceValue );
    33.      }
    34.      count++;
    35.  }
    36.  
    37. }
    38.  
    39.  
    But it is working only sometimes. At other time bullet is going in different direction (180 degrees wrong). What can I do? I must use Raycast?

    Thx for any advice or working example.
     
  2. Izzy4me

    Izzy4me

    Joined:
    Aug 14, 2014
    Posts:
    8
    Any suggestion?
     
  3. TRG96

    TRG96

    Joined:
    Mar 26, 2011
    Posts:
    102
    My guess is that myCharacter is not looking at the player and shooting the bullet in its forward direction. Is my character, a sphere or a capsule? change it to a cube or something and see which is its forward direction it shoots in. Since you are using Vector3.distance, it will fire a bullet even when its not facing the player and you are using slerp so it slowly rotates and shoots when its not facing you.

    use the dot product to get the angle and if the player is in sight then shoot.
    http://docs.unity3d.com/ScriptReference/Vector3.Dot.html
     
  4. Izzy4me

    Izzy4me

    Joined:
    Aug 14, 2014
    Posts:
    8
    myCharacter is a capsule (1st person view). I will test it tomorrow and write if my problem will be solved or will change. Thx you for a clue :)