• 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 Fewpwew130 · Jul 23, 2014 at 07:50 PM · rotationvector

Vector rotation

Hello, dear community! I have a 2D game from the top view. I would like to make a character shooting forward one projectile, one projectile 45° to the left and another projectile 45° to the right.

I know how to correctly spawn the first projectile with the right position and direction. But 2 other projectiles do not work as intended - they seem to spawn just at the top of the first one.

I am leaving a script and a picture for reference. Do you guys have an idea, what may be the problem? Or maybe there is another way to rotate these 2 projectile vectors +/- some degrees from the main vector? Any help is very appreciated!

  function Update() {
 
            //if a player is moving
         if(transform.position != old_position)
         {
         newVector = transform.position - old_position;
         var coordinates = GameObject.Find("professor").transform.position;
     
                 //if a player decides to shoot while moving
                 if (Input.GetMouseButtonDown(0))
                 {
                 var projectile: Rigidbody2D = Instantiate (rocket, coordinates, Quaternion.identity);    
                 var force =10;
                 projectile.rigidbody2D.AddForce(newVector.normalized * force / Time.fixedDeltaTime);
                 
                 
                 
         plus45vector = Quaternion.AngleAxis(45, Vector2.up)* newVector;
                var projectile2: Rigidbody2D = Instantiate (rocket, coordinates, Quaternion.identity);  
                 projectile2.rigidbody2D.AddForce(plus45vector.normalized  * force / Time.fixedDeltaTime);
                     
                     
                     
                 minus45vector = Quaternion.AngleAxis(-45, Vector2.up)* newVector;
                var projectile3: Rigidbody2D = Instantiate (rocket, coordinates, Quaternion.identity);  
                 projectile3.rigidbody2D.AddForce(minus45vector  * force / Time.fixedDeltaTime);
                     
                     
                    
                 }
          
         old_position = transform.position; 
         }


alt text

rotation.jpg (76.1 kB)
Comment
Add comment · Show 4
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 robertbu · Jul 23, 2014 at 08:11 PM 0
Share

Your rotation code looks fine if your camera is looking towards negative 'Y'. Some build top-view games on the XY plane. A couple of thoughts:

  • You are spawning three objects at the same position. They are likely colliding with each other. I've only limited experience with the 2D physics, but it definitely would be a problem in 3D. A 3D solution would be to use Physics.IgnroeCollision(). Currently there is no Physics2D.IgnoreCollision() in the reference manual, but based on reading, there was one in the works. I'm running an older version of Unity (4.3.4) due to some conflicts with the latest build, but you should see if Physics2D is in the latest build. If not, you can solve the problem by either creating three separate spawn points separated a bit or by spawning the objects a little ways out on the vector you are using to give velocity to the projectile (so they are separated).

  • Time.fixedDeltaTime is a constant that doesn't make much sense used here. That is, it does not give any intuitive value to the 'force' setting.

avatar image Fewpwew130 · Jul 24, 2014 at 08:29 AM 0
Share

Thank you very much for your input: robertbu and schaddemm!

"Is this what you mean, or is your problem actually that the shots arent flying the right way?"

There is no problem they spawn in one point, the problem is they follow the same direction. I want to have 3 directions: one depending on rotation and movement (already done) and other two + / - 45° from the main direction. :)

avatar image schaddemm · Jul 24, 2014 at 02:31 PM 0
Share

Thats what I meant. Ill try to code this and get back to you soon, shouldnt be too hard to figure out.

avatar image robertbu · Jul 24, 2014 at 05:27 PM 0
Share

If collision is not the problem, the only way I can see for you to have three directions is if you are rotating your vector on the wrong axis. So your camera is looking at negative 'y'?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by schaddemm · Jul 23, 2014 at 08:22 PM

Awesome picture.

You are spawning all of the projectiles at the Vector3 coordinates, so yes they all will spawn in the same place.

If you want to spawn them in different places you have to calculate different positions.

A mathfree approach would be to create spawnpoints as children of the gameObject.

Is this what you mean, or is your problem actually that the shots arent flying the right way?

Update:

Got this working now. The major issue is the quaternion multiplication. projectile.rigidbody2D.velocity = newVector Quaternion.AngleAxis(45f, Vector3.forward) Vector3.up * projectileSpeed; This should do the trick.

Rigidbody2D bullet = Instantiate() I couldnt get to work in c#, only passing a Transform did work for me, so maybe thats another issue.

As was mentioned the use of fixedDeltatime is illogical, since its a constant(see manual). So delete that. I wouldnt see sense in using Time.deltatime also. Because we always want to apply the same force, and not more if more time has passed.

The projectiles spawning in the same place is also going to cause issues, if they collide with eachother. As mentioned by robertbu.

Also I think its better practice to directly set the velocity of the projectiles instead of adding a force, since this achieves more directly what we want to do. rigidbody2D.velocity = ...

Lastly, because I believe this kind of information is always good to repeat, using Gameobject.Find in timecritical operations is not so good, because its slow.

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 schaddemm · Jul 23, 2014 at 08:28 PM 0
Share

as a sidenote: projectile3 is a Rigidbody2D, so projectile3.rigidbody2D is redundant(does that even work?)

avatar image robertbu · Jul 23, 2014 at 08:53 PM 0
Share

@schaddemm - yes it works, though it is less efficient. Rigidbody2D inherits 'rigidbody2D' from $$anonymous$$onobehaviour. Under the hood, it is a GetComponent(Rigidbody) on the current game object.

avatar image schaddemm · Jul 23, 2014 at 08:58 PM 0
Share

@robertbu ty

avatar image Fewpwew130 · Jul 25, 2014 at 08:42 AM 0
Share

I think I will stay with this variant: it works, but not as intended:

function Update() {

            //if a player is moving
         if(transform.position != old_position)
         {
         newVector = transform.position - old_position;
         plus45vector = Quaternion.AngleAxis(45, Vector2(1, 0.5))* newVector.normalized;
         $$anonymous$$us45vector = Quaternion.AngleAxis(-45, Vector2(1, -0.5))* newVector.normalized;
         
         
         
         
         
         var coordinates = GameObject.Find("professor").transform.position;
     
                 //if a player decides to shoot while moving
                 if (Input.Get$$anonymous$$ouseButtonDown(0))
                 {
                 var projectile: Rigidbody2D = Instantiate (rocket, coordinates, Quaternion.identity);    
                 var force =10;
                 projectile.rigidbody2D.AddForce(newVector.normalized * force / Time.fixedDeltaTime);
                 
                 
                 
                   
                     var projectile2: Rigidbody2D = Instantiate (rocket, coordinates, Quaternion.identity);  
                    projectile2.rigidbody2D.AddForce(plus45vector.normalized  * force / Time.fixedDeltaTime);
           
         
                   
                     
                     
                 
                var projectile3: Rigidbody2D = Instantiate (rocket, coordinates, Quaternion.identity);  
                projectile3.rigidbody2D.AddForce($$anonymous$$us45vector.normalized  * force / Time.fixedDeltaTime);
                     
                     
                    
                 }
          
         old_position = transform.position; 
         state = 1;
         }
avatar image
0

Answer by Fewpwew130 · Jul 25, 2014 at 08:37 AM

Thank you again for trying to help, I upvoted you, unfortunatelly don't know how to commend robertbu.

In the line

 var projectile2: Rigidbody2D = Instantiate (rocket, coordinates, Quaternion.identity);  
 projectile2.rigidbody2D.AddForce(plus45vector.normalized  * force / Time.fixedDeltaTime);

I'm trying to substitute according to your remarks:

  var projectile2: Rigidbody2D = Instantiate (rocket, coordinates, Quaternion.identity);  
  projectile2.rigidbody2D.velocity = newVector * Quaternion.AngleAxis(45f, Vector2.forward) * Vector2.up * force;  
                     
 And the Unity shows an error: "Operator '*' cannot be used with a left hand side of type 'UnityEngine.Vector2 and a right hand side of type UnityEngine.Quaternion." Removing newVector from the equation removes the error. And spawns the projectile without any velocity at 0-0-0.

Anyway, thank you for help guys! I am pleased to received answers so quickly. Thank you.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

23 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

Related Questions

Multiple Cars not working 1 Answer

Shooting problem the bullets fly in diffrent direction then player looks? 2 Answers

Setting rotation of player equal to rotation of a Line from Line Renderer,Get the rotation of Line Renderer and equal it to the rotation of the player 0 Answers

How to make a animation rotation js.script? 2 Answers

Change input direction to camera's forward 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