Hi Friends,
I have a code which works for hitting the ball once, but then it cant be hit again:
Can somebody please help or advise, I would like to hit the ball a few times before it reaches its goal rather then one hit and I have to restart the level (which is what is happening now)
Appreciate you help and thank you very much!
void UpdatePlaying()
{
//Debug.Log(“<color=blue>UpdatePlaying”);
if (punchCursor.CurrentState == PunchStates.Rotate && Input.GetMouseButtonDown(0))
{
punchCursor.ChangeState(PunchStates.Power);
}
if (Input.GetMouseButtonUp(0))
{
if (punchCursor.CurrentState == PunchStates.Power)
{
punchCursor.ChangeState(PunchStates.NotShown);
ball.ChangeState(BallStates.Punched);
}
}
}
Also:
//Punch the ball
ball.RigidBall.isKinematic = false;
ball.RigidBall.AddForce(ball.TransBall.right* settings.PunchForce * punchCursor.PowerCooficient, ForceMode2D.Impulse);
ball.AudioSource.PlayOneShot(settings.PunchSound,settings.PunchVolume);
Hey there, be sure to use Code Tags when pasting code into the forums.
Could you explain a little bit more about the state system you have going on, what states are supposed to represent, and what the actual game is? Also, a more complete code sample would be very helpful in giving you quality suggestions.
Thanks very much, it is a one hit ball game in which you try to hit a target.
BUT I want another scene in which you have more than 1 shot!
I cannot work out how to enable more than 1 shot within the game, hopefully somebody here can help
/// <summary>
/// Show current game state
/// </summary>
private GameStates state = GameStates.WaitingToStart;
public GameController(Settings settings, Ball ball, PunchCursor punchCursor, AsyncProcessor asyncProcessor, CameraCtrl cameraCtrl,LevelsManager levelManager)
{
this.settings = settings;
this.ball = ball;
this.punchCursor = punchCursor;
this.asyncProcessor = asyncProcessor;
this.cameraCtrl = cameraCtrl;
this.levelManager = levelManager;
}
/// <summary>
/// Called in runtime when plays level
/// </summary>
void UpdatePlaying()
{
//Debug.Log("<color=blue>UpdatePlaying</color>");
if (punchCursor.CurrentState == PunchStates.Rotate && Input.GetMouseButtonDown(0))
{
punchCursor.ChangeState(PunchStates.Power);
}
if (Input.GetMouseButtonUp(0))
{
if (punchCursor.CurrentState == PunchStates.Power)
{
punchCursor.ChangeState(PunchStates.NotShown);
ball.ChangeState(BallStates.Punched);
}
}
}
/// <summary>
/// Called in gameover
/// </summary>
void UpdateGameOver()
{
if (state != stateCheck)
{
PrefsCenter.GameOversCount++;
if (PrefsCenter.LvlBestScore < LevelScore)
{
PrefsCenter.LvlBestScore = LevelScore;
}
ball.ChangeState(BallStates.Dead);
punchCursor.ChangeState(PunchStates.NotShown);
stateCheck = state;
}
I still don’t have a very clear idea of how the game is actually running with this logic, but if you want to allow more than one shot before the game ends, then keep a counter of how many shots the player has taken, and how many shots are allowed in a specific level. Then only declare the game is over once the counter equals the max allowed.
Thanks friend , sound like a great idea. Its basically a one hit into the ball into pot game, similar to something like quick golf
hit the ball to the target get a point, load next level do the same etc.
MISS your one shot and game over, calculates total score
But i wanted another level which lets you keep hitting the ball until you hit the target, rather then 1 hit , game over etc
Please let me know if you need any other CS code or information…
The most helpful thing would be the complete script. It’s difficult to imagine what else is going on to make this work.
Where is the code that ends the game if you miss? That is the code you’ll need to change in order to keep playing after a miss, adding to a hit count or something instead.
1 Like
Hi friend, does this help/
/// <summary>
/// Called when new game level start
/// </summary>
void StartingState()
{
ball.ChangeState(BallStates.Start);
if (punchCursor.CurrentState == PunchStates.NotShown)
{
punchCursor.ChangeState(PunchStates.Rotate);
}
menuHandler.ChangeState(MenuStates.InGameStart);
if (!PrefsCenter.IsTutorialPassed)
{
menuHandler.ChangeState(MenuStates.Tutorial);
}
else
{
menuHandler.ChangeState(MenuStates.InGameTimer);
}
state = GameStates.Playing;
stateCheck = state;
}
/// <summary>
/// Called in runtime when plays level
/// </summary>
void UpdatePlaying()
{
//Debug.Log("<color=blue>UpdatePlaying</color>");
if (punchCursor.CurrentState == PunchStates.Rotate && Input.GetMouseButtonDown(0))
{
punchCursor.ChangeState(PunchStates.Power);
}
if (Input.GetMouseButtonUp(0))
{
if (punchCursor.CurrentState == PunchStates.Power)
{
punchCursor.ChangeState(PunchStates.NotShown);
ball.ChangeState(BallStates.Punched);
}
}
}
/// <summary>
/// Called in gameover
/// </summary>
void UpdateGameOver()
{
if (state != stateCheck)
{
PrefsCenter.GameOversCount++;
if (PrefsCenter.LvlBestScore < LevelScore)
{
PrefsCenter.LvlBestScore = LevelScore;
}
ball.ChangeState(BallStates.Dead);
punchCursor.ChangeState(PunchStates.NotShown);
stateCheck = state;
}
}
#endregion
/// <summary>
/// Calls when level is completed
/// </summary>
/// <param name="isGroundTouch">Is ball touch the ground in this level</param>
private void LevelComplete(bool isGroundTouch)
{
string message = isGroundTouch ? "Hole in one": "Chip in";
menuHandler.ChangeState(MenuStates.ShowText, message);
if (!levelManager.IsLevelTest)
{
LevelScore++;
levelManager.LevelNext();
asyncProcessor.StartCoroutine(WaitAndLevelComplete(timeTxtState));
}
}