- Home /
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
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.
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;
Your answer
Follow this Question
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