Saloame
1
Hello,
I am total beginer and need help because I have no idea what mistake i done with script.
My script:
using UnityEngine;
using System.Collections;
public class controler : MonoBehaviour
{
public float maxspeed = 10f;
bool facingRight = true;
void Start ()
{}
void FixedUpdate ()
{
float move = Input.GetAxis ("Horizontal");
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
if (move > 0 &&!facingRight)
Flip ();
else if (move <0 && facingRight)
Flip ();
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = tranform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Here you go:
using UnityEngine;
using System.Collections;
public class controler : MonoBehaviour {
public float maxspeed = 10f;
bool facingRight = true;
void Start ()
{}
void FixedUpdate ()
{
float move = Input.GetAxis ("Horizontal");
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
if (move > 0 &&!facingRight)
Flip ();
else if (move <0 && facingRight)
Flip ();
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = tranform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
You just needed to put your Flip method outside your FixedUpdate.
I think you have a wiggly bracket “}” missing on line 14
you actually have loads of wiggly brackets missing in terms of code understandability imho:
a pile of
if (whatever)
dowhatever();
if (whateverelse)
dowhateverelse();
elseif(blabla)
moo();
is fairly hellish in the long run , use:
if(whatever){
dowhatever();
}
if (whateverelse){
dowhateverelse();
}else if(blabla){
moo();
}
then you won’t make mistakes.
note: usu folks give an extra line for each “{” etc.