Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Nboaram · Apr 23, 2015 at 03:22 PM · c#2d2d game2d rotation

Firing Projectiles in the same direction as my character is looking.

Hello Internet!, I've searched through a bunch of posts on Top Down 2D rotation with firing missiles and none of them apply to mine.

My Wizard manages to fire his magicmissiles from his wand but they come out unaligned with his wands direction. (Image showing missiles going in odd directions) It should be taking the Z value from the Quaternion of the wand and assigning that as the angle it goes out in ( or atleast thats what i believe it should be doing) But it seems to rotate faster than the wand and whilst it does change if I spin it isn't changing equally with the wand. So whilst if I point up it will fire up. If i point 45 degrees right of that it fires the missile directly into my wizard.

The Code for the MissileMovement ---------------

 using UnityEngine;
 using System.Collections;
 
 public class MoveMissile : MonoBehaviour {
 
     // Use this for initialization
 
     public float speed = 0.5F;
     public Transform Shotspawn; // <-- Set to Wand2
     // public Quaternion Direction;
     private    float Direction;
     void Start (){
 
         // Sets the direction that shot is fired in.
         Direction = transform.rotation.eulerAngles.z;
         transform.Rotate(0 , 0, Direction);
     }
     
     // Update is called once per frame
     void Update () {
     
 
         transform.Translate(Vector2.up * speed);
     }
 }

The Code for the Character Movement ---------------------------

 using UnityEngine;
 using System.Collections;
 
 public class TopDownCharController2 : MonoBehaviour {
 
     // Movement Variables
     public float walkSpeed;
     public bool colliding; 
 
     // Use this for initialization
     void Start () {
 
     }
 
 
 
     // Update is called once per frame
     void Update ()
     {
 
             if(Input.GetKey (KeyCode.I)) 
             {transform.Translate(Vector2.up * walkSpeed); } // UP MOVEMENT
             
             if(Input.GetKey(KeyCode.J)) 
             {transform.Translate(-Vector2.right * walkSpeed); } // LEFT MOVEMENT
             
             if(Input.GetKey(KeyCode.K)) 
             {transform.Translate(-Vector2.up * walkSpeed); }// DOWN MOVEMENT
             
             if(Input.GetKey(KeyCode.L)) 
             {transform.Translate(Vector2.right * walkSpeed); }// RIGHT MOVEMENT
             
             if(Input.GetKey(KeyCode.U)) {
                 // Clockwise
                 transform.Rotate(0, 0, -3.0f);
             }
             if(Input.GetKey(KeyCode.O)) {
                 // Counter-clockwise
                 transform.Rotate(0, 0, 3.0f); 
 
                 
             }
         }             
     }


I just don't understand what is causing it. If anyone is able to explain where i'm going wrong that would be extremely helpful :D

Comment
Add comment · Show 1
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 Nboaram · Apr 23, 2015 at 03:53 PM 0
Share

Genius. Thanks ever so much. I've been muddling away for hours trying to get that to work. if you post it as an Answer I shall vote it correct.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by maccabbe · Apr 23, 2015 at 03:36 PM

I can't tell without the script that spawns the missles. However the way you set the direction of the shot in the shot script

 Direction = transform.rotation.eulerAngles.z;
 transform.Rotate(0 , 0, Direction);

reads the transformation in the shot script and then rotates the shot even more. This should probably be

 transform.rotation=Shotspawn.rotation;
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

2 People are following this question.

avatar image avatar image

Related Questions

The player randomly freezes in place while other objects move ingame 1 Answer

Multiple Cars not working 1 Answer

How can I make a speed booster in a 2D game? 1 Answer

My 2D Player Can't Move (2d Photon Game) :(,My Player Don't Move (2d Character Controller 0 Answers

How to make a 2D autoaim,How to make an autoaim for a 2D 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