I’m working on a small project for a class, I accidentally downloaded some assets from the store that screwed up the entire project. I’m almost done fixing it, but I can’t get my character to move left or right on keyboard, but can get them to jump and shoot. This is the code I’m working with.
using UnityEngine;
public class CharacterController2D : MonoBehaviour {
public GameObject bulletToRight, bulletToLeft;
Vector2 bulletPos;
public float fireRate = 0.5f;
float nextFire = 0.0f;
bool facingRight = true;
public float maxSpeed = 10f;
Animator anim;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 180f;
bool doubleJump = false;
public AudioClip coinSound;
public AudioClip shot1;
void Start ()
{
anim = GetComponent<Animator>();
}
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool("Ground", grounded);
if(grounded)
doubleJump = false;
anim.SetFloat ("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
float move = Input.GetAxis ("Horizontal");
anim.SetFloat("Speed", Mathf.Abs(move));
GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if(move > 0 &&!facingRight)
Flip ();
else if(move < 0 && facingRight)
Flip ();
if (Input.GetButton("Run"))
{
maxSpeed = 4f;
anim.SetFloat("Running",Mathf.Abs(move));
}
else if(!Input.GetButton("Run")){
maxSpeed = 4f;
}
}
void Update()
{
if ((grounded || !doubleJump) && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("Ground", false);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
if (!doubleJump & !grounded)
doubleJump = true;
}
bool shoot = Input.GetKeyDown(KeyCode.F);
anim.SetBool("Shooting", false);
if (shoot && Time.time > nextFire)
{
GetComponent<AudioSource>().PlayOneShot(shot1);
anim.SetBool("Shooting", true);
nextFire = Time.time + fireRate;
fire();
}
}
void fire()
{
bulletPos = transform.position;
if (facingRight)
{
bulletPos += new Vector2(+0.8f, -0.43f);
Instantiate(bulletToRight, bulletPos, Quaternion.identity);
} else
{
bulletPos += new Vector2(-0.8f, -0.43f);
Instantiate(bulletToLeft, bulletPos, Quaternion.identity);
}
}
public void FinishLevel()
{
SoundHelper.Instance.MakeLevelFinishSound();
}
private void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Coin")
{
GameManager.Instance.CollectedCoins++;
GetComponent<AudioSource>().PlayOneShot(coinSound);
Destroy(col.gameObject);
}
}
public void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Platforms")
{
this.transform.SetParent(other.transform);
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
},Im making a small game for a project im doing, I accidentally installed assets from the store that screwed something up with my game. I’m almost done fixing it but I can’t move my character left and right and I don’t know where to fix it. This is the C# code im working with.
using UnityEngine;
public class CharacterController2D : MonoBehaviour {
public GameObject bulletToRight, bulletToLeft;
Vector2 bulletPos;
public float fireRate = 0.5f;
float nextFire = 0.0f;
bool facingRight = true;
public float maxSpeed = 10f;
Animator anim;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 180f;
bool doubleJump = false;
public AudioClip coinSound;
public AudioClip shot1;
void Start ()
{
anim = GetComponent<Animator>();
}
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool("Ground", grounded);
if(grounded)
doubleJump = false;
anim.SetFloat ("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
float move = Input.GetAxis ("Horizontal");
anim.SetFloat("Speed", Mathf.Abs(move));
GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if(move > 0 &&!facingRight)
Flip ();
else if(move < 0 && facingRight)
Flip ();
if (Input.GetButton("Run"))
{
maxSpeed = 4f;
anim.SetFloat("Running",Mathf.Abs(move));
}
else if(!Input.GetButton("Run")){
maxSpeed = 4f;
}
}
void Update()
{
if ((grounded || !doubleJump) && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("Ground", false);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
if (!doubleJump & !grounded)
doubleJump = true;
}
bool shoot = Input.GetKeyDown(KeyCode.F);
anim.SetBool("Shooting", false);
if (shoot && Time.time > nextFire)
{
GetComponent<AudioSource>().PlayOneShot(shot1);
anim.SetBool("Shooting", true);
nextFire = Time.time + fireRate;
fire();
}
}
void fire()
{
bulletPos = transform.position;
if (facingRight)
{
bulletPos += new Vector2(+0.8f, -0.43f);
Instantiate(bulletToRight, bulletPos, Quaternion.identity);
} else
{
bulletPos += new Vector2(-0.8f, -0.43f);
Instantiate(bulletToLeft, bulletPos, Quaternion.identity);
}
}
public void FinishLevel()
{
SoundHelper.Instance.MakeLevelFinishSound();
}
private void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Coin")
{
GameManager.Instance.CollectedCoins++;
GetComponent<AudioSource>().PlayOneShot(coinSound);
Destroy(col.gameObject);
}
}
public void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Platforms")
{
this.transform.SetParent(other.transform);
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
},