Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

[SOLVED] Fire Projectile towards last position of player and make it continue in that direction

Discussion in 'Physics' started by transparo21, Feb 11, 2017.

  1. transparo21

    transparo21

    Joined:
    Jan 27, 2017
    Posts:
    4
    Hi guys. OK, i managed to make a script that makes an enemy shoot a projectile at last player position. The projectile will hit the player if he is standing still but will miss if he moves, giving him a chance. However, the projectile stops at the target position and stay there.

    My problem is how to make the projectile move towards last player position and continue moving in that direction indefinitely?

    I want to avoid using rigidBody if possible because the projectile gets stuck in objects and causes weird interaction with the other enemies.

    Code (CSharp):
    1.      
    2.     void Start()
    3.     {
    4.        // Get last know player position, y is +1 so the bullet appears to "float above ground a bit"
    5.        lastPos = new Vector3(player.transform.position.x, player.transform.position.y+1,          player.transform.position.z);[/INDENT]
    6.     }
    7.  
    8.     // Update is called once per frame
    9.     void Update()
    10.     {
    11.        // Move the projectile towards player's last position
    12.      transform.position = Vector3.MoveTowards(transform.position, lastPos, speed * Time.deltaTime);
    13.  
    14.     }
    Thanks in advance.
     
  2. IsaiahKelly

    IsaiahKelly

    Joined:
    Nov 11, 2012
    Posts:
    333
    Vector3.MoveTowards moves the bullet towards the player's last knwon position, what you want is to move it in the player's last known direction. This should work:

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         // Add + 1 to player's last known position so bullet appears to float above ground.
    4.         Vector3 playerPos = new Vector3(player.position.x, player.position.y + 1, player.position.z);
    5.  
    6.         // Aim bullet in player's direction.
    7.         transform.rotation = Quaternion.LookRotation(playerPos);
    8.     }
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         // Move the projectile forward towards the player's last known direction;
    14.         transform.position += transform.forward * speed * Time.deltaTime;
    15.     }
     
  3. transparo21

    transparo21

    Joined:
    Jan 27, 2017
    Posts:
    4
    Thanks Isaiah =). Glad to receive a reply, especially when starting out in Unity and seeking advice in these forums.

    I am using navmesh for my enemies to move towards my player so they are already facing the playing and line 14 works perfectly.

    I will keep the Quaternion.LookRotation in mind in case I need to objects to "aim" at the player first.

    Cheers =D
     
  4. IsaiahKelly

    IsaiahKelly

    Joined:
    Nov 11, 2012
    Posts:
    333
    Glad it helped you. :)

    The simplest method is actually Transform.LookAt:
    Code (CSharp):
    1. transform.LookAt(player.position);
    However, Quaternion.LookRotation is helpful when you want to look at a Vector3 instead of a Transform. Which I needed in the above example since I was modifying the player's position beforehand and returning a vector3 to look at instead of the transform.

    Also for simple questions like this Unity Answers might be a better place. They probably have answers to very similar questions there already and you can learn a lot by just browsing it.

    Best of luck with Unity!
     
    Last edited: Feb 16, 2017
  5. SlappyTheMonkeyBoy

    SlappyTheMonkeyBoy

    Joined:
    Nov 20, 2018
    Posts:
    1
    this was super helpful
     
  6. unity_dvCFv3_q_Rs3-A

    unity_dvCFv3_q_Rs3-A

    Joined:
    Feb 4, 2019
    Posts:
    1
    Once again old posts have saved me.
     
  7. IsaiahKelly

    IsaiahKelly

    Joined:
    Nov 11, 2012
    Posts:
    333
    Great to see people are still finding this helpful. You can also just hit that like button to show your appreciation, instead of resurrecting an ancient tread ;)