- Home /
Projectile Rotation To Match Direction Shot? Mobile Joystick
I've scanned over tons of threads, even looked into most of the Quaternion documents to try to solve my problem myself.. So no flaming me for not trying! ;)
Anywho.. I've got this neat little 2d rpg I'm developing for Android/iOS, in which I use two joysticks to interact with the character. The left joystick moves the current character around while the right shoots a projectile in whichever direction the joystick is pointed.
So far I've got everything working except the dang rotation of the projectile.
I've got it shooting in the right direction, I just can't seem to make it rotate to point in the direction it was shot.
I've tried the Quaternion.LookRotation(shootDir)... however there is a problem with this method and the way my game is set up. I need the X rotation of the projectile to REMAIN -90.
Any alternatives/thoughts? :) Would save me a headache or two. Either way, I'm going to figure it out.
All of this code is on one page: playerweapon.js
Mobile vector grab for direction and majorStrike(); function call when touching.
#if UNITY_ANDROID
if(Input.touchCount >= 1){
for(var touch1 : Touch in Input.touches) {
if(touch1.position.x > Screen.width/2 && touch1.position.y < Screen.height/3*2){
innerJoy2.transform.position = camera.main.ScreenToViewportPoint(touch1.position)+Vector3(0,0,0.1);
var shootDir = new Vector3(outerJoy2.transform.position.x - innerJoy2.transform.position.x, 0,outerJoy2.transform.position.y - innerJoy2.transform.position.y);
shootDir.Normalize();
majorStrike(shootDir);
}
}
}else{
touching = false;
}
#endif
majorStrike function as follows
function majorStrike(shootDir : Vector3){
atkcounter += Time.deltaTime;
if(attackingnow == false){
attackingnow = true;
animScript.strike();
strike(shootDir);
}
if(atkcounter >= playerStats.fireSpeed){
attackingnow = false;
atkcounter = 0.0;
}
}
And now the strike() function which is called within majorStrike()
function strike (shootDir : Vector3) {
//ASSASSIN WEAPONS! :D//
if(PlayerPrefs.GetFloat("weapon") == 1){
randAngle = Random.Range(-10,10);
var daggerPrefab1 = Instantiate(dagger1, Vector3(transform.position.x,-5,transform.position.z), Quaternion.Euler(-90,180,0));
daggerPrefab1.rigidbody.velocity = shootDir*-daggerSpeed1;
}
I will buy whoever saves me from racking my brain even more tonight a steak dinner and a beer.
Thanks, Steven
Answer by djfatsteve2 · Mar 22, 2014 at 08:27 PM
To anyone else having this problem... I've solved the issue of the rotation of my projectile on the x axis. It NEEDED TO REMAIN -90 degrees to keep the sprite facing the camera and not sideways. I achieved this after instantiating by rotating the localEulerAngles on the projectile ;)
Code below for fix. I added the += 180 to the y axis because my projectiles images were backwards yet moving in the correct direction.
//ASSASSIN WEAPONS! :D//
if(PlayerPrefs.GetFloat("weapon") == 1){
curWeapon = sword;
useDir();
transform.position.y = -transform.position.z/10000+0.5;
randAngle = Random.Range(-10,10);
var daggerPrefab1 = Instantiate(dagger1, Vector3(transform.position.x,-5,transform.position.z), Quaternion.LookRotation(shootDir));
daggerPrefab1.rigidbody.velocity = shootDir*-daggerSpeed1;
daggerPrefab1.transform.localEulerAngles.x = -90;
daggerPrefab1.transform.localEulerAngles.y += 180;
}
Learning on our own does pay off eventually. Never hurts to ask though ;)
The game continues! :D
Answer by sarthakshah · Aug 19, 2014 at 07:18 AM
Hi, I have done similar thing , but using touch. I have tried to rotate bow using touch and asper its angle, i have called fire function, in which projectile is instantiated, and then force os given to it, and after that it will rotate in real time.
See below post, i have posted my answer, it may help you.
http://answers.unity3d.com/questions/656781/unity3dprojectilearrow-of-bow-is-not-rotating-as-p.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613140850im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
why is my character rotation always 180° after joystick is released. 1 Answer
Problem finding relative rotation from one quaternion to another 4 Answers
How can I get the rotation between two objects 1 Answer
Rotate object following mouse movement. Object jumps/ flips 1 Answer
How to make a plane face a static but rotating camera? 3 Answers