Search Unity

[solved] 3rd person aiming with script

Discussion in 'Scripting' started by sowel, Feb 7, 2010.

  1. sowel

    sowel

    Joined:
    Feb 4, 2010
    Posts:
    10
    this is a fundamental part of most games so i cant understand why there is not a tutorial deadicated to this.

    i will leave the script i used to get it to work here so anyone looking for the same answer as i was wont have to look to far.

    Replace your Crosshair script with this

    Code (csharp):
    1.  
    2. var hit : RaycastHit;
    3. var Gun : GameObject;
    4. var range = 100.0;
    5. var crosshairTexture: Texture2D;
    6. var position : Rect;
    7.  
    8. function Start ()
    9.  
    10. {
    11.   position = Rect ((Screen.width - crosshairTexture.width)/2, (Screen.hight - crosshairTexture.hight)/2 crosshairTexture.width, crosshairTecture.hight);
    12. }
    13.  
    14. function OnGUI ()
    15. {
    16.   GUI.DrawTexture (position, crosshairTexture);
    17. }
    18.  
    19. function Update ()
    20.  
    21.     {
    22.      var ray = camera.ViewportPointToRay        
    23.      (Vector3(0.5,0.5,0));
    24.      Debug.DrawRay (ray.origin, ray.direction *
    25.      range, Color.green);
    26.      
    27.      var hit : RaycastHit;
    28.      
    29.    
    30.        
    31.      if (Physics.Raycast (ray, hit))
    32.          {Gun.transform.LookAt(hit.point);}
    33.      else
    34.          {Gun.transform.LookAt(ray.GetPoint(range));}
    35.     }
    36.  
     
    Last edited: Oct 12, 2010
  2. sowel

    sowel

    Joined:
    Feb 4, 2010
    Posts:
    10
    please can someone help, i have been looking at the same pieces of code for 3 days now and its driving me crazy. im up till 2am just looking and failing every time i try somethig and im getting sick of the whole engine because of it.

    its taking all the restraint i have not to be rude in this post.
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Hi, welcome to the forum (and I'm sorry your first experiences with Unity are proving so frustrating).

    A raycast is usually performed using the Physics.Raycast function, but there are actually several variants of it, each tailored to a specific case. The idea is that a "ray" (think laser beam) is sent out from a particular position and in a particular direction. It is possible to detect the object that the ray hits and also the exact position of the contact point and a few other properties. This can be used to trace the firing line of a bullet or other weapon to see where it hits.

    Typically, you will know the position from which the projectile is launched (say, the gun's transform.position value) and the direction (eg, the gun's transform.forward). You can pass these into the raycast function along with a RaycastHit object, which receives information about the hit:-
    Code (csharp):
    1. var hitInfo: RaycastHit;
    2.  
    3. if (Physics.Raycast(gunPos, gunDirection, hitInfo)) {
    4.     var hitPos: Vector3 = hitInfo.point;
    5. }
    The RaycastHit object also contains the transform of the object that was hit and a number of other details. This means you can identify what was hit and where so you could, for example apply damage to the script or place a particle system for sparks or blood at the hit position.

    If the targetting position is obtained from the camera, then you can use Camera.ScreenPointToRay to generate the ray - there is an example script for this technique in this thread.
     
  4. sowel

    sowel

    Joined:
    Feb 4, 2010
    Posts:
    10
    thanks for getting back to me. im just about to read through the example you posted the link to. although i have no experience with java i can seem to understand it :D
     
  5. sowel

    sowel

    Joined:
    Feb 4, 2010
    Posts:
    10
    now i need a little help with my script.

    remember i have never used javascript so if its totaly wrong that would be why :D

    Code (csharp):
    1.  
    2. var hit : RaycastHit;
    3. private var Gun : GameObject;
    4.  
    5. function Start ()
    6.      {Gun = GameObject.Find ("/player/gun");}
    7.  
    8. function Update ()
    9.  
    10.     {
    11.      var ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0));
    12.      Debug.DrawRay (ray.origin, ray.direction * 200, Color.green);
    13.      
    14.      var hit : RaycastHit;
    15.      
    16.      Gun.transform.LookAt(RaycastHit.point);
    17.        
    18.      if (Physics.Raycast (ray, hit))
    19.          {print ("I'm looking at " + hit.transform.name);}
    20.      else
    21.          {print ("I'm looking at nothing!");}
    22.     }
    23.  
    24.  
    i keep getting two errors with this,
    the 1st one.
    and this is the 2nd

    any help would really be appreciated.
     
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The point value is an instance variable, so you need to get it from "hit" rather than the RaycastHit class:-
    Code (csharp):
    1. Gun.transform.LookAt(hit.point);
    Furthermore, the value of this is only set after the Physics.Raycast call has been made, so you will need to make the LookAt call inside the "if" statement.

    The other error is due to the fact you have the Gun object declared private. Declare it as a normal variable and you should see it in the inspector when you add your script to its GameObject. You then drag another object onto this variable in the inspector to make a reference to that object.
     
  7. sowel

    sowel

    Joined:
    Feb 4, 2010
    Posts:
    10
    thank you soo much.
    i finaly have my 3rd person aiming system :D

    now i just need to give the raycast a max distance, tell my projectile to aim there if the raycast hits nothing and to destroy itself once it gets there.

    the only part i dont know how to do it set the max distance for the raycast when useing.
    Code (csharp):
    1. var ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0));
    i wish my local college did a part time course on javascript. any books you could recommend to help me learn?
     
  8. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You don't set the raycast distance when you create the ray from the camera, it's actually a parameter to the Physics.Raycast call.

    Most JavaScript books are aimed at web development, and that type of JS is not quite the same as Unity's. There is, however, a Unity JS tutorial in this thread that you may find useful.
     
  9. sowel

    sowel

    Joined:
    Feb 4, 2010
    Posts:
    10
    thanks ill give that a look over. :D
    i have posted the full script i am now using in the 1st post.
     
  10. Wibo

    Wibo

    Joined:
    Jun 12, 2010
    Posts:
    11
    Hi, and thanks for posting the result of the aiming script.
    There's one thing i have trouble with to figure it out(probably a newb question :) im new to Unity and Javascript. :

    Code (csharp):
    1. {Gun.transform.LookAt(GetPoint(range));}
    when i use the script, everything works perfectly, exept for one thing, i get the error messege:
    Unknown identifier:'GetPoint'.

    i have used the search function, but i didnt find a result helping me to solve it.
     
  11. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    To fix it do this instead of just GetPoint, use ray.GetPoint, it tells it what to get the point from.
     
  12. sowel

    sowel

    Joined:
    Feb 4, 2010
    Posts:
    10
    sorry about bumping such an old post but i just needed to update the script.
    i just couldn't leave a broken script on here.
    thanks to astrauk for pointing out the bit i forgot, i was not at my pc when i posted this scrip so i had to wright it all out and because i didnt have the scrip in fount of me i forgot about that little bit.

    anyway the top script is updated to work with out needing to edit it.
    if anyone knows how to keep the players back to the camera i would like to also add that to the script.
     
  13. Nikolay116

    Nikolay116

    Joined:
    Mar 21, 2010
    Posts:
    421
    so you are basically aligning gun with the mouse cursor? As a first step this is OK, but for 3rd person you need normally the hand together with the gun and torso a bit track to the mouse cursor (or some point on a ray coming from camera via mouse cursor). So what you did is just the beginning of the story
     
  14. sowel

    sowel

    Joined:
    Feb 4, 2010
    Posts:
    10
    well i have to admit i have not looked into that, i had my gun coming out of the chest of my player.

    the script is really just a basic starting point for someone to expand on. maybe attach the gun to the arm bone and have that move with the gun.... this was my first script and i haven't had an opportunity to use unity since then up until now :)

    if you know on how to improve the script feel free to add to what i have done, some one might put it to good use
     
    Last edited: Oct 12, 2010
  15. Adibazhor

    Adibazhor

    Joined:
    Jul 25, 2014
    Posts:
    1
    i need some help please help me everyday im trying to make a game but im not able to make only two think i need some help with it first i cant make enemys and second im trying to make aim but i cant i need help with both of there scripts please help me .........
     
  16. Mathias_Melk

    Mathias_Melk

    Joined:
    Sep 25, 2017
    Posts:
    1
    {
    public RaycastHit hit;
    public GameObject Gun;
    public float range = 100f;
    public Texture2D crosshairTexture;
    public Rect position;
    void Start()
    {
    position = new Rect(((Screen.width - crosshairTexture.width)/2f), ((Screen.height - crosshairTexture.height)/2f), crosshairTexture. width, crosshairTexture.height);
    }

    void OnGUI()
    {
    GUI.DrawTexture(position, crosshairTexture);
    }

    void Update()

    {
    Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    Debug.DrawRay(ray.origin, ray.direction * range, Color.green);

    if (Physics.Raycast(ray, out hit))
    {
    Gun.transform.LookAt(hit.point);
    }
    else
    {
    Gun.transform.LookAt(ray.GetPoint(range));
    }
    }

    }