Scripting Issues on the Player with Laser

@Harinezumi
I have some issues with my laser system, and many of that question was resolve in here link to the question

Another question here is about my Player script, what is wrong about it that the knockback time is too long?

using UnityEngine;
using System.Collections;

public class PlayerControllerScript : MonoBehaviour {

	public float maxSpeed = 10f;
	public float jumpForce = 700f;
	bool facingRight = true;
	Animator anim;
	bool grounded = false;
	public Transform groundCheck;
	public float groundRadius = 0.2f;
	public LayerMask whatIsGround;
	public float move;
	public float score;
	public float spawnX, spawnY;

	public int jumpConter;

	public float knockback;
	public float knockbackLength;
	public float knockbackCount;
	public bool knockFromRight;

	private Rigidbody2D rb2d;

	public float timer = 5f;

	public void Start () 
	{
		rb2d = gameObject.GetComponent<Rigidbody2D> ();

		anim = GetComponent<Animator>();

		spawnX = transform.position.x;
		spawnY = transform.position.y;
	}

	public void Update()
	{
		if (Input.GetKeyDown ("space") && jumpConter < 2) {
			if ((jumpConter == 0 && grounded) || (jumpConter == 1)) {

				anim.SetBool ("Ground", false);

				rb2d.AddForce (Vector2.up * jumpForce);

				grounded = true;

				rb2d.velocity = (new Vector2 (move * maxSpeed * Time.deltaTime, rb2d.velocity.y));

				jumpConter++;
			}
		}
	}


	public void FixedUpdate() 
	{
		grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround.value);

		float move = Input.GetAxis ("Horizontal");

		anim.SetBool ("Ground", grounded);

		anim.SetFloat ("vSpeed", rb2d.velocity.y);

		anim.SetFloat ("Speed", Mathf.Abs (move));

		if (knockbackCount <= 0) {
			rb2d.velocity = new Vector2 (move * maxSpeed, rb2d.velocity.y);
		} else {
			if (knockFromRight)
				rb2d.velocity = new Vector2 (-knockback * 2f, knockback);
			if (!knockFromRight)
				rb2d.velocity = new Vector2 (knockback * 2f, knockback);
			knockbackCount -= Time.deltaTime;
		}
		if (move > 0 && !facingRight)
			Flip ();
		else if (move < 0 && facingRight)
			Flip ();

		if (timer > 0) {
			timer -= Time.deltaTime;
			DamagePostProcessing.damage = true;
			return;
		} else {
			DamagePostProcessing.damage = false;
		}
	}


	void Flip()

	{
		facingRight = !facingRight;
		Vector3 theScale = transform.localScale;
		theScale.x *= -1;
		transform.localScale = theScale;
	}


	void OnCollisionEnter2D(Collision2D col)
	{
		if (col.gameObject.layer == LayerMask.NameToLayer ("killObjects") || col.gameObject.layer == LayerMask.NameToLayer ("dieCollider"))// || col.gameObject.layer == LayerMask.NameToLayer ("Enemy"))
			transform.position = new Vector3 (spawnX, spawnY, transform.position.z);

		jumpConter = 0;
		grounded = true;

		if (col.transform.tag == "MovingPlatform") 
		{
			transform.parent = col.transform;
		}
	}

	void OnCollisionExit2D (Collision2D col)
	{
		if (col.transform.tag == "MovingPlatform") 
		{
			transform.parent = null;
		}
	}

	void OnTriggerEnter2D(Collider2D col) 
	{
		if (col.gameObject.layer == LayerMask.NameToLayer ("food")) {
			Destroy (col.gameObject);
			score++;
		}

		if (col.gameObject.layer == LayerMask.NameToLayer ("Enemy")) {
			knockbackCount = knockbackLength;
			timer = 5f;
			if (transform.position.x < col.transform.position.x)
				knockFromRight = true;
			else
				knockFromRight = false;
		}
	}
}

If you need to here is the script that was made by the last question in the link above

I’m assuming that what you want is to move (knockback) the player when it is hit by the laser, and this effect should last knockbackCount seconds (it helps a lot if you explain what you are trying to achieve, maybe even a short description of your game).

I strongly suspect that the issue is that as long as the raycast in the OneLaserScript is hitting the player, it will add to knockbackCount, increasing more than once how long the player is knocked back. Instead of directly modifying knockbackCount from OneLaserScript, you should encapsulate the logic into PlayerControllerScript (basically hide it from outside). Something like this:

// add this to PlayerControllerScript
public void Knockback (float knockbackDuration, bool knockFromRight) {
    if (knockbackCount <= 0) { // if not being knocked back
        knockbackCount += knockBackDuration;
        this.knockFromRight = knockFromRight;
        timer += 5;
    }
}

// replace the part after PlayerControllerScript pcs = ... in OneLaserScript with this:
pcs.Knockback(1, spottedLeft);

This does basically the same thing, but is a lot cleaner.

@Harinezumi Hello again, I added a question as you said.
If you can, please try to resolve this issue, thanks!

@Harinezumi Please try to help, I think we almost get it done!