• 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 /
  • Help Room /
avatar image
0
Question by ak1603001 · Nov 04, 2019 at 02:44 PM · 2d rotation

After Instantiating a gameobject it does ot go in the desired direction and flys up

I have been working on a trying to implement a simple shooting system but I have no idea how the bullet prefab on instantiating changes its rotation and moves in a different direction as it should. I also tried to manually change the transform and rotation of the instantiated projectile but even that did not work . Here is the shoot class that instantiates projectiles from the weapon

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Shoot : MonoBehaviour {
 public float Offset=-90;
 public GameObject projectile;
 public Transform ShotPos;
 public float timeBtwShots;
 public float startTimeBtwShots;
     // Use this for initialization
     void Start () {
         
         
     }
     
     // Update is called once per frame
     void Update () {
         
                  Vector3 mousePos = Input.mousePosition;
          mousePos.z = 5.23f;
  
          Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
          mousePos.x = mousePos.x - objectPos.x;
          mousePos.y = mousePos.y - objectPos.y;
  
          float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
          transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
          if(timeBtwShots<=0)
          {
          if(Input.GetMouseButtonDown(0))
          {
              Instantiate(projectile,ShotPos.position,transform.rotation);
             projectile.transform.position=ShotPos.position;
             projectile.transform.rotation=transform.rotation;
 
             
             
              timeBtwShots=startTimeBtwShots;
          }
          }
          else{
              timeBtwShots-=Time.deltaTime;
 
          }
     }
 }
 

And this is the projectile class that destroys projectiles and translates its position

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Projectile : MonoBehaviour {
 public float bulletSpeed=7;
 public float lifeTime=1f;
 private Vector3 Target;
 
     // Use this for initialization
     void Start () {
 
         Invoke("DestroyProjectile",lifeTime);
         //Target=Camera.main.ScreenToWorldPoint(Input.mousePosition)+Camera.main.ScreenToWorldPoint(Input.mousePosition);
         
 
         
         
     }
     
     // Update is called once per frame
     void Update () {
     transform.Translate(Vector2.up*bulletSpeed*Time.deltaTime);
         
 
         
     }
     void DestroyProjectile()
     {
         Destroy(gameObject);
     }
 }
 



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
Best Answer

Answer by xxmariofer · Nov 04, 2019 at 02:52 PM

you are using the vector3 up so move the object so thats why it goes up

  transform.Translate(Vector2.forward*bulletSpeed*Time.deltaTime);
Comment
Add comment · Show 4 · 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
avatar image ak1603001 · Nov 04, 2019 at 03:23 PM 0
Share

Thanks for the answer but it shows that Vector2 does not contain a definition for forwardalt text

capture.png (14.4 kB)
avatar image ak1603001 · Nov 04, 2019 at 03:51 PM 0
Share

And also Vector3.forward makes the object move along the z axis but my project is in 2d

avatar image xxmariofer ak1603001 · Nov 04, 2019 at 04:01 PM 0
Share

Then use Vector2.right

avatar image ak1603001 xxmariofer · Nov 04, 2019 at 04:54 PM 0
Share

Thank you so much this works just fine

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

187 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

Related Questions

2D (look at) object disappears 1 Answer

2D Joint motor ignores limits when rotated towards -x. 0 Answers

How to fix enemy's rotation (2D) 0 Answers

2D smooth rotation (Quaterion slerp solution?) 0 Answers

how can i anchor a point of a sprite to make it swing? 1 Answer


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