Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by skaardesigns · Nov 22, 2018 at 10:20 AM · rotationscripting probleminstantiatemousepositionshotgun

Making a shotgun shooting 45 degrees to the left of the mouse

I am making a 2D top-down shooter and attempting to make a shotgun-like weapon, I currently have a piece of the script that shoots towards the mouse and I had it instantiate two other bullets alongside of that that rotates 45 degrees but it still keeps moving along the same axes. This is the script I have that's supposed to use the Shotgun mechanic

     public void Shotgun ()
     {
         GameObject bb = Instantiate(player.bullet, player.bulletSpawn.transform.position, Quaternion.identity);
         bb.GetComponent<Rigidbody2D>().AddForce((player.mousePos - transform.position) * player.bulletSpeed);
         GameObject bb2 = Instantiate(player.bullet, player.bulletSpawn2.transform.position, Quaternion.identity);
         bb2.GetComponent<Rigidbody2D>().AddForce((new Vector3(player.mousePos.x, player.mousePos.y, player.mousePos.z + 45) - transform.position) * player.bulletSpeed);
         GameObject bb3 = Instantiate(player.bullet, player.bulletSpawn3.transform.position, Quaternion.identity);
         bb3.GetComponent<Rigidbody2D>().AddForce((new Vector3 (player.mousePos.x, player.mousePos.y, player.mousePos.z - 45) - transform.position) * player.bulletSpeed);
     }

The parts of the "player" variable it's refering to is a reference to my player script which looks like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : MonoBehaviour {
 
     public Animator anim;
     public float moveSpeed;
     public GameObject bullet;
     public GameObject bulletSpawn;
     public GameObject bulletSpawn2;
     public GameObject bulletSpawn3;
     public float bulletSpeed;
     public float stamina;
     public bool hasEnoughStamina;
     //[HideInInspector] public Vector2 lookDirection;
     [HideInInspector] public Vector3 mousePos;
     private float sprintSpeed;
     private PlayerAbilities pa;
 
     private void Start()
     {
         pa = gameObject.GetComponent<PlayerAbilities>();
         sprintSpeed = moveSpeed *= 1.5f;
     }
 
     void Update () 
     {
         if(!hasEnoughStamina)
         {
             moveSpeed = 0.1f;
             stamina += Time.deltaTime;
             if (stamina >= 10)
                 hasEnoughStamina = true;
         }
 
         mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
 
         if (Input.GetKey(KeyCode.A))
         {
             transform.Translate(Vector3.left * moveSpeed, Space.World);
         }
 
         if (Input.GetKey(KeyCode.D))
         {
             transform.Translate(Vector3.right * moveSpeed, Space.World);
         }
 
         if (Input.GetKey(KeyCode.W))
         {
             transform.Translate(Vector3.up * moveSpeed, Space.World);
         }
         
         if (Input.GetKey(KeyCode.S))
         {
             transform.Translate(Vector3.down * moveSpeed, Space.World);
         }
 
         if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
         {
             anim.SetBool("IsMoving", true);
         }
 
         if (!Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D))
         {
             anim.SetBool("IsMoving", false);
         }
 
         if(Input.GetKeyDown(KeyCode.Mouse0))
         {
             pa.Rifle();
         }
 
         if(Input.GetKeyDown(KeyCode.Mouse1))
         {
             pa.Shotgun();
         }
 
         if(Input.GetKey(KeyCode.LeftShift))
         {
             if (hasEnoughStamina)
             {
                 moveSpeed = sprintSpeed;
                 stamina -= Time.deltaTime;
                 if (stamina <= 0)
                     hasEnoughStamina = false;
             }
         }
 
         if(Input.GetKeyUp(KeyCode.LeftShift))
         {
             moveSpeed = 0.1f;
         }
     }
 }

If you need any images at all or any other information to help answering, I would be more than happy to give it to you. Thank you for reading this, any advice or help would be wonderful!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by skaardesigns · Nov 28, 2018 at 06:04 PM

I did some researching and came across this Unity answers that I had to do some editing and translating with (https://answers.unity.com/questions/171437/shoot-at-an-angle-2d.html) and I came up with this piece of code:

 public void Shotgun ()
     {
         Vector3 targetDelta = player.mousePos - transform.position;
         Vector3 force = targetDelta.normalized * player.bulletSpeed;
         Vector3 eulerAngles = new Vector3 (0, 0, 0);
         GameObject bb = Instantiate(player.bullet, player.bulletSpawn.transform.position, Quaternion.identity);
         bb.GetComponent<Rigidbody2D>().AddForce(targetDelta * player.bulletSpeed);
         bb.GetComponent<Bullet>().damage = shotgunBulletDamage;
         eulerAngles.z = -15;
         Vector3 bb2Force = Quaternion.Euler(eulerAngles) * force;
         GameObject bb2 = Instantiate(player.bullet, player.bulletSpawn2.transform.position, Quaternion.identity);
         bb2.GetComponent<Rigidbody2D>().AddForce(bb2Force * (player.bulletSpeed/6));
         bb2.GetComponent<Bullet>().damage = shotgunBulletDamage;
         eulerAngles.z = 15;
         Vector3 bb3Force = Quaternion.Euler(eulerAngles) * force;
         GameObject bb3 = Instantiate(player.bullet, player.bulletSpawn3.transform.position, Quaternion.identity);
         bb3.GetComponent<Rigidbody2D>().AddForce(bb3Force * (player.bulletSpeed/6));
         bb3.GetComponent<Bullet>().damage = shotgunBulletDamage;
     }

It basically creates a force and adjusts the angle of the force instead of adjusting the rotation it spawns at which gets the effect I want perfectly.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

207 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating objects which are facing center of a sphere 1 Answer

Shoot in proper direction with spread? 1 Answer

How to rotate object in instantiate with float value 1 Answer

How to Instantiate to Mouse Position 0 Answers

Network.Instatiate Problem 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges