• 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 howtobasic100legit · Mar 04, 2019 at 12:43 PM · scripting beginnershootingprojectiletop down shootercharge

Problem with a charged projectile direction

I've been trying to create a topdown shooter were the weapon and the projectiles that should work like this. If you hold left mousebutton, start charging your shot to increase its speed and size. When you release the button it should fire the projectile on the cursor direction. I also want to be able to see my projectile while its charging and it should follow the player untill its shot. This is the code I made and its working fine with one exception. If I start charging my shot then i rotate my player to any direction and release the shot, my projectile will lauch on that direction.alt text

Gun Controller

 public PlayerProjectile projectile;

 private PlayerProjectile newProjectile;

 public bool isCharging;

 public Transform firePoint;

 private void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         isCharging = true;
         newProjectile = Instantiate(projectile, firePoint.position, firePoint.rotation);
         newProjectile.isCharging = true;
     }

     if (Input.GetMouseButtonUp(0))
     {
         isCharging = false;
         newProjectile.isCharging = false;

     }   
 }

}

PlayerProjectile

 private Transform firePoint;

 private Rigidbody rb;

 public float chargeSpeed = 10;
 public float chargeLimit = 30;

 public float newCharge;
 public float newScale;

 private float chargeLevel = 0f;


 public bool isCharging;

 void Start()
 {
     firePoint = GameObject.FindWithTag("firePoint").transform;
     rb = gameObject.GetComponent<Rigidbody>();
     rb.velocity = firePoint.transform.forward * 20;
 }

 void Update()
 {


     if (isCharging == true && newCharge <= chargeLimit)
     {
         transform.position = firePoint.position;


   
         chargeLevel += Time.deltaTime * chargeSpeed;
         chargeLevel = (chargeLevel > chargeLimit ? chargeLimit : chargeLevel + Time.deltaTime * chargeSpeed);
         newCharge = chargeLevel;
         newScale = chargeLevel * 0.1f;


         transform.localScale = new Vector3(newScale, newScale, newScale);


        // print(newScale);
     }

     if (newCharge >= chargeLimit)
     {
         newCharge = chargeLimit;

     }

     if (isCharging == false)
     {
         print(newCharge);
         Shoot();
         chargeLevel = 0f;
     }

 }
 
 void Shoot()
 {
    // rb.velocity = transform.forward * 20;
 }





untitled.png (318.3 kB)
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 · Mar 04, 2019 at 12:52 PM

save the rotation of the player whne you startted pressing the shoot button and use that in the shoot method

Comment
Add comment · Show 2 · 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 howtobasic100legit · Mar 04, 2019 at 01:12 PM 0
Share

It works. Thank you so much for your help. I appreciate alot. :)

avatar image xxmariofer howtobasic100legit · Mar 04, 2019 at 01:14 PM 0
Share

you are welcome, i have changed it to an answer so you can mark it as correct :)

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

106 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

Related Questions

Getting 2D projectiles that fire in the direction of the mouse position to work in unity 5? 0 Answers

Making a projectile specific model/mesh/etc rather than a primitive 4 Answers

Shooting projectile : GameObjects or Particles? 1 Answer

How to fire projectile in direction character is facing? 3 Answers

Multiplayer tank game 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