- Home /
2D bullet script errors.
I'm currently trying to make a 2d game that allows the player to spawn and launch a bullet, however i'm getting a couple of errors. Specifically the errors are:
"Assets/RangedAttack.cs(26,61): error CS1525: Unexpected symbol `launcher'"
and
"Assets/RangedAttack.cs(54,1): error CS8025: Parsing error"
Any and all help will be greatly appreciated, here is my code:
using UnityEngine;
public class RangedAttack
{
public Bullet bullet; // reference to bullet prefab to launch
public AttackContext contextStand; // when attacking while standing
public AttackContext contextInAir; // when attacking while in air
public enum ContextId
{
eStand = 0,
eInAir
}
public class AttackContext
{
public string anim = string.Empty; // animation state name
public Vector2 bulletStartPosition; // spawn position of bullet
public float bulletDirection; // launch direction
}
void update()
{
if (CrossPlatformInput.GetButtonDown("z"))
{
FireBullet(UFGPlatformerCharacter2D launcher, ContextId contextId)
{
AttackContext curContext = contextStand;
switch(contextId)
{
case ContextId.eRun: curContext = contextRun; break;
case ContextId.eInAir: curContext = contextInAir; break;
}
// make sure move horizontal direction is valid
float fAngle = Mathf.Deg2Rad * curContext.bulletDirection;
Vector2 v2MoveDir = new Vector2(Mathf.Cos(fAngle), -Mathf.Sin(fAngle));
if(launcher.GetMotor().faceRight && v2MoveDir.x < 0f || !launcher.GetMotor().faceRight && v2MoveDir.x > 0f)
{
v2MoveDir.x = -v2MoveDir.x;
}
// spawn and launch bullet (add player velocity before spawn)
Vector2 pointPos = launcher.transform.TransformPoint(curContext.bulletStartPosition);
pointPos = pointPos + (Time.deltaTime * launcher.GetMotor().velocity);
Bullet newBullet = (Bullet)UnityEngine.Object.Instantiate(bullet, pointPos, Quaternion.identity);
// initiate bullet
v2MoveDir = launcher.transform.TransformDirection(v2MoveDir);
newBullet.Setup(launcher, pointPos, v2MoveDir);
}
}
}
}
Answer by Kiwasi · Jul 14, 2014 at 04:21 AM
What's the point of line 26?
If you are trying to declare a method then you need to add a return type first. You also need to move the code outside of update. C# doesn't support nested methods.
If you are trying to call a method you should remove the variable types from inside the parameter list.
If you copied and pasted this code from the net then you really should learn the basics of computer programming before you continue. Check out the beginner tutorials.
I have a void return type setup in my actual bullet script, this script is meant to instantiate that. But I See now that I never declared it or called it here.
Come back once you've tried it in Unity, I'm not confident we have hit all the problems yet.
Yes after removing the Update and if functions I just added public void to the firebullet method and it worked fine, but now I have some errors in my bullet code to check out. Thank you for the help though, I really appreciate it. To be honest, I submitted this problem about a week ago but it just now got approved by moderators, so I was kind of in the midst of moving on to a different ranged attack and bullet script for my game.
Sorry about the delays. There was something weird going on in the mod queue. You would think a site form computer game programmers would be able to get a website working properly.
Haha yeah totally. Oh well though, things happen. Thanks for the help friend. I'll mark the script as solved and work on something new, hopefully when I comeback for some help it won't take a week to get feedback .-.
Your answer
Follow this Question
Related Questions
Error UCE0001 ";"Expected 2 Answers
Multiplayer Script error 2 Answers
error with footsteps script 1 Answer
Main Menu Script Issues 1 Answer
Bullet Fire script not working 1 Answer