Search Unity

Third person shooter - Learning project

Discussion in 'Made With Unity' started by TwiiK, Mar 29, 2009.

  1. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    I honestly don't know. :)

    I don't remember how things work in Unity.

    I want to fiddle with Unity again, but there's just so much else going on at the moment.

    Most games have this btw. Just look at how running and jumping makes you throw a grenade further in almost any first person shooter. This is because the momentum of the player is added to the grenade when it's thrown just like in real life.
     
  2. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    Ahh, problem solved. Don't use trail renderer projectiles. No matter what you set the momentum to the trail will always begin where it began and wont move and update with the position of the gun. I use a stretched out sphere now and it works fine, it wouldn't be good for collisions and damage, but it only has to look pretty and go away when it hit something or go through and time out I don't care. The drag no longer occurs when strafing/moving etc.
     
  3. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,665
    Nice game. It makes fun :D
     
  4. eric_c

    eric_c

    Joined:
    Sep 19, 2010
    Posts:
    44
    Twiik, can you upload the converted javascript package file?
     
  5. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    @CSHen042, not sure what package you're talking about.

    A lot of people have been asking me by PM about how I handle the aiming in my game. So I'll answer it once and for all here and encourage future questions or requests to be posted here as I don't care for PM's. :)

    This code is attached to my camera. It shoots a ray out from the center of the camera and weaponTarget is just an empty game object that my weapons can aim at. The layermask ensures the raycast doesn't hit myself, powerups etc. Not sure what the 1000 range does or if it's needed. :)
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraRayCast : MonoBehaviour {
    5.  
    6.     public GameObject weaponTarget;
    7.     public LayerMask layerMask;
    8.  
    9.     void LateUpdate () {
    10.         Vector3 direction = transform.TransformDirection(0, 0, 1);
    11.         RaycastHit hit;
    12.        
    13.         if (Physics.Raycast (transform.position, direction, out hit, 1000, layerMask)) {
    14.             weaponTarget.transform.position = hit.point;
    15.         }
    16.     }
    17. }
    This code is attached to my weapons game object, this is just an game object which has my weapons as children.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LookAt : MonoBehaviour {
    5.  
    6.     public Transform target;
    7.  
    8.     void LateUpdate () {
    9.         if (target) {
    10.             transform.LookAt(target);
    11.         }
    12.     }
    13. }
    If you want your weapons to smoothly follow the sight instead aiming perfect no matter how fast you turn can use the SmoothLookAt script which comes with the Unity standard assets.

    Someone also asked for the weapon switching so here it is:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerWeapons : MonoBehaviour {
    5.  
    6.     void Awake () {
    7.         SelectWeapon(0);
    8.     }
    9.  
    10.     void Update () {
    11.         if (Input.GetKeyDown("1")) {
    12.             SelectWeapon(0);
    13.         } else if (Input.GetKeyDown("2")) {
    14.             SelectWeapon(1);
    15.         } else if (Input.GetKeyDown("3")) {
    16.             SelectWeapon(2);
    17.         } else if (Input.GetKeyDown("4")) {
    18.             SelectWeapon(3);
    19.         } else if (Input.GetKeyDown("5")) {
    20.             SelectWeapon(4);
    21.         } else if (Input.GetKeyDown("6")) {
    22.             SelectWeapon(5);
    23.         }
    24.     }
    25.  
    26.     void SelectWeapon (int index) {
    27.         for (int i=0;i<transform.childCount;i++) {
    28.             // Activate the selected weapon
    29.             if (i == index)
    30.                 transform.GetChild(i).gameObject.SetActiveRecursively(true);
    31.             // Deactivate all other weapons
    32.             else
    33.                 transform.GetChild(i).gameObject.SetActiveRecursively(false);
    34.         }
    35.     }
    36. }
    37.  
    So it's:
    Weapons
    - pistol
    - rifle
    - shotgun
    - etc.

    and the LookAt script above is attached to "Weapons".

    Keep in mind the above weapon switching code is taken directly from the first person shooter tutorial so there's lots to learn from the tutorials. And I see it can be rewritten, but I won't bother with that now.

    Hope this helps.
     
    Last edited: Mar 17, 2011
  6. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Well in that code's current incarnation if you were aiming at a wall that was more than 1000 units away the weaponTarget would remain at the last known good position that was within range, so you'd probably an else clause that just positions it 1000 units in front of the camera.
     
  7. sheva

    sheva

    Joined:
    Nov 22, 2010
    Posts:
    157
    It's AWESOME :) I liked it too much
     
  8. felipi arena

    felipi arena

    Joined:
    Jun 7, 2010
    Posts:
    25
    Great work!

    Your camera is just what i'm looking for, can you please share your camera code too? or if not tell me where did you get the code from before changing? whatever i do i can only make my camera look to my character, so the aim don't work...

    Thanks in advance.
     
  9. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,553
    Pretty clever "pathfinding"! I implemented what you described and it worked so much more efficiently than what I could do with UnitySteer. Of course, I may have been just using UnitySteer in the wrong way. There were times when my enemies kept on circling you like a car, or times when they wouldn't want to follow you if you're behind them even though you're far enough for the seeking behaviour to kick in.
     
  10. CharlieSamways

    CharlieSamways

    Joined:
    Feb 1, 2011
    Posts:
    3,424
    VERY NICE! you in need of a 3d artist?

    Regards

    -Charlie Samways, 3D Freelance Modeller
     
  11. eltersouza

    eltersouza

    Joined:
    May 5, 2011
    Posts:
    1
    Hey TwiiK, I think it's amazing. I'm learning unity too, I've just began but i'm doing some modifications in tutorials I get. Do you share your project, for studying?
     
  12. Creativita

    Creativita

    Joined:
    Dec 2, 2011
    Posts:
    180
    This is awesome. I love everything except two small details. The waves are too long and their are too many enemies. But other than these faults it is flawless

    Points: 87900
    Wave: 7
     
    Last edited: Dec 13, 2011
  13. WalterEspinar

    WalterEspinar

    Joined:
    Aug 23, 2011
    Posts:
    84
    Hi TwiiK!, can you post some info about your lighting settings? eg: light intensity, colors, image effects,etc
    thank you
     
  14. handsome_boy

    handsome_boy

    Joined:
    Apr 24, 2012
    Posts:
    2
    I love this demo, but i am getting a big error, need your help, i want to make this game run for android, but this conversion gives me a series of errors ?? i need to resolve them, but i couldnt as i am newbie and only had 1 month to complete the whole game ?? please do help me
    My Email: bcsf8_123@hotmail.com
    please do contact me ...
     
  15. JohnPet

    JohnPet

    Joined:
    Aug 19, 2012
    Posts:
    85
    How did you make this dirty lens effect to appear when you look at light??
     
  16. Kemee21

    Kemee21

    Joined:
    Jul 14, 2013
    Posts:
    1
    Can you do it in multiplayer? I've doing project for school. A third person camera multiplayer game and I'm getting problems in my camera. How will I initialize the cameras for each player?. It would be a great help.