- Home /
2d shooting problem
im new to unity 1 month ago , im creating a one sided game shooting to the left side , i seen some scripts in tutorials and made my player shoot succesfully , but problem is in some angels of 360 the bullet go above my mouseposition and its very noticable , thanks in advance ;) heres my script for arm rotation:
 // Update is called once per frame
 void Update () 
 {
     Vector2 mouse = Camera.main.ScreenToViewportPoint(Input.mousePosition);        //Mouse position
     Vector3 objpos = Camera.main.WorldToViewportPoint (transform.position);        //Object position on screen
     Vector2 relobjpos = new Vector2(objpos.x - 0.5f,objpos.y - 0.5f);            //Set coordinates relative to object
     Vector2 relmousepos = new Vector2 (mouse.x - 0.5f,mouse.y - 0.5f) - relobjpos;
     float angle = Vector2.Angle (Vector2.up, relmousepos);    //Angle calculation
     if (relmousepos.x > 0)
         angle = 360-angle;
     Quaternion quat = Quaternion.identity;
     quat.eulerAngles = new Vector3(0,0,angle-102); //Changing angle
     transform.rotation = quat;
 }
}
and my weaponPistole script too: void Awake () { firePoint = transform.FindChild ("FirePoint"); if (firePoint == null) { Debug.LogError ("no fire point noob");
     }
 }
 
 // Update is called once per frame
 void Update () {
     
     if (fireRate == 0) {
         if (Input.GetButtonDown ("Fire1")) {
             Shoot ();
         }
     }
         
     
 
 else {
         if (Input.GetButton ("Fire1") && Time.time > timeToFire){
             timeToFire=Time.time + 1/fireRate;
             Shoot();
         }
 }
 }
 void Shoot(){
     Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint(Input.mousePosition).x,Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
     Vector2 firePointPosition = new Vector2 (firePoint.position.x,firePoint.position.y);
     RaycastHit2D hit = Physics2D.Raycast (firePointPosition,mousePosition-firePointPosition,whatToHit);
     if (Time.time >= timeToSpawnEffect) {
         Effect ();
         timeToSpawnEffect = Time.time + 1 / effectSpawnRate;
     
     }
     Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100,Color.green);
     if (hit.collider != null) {
         Debug.DrawLine (firePointPosition, hit.point, Color.red);
         Debug.Log ("We hit" + hit.collider.name + "and did " + Damage + "Damage");
     }
 
 }
 void Effect(){
     Instantiate (BulletTrailPrefab,firePoint.position,firePoint.rotation);
 
 }
}
true... has the issue been solved by now, or do you still need help?
Answer by triis4924 · Jan 02, 2019 at 10:26 PM
maybe try this : https://www.youtube.com/watch?v=bY4Hr2x05p8&t because I think the code is easier and there aren't bugs.
Your answer
 
 
             Follow this Question
Related Questions
I Can't Jump, And If I Remove if(isGrounded) I Can Do More Then 10 Jumps IN The Air 1 Answer
Instantiate a GameObject with a specific Z rotation 2 Answers
so I'm trying to make a 2D platform but the first animation i make is the only one that registers 0 Answers
Destroy a gameObject in the scene if instantiate the same gameObject 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                