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. Dismiss Notice

Shooting problem

Discussion in 'Scripting' started by rehtse_studio, Jul 9, 2014.

  1. rehtse_studio

    rehtse_studio

    Joined:
    Jul 25, 2013
    Posts:
    24
    Hi, Im trying to make my character shoot at the direction of my mouse pointer but I was getting the fantastic error of invalidcase, made some changes to:


    Code (CSharp):
    1. public Rigidbody2D ammo;
    2.  
    3. if (Input.GetMouseButtonDown (0)) {
    4.  
    5.             Rigidbody2D bullet = (Rigidbody2D)Instantiate (ammo,transform.position, transform.rotation);
    6.             bullet.transform.LookAt(mouse_pos);
    7.             bullet.rigidbody2D.AddForce (bullet.transform.forward * bulletSpeed);
    8.  
    9.  
    10.                 }

    the thing is that i see that the code is working since i see the reaction in the game but i don't see the image of my prefab that i am using as the bullet, if i change the code i will get back the invalid case error.

    Please help, I have been with this problem for over a month.
     
    Last edited by a moderator: Jul 9, 2014
  2. Aurore

    Aurore

    Director of Real-Time Learning Unity Technologies

    Joined:
    Aug 1, 2012
    Posts:
    3,106
    Moved to scripting, in future please use code tags also.
     
  3. magnetix

    magnetix

    Joined:
    Apr 4, 2010
    Posts:
    102
    You are instantiating a Rigidbody2d, which is a physics component. Your prefab presumably has some graphical element too, so the Rigidbody2d would presumably be a component of the object you want to instantiate.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    this doesn't make sense... how can you see it working if you can't see the instantiated prefab?
     
  5. rehtse_studio

    rehtse_studio

    Joined:
    Jul 25, 2013
    Posts:
    24
    That is the thing; when i press the mouse key were i want to shoot, the enemy vanish like it was destroy.
     
  6. dvirus1023

    dvirus1023

    Joined:
    Mar 4, 2013
    Posts:
    51
    Try and instantiate the bullet as a GameObject instead of a RigidBody.
     
  7. rehtse_studio

    rehtse_studio

    Joined:
    Jul 25, 2013
    Posts:
    24
    Same thing happens, change RigidBoy2D to GameObject and see the reaction of the enemy when there are destroy but i don't see the prefab of the bullet.
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    is bulletspeed a big number? i.e. its getting created and destroyed in one frame and the next.... it would be difficult to see that.

    If you leave the hierarchy open when playing the game do you see the bullet appear in the hierarchy?
     
  9. rehtse_studio

    rehtse_studio

    Joined:
    Jul 25, 2013
    Posts:
    24
    the bulletspeed is 800 and every time you press the mouse you see in the hierarchy the name of the bullet a lot of time. For example, the name of my object is Pew, you see in the hierarchy:
    Pew(clone)
    Pew(clone)
    Pew(clone)
    every time you press the mouse key
     
  10. Recon03

    Recon03

    Joined:
    Aug 5, 2013
    Posts:
    838
    Code (CSharp):
    1.  Rigidbody2D bullet = (Rigidbody2D)Instantiate (ammo,transform.position, transform.rotation);
    Looks incorrect to me, I'm more of a 3d guy, and use the old school ways with 2d, but when writing Instantiate code this looks incorrect . Which may cause it to bug out.

    Code (CSharp):
    1. var clone : Rigidbody;
    2.             clone = Instantiate(ammo, transform.position, transform.rotation);
    3.  

    is this all of your code for this??

    Code (CSharp):
    1. public Rigidbody2D ammo;
    2. public float speed = 10f;
    3.  
    4. void Fire () {
    5.     Rigidbody2D holder= (Rigidbody2D) Instantiate(ammo, transform.position, transform.rotation);
    6.     holder.velocity = transform.forward * speed;
    7.  
    8.  
    9. }
    10.  
    11. // Calls the fire method when holding down ctrl or mouse
    12. void Update () {
    13.     if (Input.GetButtonDown("Fire1")) {
    14.         Fire();
    15.     }
    16. }
    17.  
    18.  


    When I do anything with shooting, depending on your game mechanics, have one script control the actually firing, and another one controlling the bullet separately. It looks to me that you have both together. This can cause issues. atleast from my experience , I been doing this along time.


    Look into this for your cursor when shooting.

    http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

    You may want a distance variable for that as well.
     
    Last edited: Jul 12, 2014
  11. rehtse_studio

    rehtse_studio

    Joined:
    Jul 25, 2013
    Posts:
    24
    This is my full code:

    using UnityEngine;
    using System.Collections;

    public class ShootingControll : MonoBehaviour {

    private Vector3 mouse_pos;
    private Vector3 object_pos;
    private float angle;
    private int bulletSpeed = 100;


    public GameObject ammo;


    void Start(){


    }


    void Update() {

    // Point the cannon at the mouse.
    mouse_pos = Input.mousePosition;
    mouse_pos.z = 0.0f;
    object_pos = Camera.main.WorldToScreenPoint(transform.position);
    mouse_pos.x = mouse_pos.x - object_pos.x;
    mouse_pos.y = mouse_pos.y - object_pos.y;
    angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg - 90;
    Vector3 rotationVector = new Vector3 (0, 0, angle);
    transform.rotation = Quaternion.Euler(rotationVector);

    if (Input.GetMouseButtonDown (0)) {

    GameObject bullet = (GameObject)Instantiate (ammo,transform.position, transform.rotation);
    bullet.transform.LookAt(mouse_pos);
    bullet.rigidbody2D.AddForce (bullet.transform.forward * bulletSpeed);


    }






    }


    }

    p.s IM sooooooooo sorry...can't find the code editor...im new at this
     
    Last edited: Jul 14, 2014
  12. rehtse_studio

    rehtse_studio

    Joined:
    Jul 25, 2013
    Posts:
    24
    WAIT...made some changes with the help with you guys:

    void Update(){

    if (Input.GetMouseButtonDown (0))
    Fire ();

    }


    void Fire(){

    GameObject bullet = (GameObject)Instantiate (ammo,transform.position, transform.rotation);
    bullet.transform.LookAt(mouse_pos);
    bullet.rigidbody2D.AddForce (bullet.transform.forward * bulletSpeed);

    }


    Every time i shooot NOW i see my prefab but i get this error:

    InvalidCastException: Cannot cast from source type to destination type.
    ShootingControll.Fire () (at Assets/Scripts/ShootingControll.cs:46)
    ShootingControll.Update () (at Assets/Scripts/ShootingControll.cs:35)