- Home /
Joystick not firing projectiles from correct location?
I'm completely stumped if anyone can possibly help. i have no clue what im doing wron.g. everything works great with keyboard controls. but when i added on-screen joystick, it works, moves my character, but my projectiles are now firing from the center of my world not my characters spawn point. im not sure if its an error in my firing code or something wrong with how im using the joystick. here is the 3 chunks of my code that i believe the issue is probably in. any help would be greatly appreciated. Thank you in advance :)
//Character Control
CharacterController controller = GetComponent<CharacterController>();
// Rotate around y - axis
transform.Rotate(0, CrossPlatformInputManager.GetAxis("Horizontal") * rotateSpeed, 0);
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
// Move forward backward
Vector3 forward = transform.TransformDirection(Vector3.forward);
float curSpeed = speed * CrossPlatformInputManager.GetAxis("Vertical");
//float curSpeed = speed * Input.GetAxis("Vertical");
controller.SimpleMove(forward * curSpeed);
//Attack if (MainCharacter.CUREP >= 3) { if (Input.GetKeyDown("1")) { AttackFireball(); } } if (MainCharacter.CUREP >= 10) { if (Input.GetKeyDown("2")) { AttackEBall(); } } if (MainCharacter.CUREP >= 50) { if (Input.GetKeyDown("3")) { AttackBomb(); }
public void AttackFireball() { Transform bullit = Instantiate(Fireball, transform.Find("spawnpoint").transform.position, Quaternion.identity); bullit.gameObject.tag = "ffball"; bullit.GetComponent().AddForce(transform.forward * 2000); MainCharacter.CUREP -= 3; }
the only changes i made were, i stopped the keyboard vertical line and added the 2 crossplatformmanager lines.
Answer by Kishotta · Mar 19, 2017 at 10:50 PM
Your code should work fine.
Are you sure the spawnpoint object is a child of the player gameobject? It might be better to use a Transform variable in your code to keep track of the spawnpoint as Find is very slow.
Thank you very much! And yes the spawn point was a child. lol I'm not quite sure how making a transform variable solved it but joystick and projectiles are working after the change :)