- Home /
Cannot instantiate rotation on Prefab
Evening, all.
I am creating a 2D platformer game in which the player shoots in the direction of where the mouse is pointing at. I am trying to set the rotation of an instantiated bullet prefab in the direction of where the mouse is pointing. The bullet will correctly travel at _deltaX in the X direction, and _deltaY in the Y direction, but will NOT be instantiated in the direction that I specify.
This ShootController.cs class is attached to the Player. The Player does have their RigidBody rotations frozen though.
Any advice would be greatly appreciated!
public class ShootController : MonoBehaviour {
public GameObject bullet;
GameObject playerObject;
//Attributes for finding the Angle of Attack
private float _deltaX;
private float _deltaY;
private float _radians;
private float _degrees;
void Start ()
{
// Assigns the player position values
playerPosX = Screen.width * 0.5f;
playerPosY = Screen.height * 0.5f - 50.0f; // This is to properly align the origin
}
void Update ()
{
// When the PLAYER clicks and Shoots
if (Input.GetMouseButtonDown(0))
{
_deltaX = Input.mousePosition.x - playerPosX;
_deltaY = Input.mousePosition.y - playerPosY;
ShootNormalShot();
}
}
void ShootNormalShot(){
_radians = Mathf.Atan(_deltaY/_deltaX);
_degrees = _radians * Mathf.Rad2Deg;
Vector3 angle = new Vector3(0f,0f, _degrees);
GameObject projectile = (GameObject)GameObject.Instantiate(bullet, playerObject.transform.position, Quaternion.Euler(angle));
projectile.GetComponent().Initialize(_deltaX, _deltaY, _normalDamage, _MaxSpeed);
}
}
Answer by xKroniK13x · Jul 06, 2012 at 04:39 PM
Why not something like:
GameObject projectile = (GameObject)GameObject.Instantiate(bullet, playerObject.transform.position, Quaternion.Euler(_deltaX,_deltaY,degrees));
$$anonymous$$mm. I only want rotation along the Z axis. _deltaX and _deltaY are used to calculate the difference in position from the mouse and the player. That is then converted into degrees which are used to rotate the object.
I tried a variation to your suggestion but it had no effect:
GameObject projectile = (GameObject)GameObject.Instantiate(normalBolt, playerObject.transform.position, Quaternion.Euler(0f,0f,_degrees));