I am a beginner(noob) creating my first 2d game on unity.
I love it and I am following a tutorial (a very good one actually) for creating a 2 player game:
But, I have few problems: first, there is no shoot delay so players can just spam.
if someone could copy my script and just do necessary changes I could just copy it again and put it in the game.But if you want to tell me the changes (write new codes and say if i should erase someothers it will be OK.
here is my code to make my player can move jump and shoot:
I have a second problem: when i jump on the side of a plateform, my player stick to walls how could I arrange this???

5274039–528282–playerController.cs (1.76 KB)
public class playerController : MonoBehaviour
{
public float moveSpeed;
public float jumpForce;
public KeyCode left;
public KeyCode right;
public KeyCode jump;
public KeyCode throwBall;
private Rigidbody2D theRB;
public Transform groundCheckPoint;
public float groundCheckRadius;
public LayerMask whatIsGround;
public bool isGrounded;
private Animator anim;
public GameObject snowBall;
public Transform throwPoint;
//add bool to see if the ball can be thrown (true at start)
bool canThrow = true;
//time between the throwing
public float throwDelay;
// Start is called before the first frame update
void Start()
{
theRB = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, whatIsGround);
if (Input.GetKey(left))
{
theRB.velocity = new Vector2(-moveSpeed, theRB.velocity.y);
}
else if (Input.GetKey(right))
{
theRB.velocity = new Vector2(moveSpeed, theRB.velocity.y);
}
else
{
theRB.velocity = new Vector2(0, theRB.velocity.y);
}
if (Input.GetKeyDown(jump) && isGrounded)
{
theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
}
//add an other check if the ball can be thrown
if (Input.GetKeyDown(throwBall) && canThrow)
{
GameObject ballClone = (GameObject)Instantiate(snowBall, throwPoint.position, throwPoint.rotation);
ballClone.transform.localScale = transform.localScale;
anim.SetTrigger("Throw");
//switch the bool to false to prevent the throw
canThrow = false;
//start the coroutine which can be paused
StartCoroutine(WaitForThrowDelay());
}
if (theRB.velocity.x < 0)
{
transform.localScale = new Vector3(-1, 1, 1);
}
else if (theRB.velocity.x > 0)
{
transform.localScale = new Vector3(1, 1, 1);
}
anim.SetFloat("Speed", Mathf.Abs(theRB.velocity.x));
anim.SetBool("Grounded", isGrounded);
}
//coroutine
IEnumerator WaitForThrowDelay ()
{
//pause it for the delay time
yield return new WaitForSeconds(throwDelay);
//enable the throwing again
canThrow = true;
}
}
1 Like
I tried and it worked soooo well without any problem best script ever, @vakabaka
thank you very much wou make me love unity such a nice comunity ^^