The question is answered, right answer was accepted
C# script broke, I can't fix it
Hello all!
I'm making a 2D platformer game, and everything has been going well, until I added my jump sound effect.
For some reason, it affected the script so that when you try to move you can't. And after attempting to press the left or right arrows (quickly) you can only move in that direction and can't change it.
I don't get what's wrong with it. I tried starting the script again, but the exact same thing happened again. I have no idea about what happened, because I only added one line of code to the jump function, which has nothing to do with the movement. Here is the code:
PlayerController.cs:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public float jumpHeight;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
private bool doubleJumped;
private Animator anim;
public Transform firePoint;
public GameObject projectile;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
void FixedUpdate() {
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
}
// Update is called once per frame
void Update()
{
if (grounded)
doubleJumped = false;
anim.SetBool("Grounded", grounded);
if (Input.GetKeyDown(KeyCode.Space) && grounded)
{
//rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpHeight);
Jump();
}
if (Input.GetKeyDown(KeyCode.Space) && !doubleJumped && !grounded)
{
//rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpHeight);
Jump();
doubleJumped = true;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
rigidbody2D.velocity = new Vector2(-moveSpeed, rigidbody2D.velocity.y);
if (Input.GetKey(KeyCode.RightArrow))
{
rigidbody2D.velocity = new Vector2(moveSpeed, rigidbody2D.velocity.y);
anim.SetFloat("Speed", Mathf.Abs(rigidbody2D.velocity.x));
if (rigidbody2D.velocity.x > 0)
transform.localScale = new Vector3(0.3294535f, 0.3294535f, 0.3294535f);
else if (rigidbody2D.velocity.x < 0)
transform.localScale = new Vector3(-0.3294535f, 0.3294535f, 0.3294535f);
if (Input.GetKeyDown(KeyCode.Return))
{
Instantiate(projectile, firePoint.position, firePoint.rotation);
}
}
}
}
void Jump() {
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpHeight);
audio.Play();
}
}
I have the AudioSource on my Player object, and this code returns no errors. When it was working fine, the movement was done so that you push the appropriate key and in moves in that direction, then stops when you release said key. I explained what is wrong above the code.
Thanks in advance!
The title is meant to say 'can't' and not that jiibberish
I think something else happened beyond the audio source.
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))
{
rigidbody2D.velocity = new Vector2(-moveSpeed, rigidbody2D.velocity.y);
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
{
This code will only check for right arrow if Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow) returns true. $$anonymous$$aybe two of your } somehow ended up further down in the script than they should be?
The Answer from earlier told me that, which worked for some of it, then I just realised that I had friction down to 0 so it was just sliding around. I fixed that.
Aye, I was overtaken by $$anonymous$$jell while my post was in moderation ... new user so my posts need to be looked over by a moderator before they show up, in case I get all offensive and such. We all know how offensive programmers can be!
Answer by Kjell-Andersson · Oct 19, 2015 at 06:16 PM
Your curl brackets are wrong. You don't want the if (Input.GetKey(KeyCode.RightArrow)) inside the if (Input.GetKey(KeyCode.LeftArrow)) since then you would have to press both the Right and the Left button at once.
To get to the if (Input.GetKeyDown(KeyCode.Return)) statement you have to hold down all three.
Try this instead:
if (Input.GetKey(KeyCode.LeftArrow))
{
rigidbody2D.velocity = new Vector2(-moveSpeed, rigidbody2D.velocity.y);
}
else if (Input.GetKey(KeyCode.RightArrow))
{
rigidbody2D.velocity = new Vector2(moveSpeed, rigidbody2D.velocity.y);
}
else
{
rigidbody2D.velocity = new Vector2(0, rigidbody2D.velocity.y);
}
anim.SetFloat("Speed", Mathf.Abs(rigidbody2D.velocity.x));
if (rigidbody2D.velocity.x > 0)
transform.localScale = new Vector3(0.3294535f, 0.3294535f, 0.3294535f);
else if (rigidbody2D.velocity.x < 0)
transform.localScale = new Vector3(-0.3294535f, 0.3294535f, 0.3294535f);
if (Input.GetKeyDown(KeyCode.Return))
{
Instantiate(projectile, firePoint.position, firePoint.rotation);
}
I've used that code and it works like it did now, but there is still one problem. When you start moving, you don't stop until something stops you. Any ideas?
If your just looking to move the object in a simple character movement script, try using transform.position += new vector2 () ins$$anonymous$$d of adding velocity and force.
Alternatively try using the += and -= to use its current velocity in the parameter for your vector 2's
If im right this should slow you down and speed you up. You will have to put a limiter on your velocity still