- Home /
Instantiate projectile rotation
So yeah this seems to be a common problem and I haven't gotten any wiser after a few hours of trying things out. The fireballs fly in all the wrong directions and I have no idea why :)
using UnityEngine;
using System.Collections;
/// <summary>
/// Create fire ball.
///
/// This script is attached to the player and instantiates a fireball when "Attack" is pressed.
/// </summary>
public class CreateFireBall : MonoBehaviour {
public GameObject fireBall;
Transform playerTransform;
Vector3 firePosition;
Quaternion playerRotation;
// Use this for initialization
void Start ()
{
//if(networkView.isMine == true)
{
playerTransform = transform;
}
//else
{
//enabled = false;
}
}
// Update is called once per frame
void Update ()
{
//mouse 0
if(Input.GetButtonDown("Attack"))
{
//In front of the player?
firePosition = playerTransform.TransformPoint(0,0,0.2f);
//what the f---?
playerRotation = Quaternion.Euler(playerTransform.eulerAngles.x + 90f, playerTransform.eulerAngles.y, playerTransform.eulerAngles.z);
//call the function to instantiate the fireball
fire (firePosition, playerRotation);
//networkView.RPC("fire", RPCMode.All, firePosition, playerRotation);
}
}
//[RPC]
void fire(Vector3 position, Quaternion rotation)
{
Instantiate(fireBall, position, rotation);
}
}
Ok I did some more debugging. The projectile flies perfectly straight when I aim at 0, 0 (x and y). However as soon as I aim somewhere else the rotation gets random. A script prints the rotation of the player, the camera and the fireball the moment it is shot and they're exactly what they should be. X rotation of the player/camera, Y rotation of the camera and Z rotation = 0. If i pause the game asap the rotation is exactly what it should be but the Z is off by like 1.23457e-08 so it shouldn't even matter. The fireball is set to fly at transform.Translate(transform.forward speed Time.deltaTime); every update(). The prefab of the fireball and the player both have their rotations set at 0, 0, 0.
So yeah.... Could anyone help me? :| I'll even upload my project if that's necessary.
Answer by Latedi · Sep 11, 2012 at 11:43 PM
Oh my god I fixed it. I added Space.World to the projectile's script like this transform.Translate(transform.forward speed Time.deltaTime, Space.World); I've been debugging this for like 12 hours I'm amazed I didn't kill anyone yet :o Is it always like this when running into problems? Even after you have some/a lot of experience in Unity?
As you get more experience in Unity and get to know its systems, debugging will become easier and faster (for known problems ;). $$anonymous$$uch the same as program$$anonymous$$g with any other library.
Answer by DesiQ · Sep 09, 2012 at 09:43 PM
It's bizarre that you're messing with the player's rotation like that. What view is the game being played from? If first person, just use camera.transform.forward. If top-down, use the player's forward vector.
Hmm it's third person. Just using the transform.rotation doesn't cut it.
Edit: and yeah, the playerRotation = ... there is just what I was currently trying.
If it's third-person, then surely you're still using the camera to aim, and camera.transform.forward will cut it?
Well if I use the camera transform, wouldn't that make my fireballs fly in the wrong direction anyway? The camera is slightly angled downwards.
Edit: Uuhm it looks like about as random as previously. The camera is attached to the player and is in a fixed position behind the player's back.
Along the same vein as DesiQ, why can't you use Quaternion.identity to have the rotation default to the gameobject's in world rotation (which will give you the unadulterated value for where it's facing)?
Edit: Disregard, I can't believe I said that. I'm going to get some sleep, now.
Well it's a projectile and I want it to fly the same way as the player is ai$$anonymous$$g. If i use Quaternion.Identity all projectiles will fly in the same direction regardless of where the player is ai$$anonymous$$g.