Cannot Make the Dramatic Finish

Hello there,
I did all this section as a challenge and I succeed except that death kick. First I used rigidbody2d.addforce then velocity as in course (actually I know both should result same in this situation). My player not dropping that force or velocity apply on it all the time but it shouldn’t I think please give it a see.

void CheckAlive()
	{
		Vector2 lastPos;
		bool launched = false;
		if(playerCollider.IsTouchingLayers(LayerMask.GetMask("Enemy")))
		{
			Debug.Log("Player is not alive anymore");
			isAlive=false;
			lastPos=transform.position;
		}
		if(!isAlive)
		{
			int counter=0;
			if(!launched)
			{
			launched=true;
			playerRb.velocity = new Vector2(Random.Range(-20,20),Random.Range(100,240));
			Debug.Log("this method should be called once but called for " +counter);
			}
		}
	}

Hi,

Try using a Debug.Log statement after this line of code;

playerRb.velocity = new Vector2(Random.Range(-20,20),Random.Range(100,240));

…and see if the output value is what you would expect. I note, for example, you are looking for random values between ints yet a Vector2 is instantiated with floats, might not be the issue, it may be casting it happily in the background, but worth perhaps checking.

I tried debugging and this code works like update. How can I make this code work once? actually there’s a bool to do that but it isn’t working…

Is this the entirety of that method or have you left anything out? I ask because you are declaring a variable, lastPos in it which doesn’t appear to be used for anything, despite being set over and over again in that first if statement. I also notice you have another variable, counter which again is declare but not used for anything.

It would probably be useful to see the issue in context, if you want to share the project files with me Muhammed, I will take a quick look.

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.

Sorry about delay, here is the link

https://drive.google.com/file/d/1GCEtB-2YMB6nUv4eS7U6lktWi0rcoXbA/view?usp=sharing

Hi Muhammed,

I just ran your game, I needed to comment out the Debug.Log statement on line 36 of Player.cs as thing doesn’t actually exist and it was throwing a NullReferenceException.

Once that was out of the way I ran your game, climbed a ladder, ran into an enemy and was sent flying out of the screen. Seemed fairly dramatic. Looks to be working. Am I missing something?

Ohhh, hang on, it’s not that you can’t make it move, the problem is that you can’t make it stop moving correct?


Updated Wed Jan 09 2019 11:58

Ok, so I think you have maybe over-complicated things a little, you can simplify this method;

void CheckAlive()
{
	Vector2 lastPos;
	bool launched = false;
	if(playerCollider.IsTouchingLayers(LayerMask.GetMask("Enemy")))
	{
		Debug.Log("Player is not alive anymore");
		isAlive=false;
		lastPos=transform.position;
	}
	if(!isAlive)
	{
		int counter=0;
		if(!launched)
		{
		launched=true;
		playerRb.velocity = new Vector2(Random.Range(-20,20),Random.Range(100,240));
		Debug.Log("this method should be called once but called for " +counter);
		}
	}
}

to this;

void CheckAlive()
{
    if(playerCollider.IsTouchingLayers(LayerMask.GetMask("Enemy")))
    {
        isAlive=false;
        playerRb.velocity = new Vector2(Random.Range(-20, 20), Random.Range(100, 240));
    }
}

You could factor out those two lines into a separate Die method perhaps.

Works now. :slight_smile:

1 Like

Thank you for solution Rob but can you illuminate me again :smiley:

That Debugs are can be wrong because I was trying to figure the error out.
I couldn’t get why my method (which applies force) called in every frame, what was wrong?

Sure, happy to :slight_smile:

So, in here;

	if(!isAlive)
	{
		int counter=0;
		if(!launched)
		{
		launched=true;
		playerRb.velocity = new Vector2(Random.Range(-20,20),Random.Range(100,240));
		Debug.Log("this method should be called once but called for " +counter);
		}
	}

You were checking whether the isAlive bool was true or not, fairly unnecessary as you had just set it to false yourself in the code block above. Then you were using another bool, launch, to determine whether you should make the player perform his death kick or not. However, this was a local variable, so once that code had finished executing in the CheckAlive method, launch was disposed. When your Update method ran next it would call CheckAlive and, as you can see you set launched to equal false again.

So effectively not trapping the state change because you were allowing it to reset each frame.

Hope this helps :slight_smile:

1 Like

OK, I got it thank you :slight_smile:

1 Like

You’re very welcome :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms