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

Help me finish the script - shoot at raycast

Discussion in 'Physics' started by KrisBendix, May 28, 2015.

  1. KrisBendix

    KrisBendix

    Joined:
    May 26, 2015
    Posts:
    15
    Hi.
    Total beginner here so keep that in mind.
    Got that far to shoot arrow from object. game1.jpg
    Now I need to change this script (and do other things if needed) to shoot from that capsule to where I click with mouse at certain point on that raycast thing.
    Script (as good/bad it is, it is working this far):ProjectileShooter.cs
    And as far as I understand this guy got it working:
    http://answers.unity3d.com/questions/763289/how-do-i-ray-cast-shoot-to-mouse-crosshair-from-me.html
    However my camera wont be moving around so it probably shouldn't be as complicated.

    Basically to incorporate necessary parts of it in my script properly. I can't do it yet... Unity doesn't like whatever script mixture I'm giving it.
     

    Attached Files:

  2. Txguug

    Txguug

    Joined:
    Jun 16, 2014
    Posts:
    18
    This is what I would try (but I'm a beginner too!)

    get a vector3 of where you clicked with the mouse,
    then instaniate the arrow wherever that is and get it's location
    then Lerp (instaniationlocation, mouseclicklocation, speed)

    This is how you can get that first vector 3 - shoot a ray from dead center of the camera to where ever it collides

    Code (CSharp):
    1.   Ray cameraRay = Camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
    2.         RaycastHit hit;
    3.         if (Physics.Raycast (cameraRay, out hit)) {
    4.             Vector3 hitPoint = hit.point;
    5.         }
     
  3. KrisBendix

    KrisBendix

    Joined:
    May 26, 2015
    Posts:
    15
    Here it is:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ProjectileShooter : MonoBehaviour {
    5.    
    6.     GameObject prefab;
    7.     void Start () {
    8.         prefab = Resources.Load ("Projectile") as GameObject;
    9.     }
    10.     void Update()
    11.     {
    12.         if (Input.GetMouseButtonDown(0))
    13.         {
    14.             RaycastHit hit;
    15.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    16.             Vector3 aimPoint;
    17.             if (Physics.Raycast(ray, out hit, 500))
    18.             {
    19.                     aimPoint = hit.point;
    20.                 }
    21.                 else
    22.                 {
    23.                     aimPoint = ray.origin + (ray.direction * 500);
    24.             }
    25.             GameObject projectile = Instantiate(prefab) as GameObject;
    26.             projectile.transform.LookAt(aimPoint);
    27.             Rigidbody rb = projectile.GetComponent<Rigidbody>();
    28.             rb.velocity = projectile.transform.forward * 40;
    29.         }
    30.     }
    31. }
    Works OK, more or less. Doesn't hit the projectile exactly where I click, but at least it is consistent and it actually can work for gameplay mechanics. The projectile is affected by gravity after all.

    Now I can't figure out how to destroy that projectile after 2 seconds. I can't have scene full of projectiles.
    Only thing I manage to get is Destroy-ing a thing after 2 seconds, but the thing is not the projectile(Clone) but the shooter. If I make camera to be projectileShooter then it Destroys camera after 2 seconds. Same with Capsule or any other object that I turn in to projectileShooter.

    How to fix this and properly destroy every new projectile(Clone) after 2 seconds?
     
  4. KrisBendix

    KrisBendix

    Joined:
    May 26, 2015
    Posts:
    15
    OK I found how to destroy it. I was trying to do it from within shooter script. Best thing that worked that way was Destroy(GameObject.Find("Projectile(Clone)")), but the problem there was that if I shoot multiple projectiles in a row it deleted only last one. Couldn't keep up or something.
    What I actually had to do was to create a new script file and attach it to projectile prefab itself (the object that is loaded, cloned and shot).
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class destroyClone : MonoBehaviour {
    5.  
    6.     void Update () {
    7.         Destroy (this.gameObject, 1);
    8.     }
    9. }
    Now I shoot it and the thing disappears after 1 second.

    Problem is, that in some posts peoples forget to mention it, so I go mad trying to do it from within shooter script. :p
    But thats OK. Experienced peoples tend to assume that other know stuff already. :)