- Home /
Question by
Kimone92 · May 07, 2016 at 04:15 PM ·
scripting problemerrorscripting beginnerscript errorvoid
Help with script
private Animator myAnimator;
[SerializeField]
private float movementSpeed;
private bool facingRight;
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
[SerializeField]
private bool airControl;
[SerializeField]
private float jumpForce;
public Rigidbody2D MyRigibody { get; set; }
public bool Attack { get; set; }
public bool Jump { get; set; }
public bool OnGround { get; set; }
// Use this for initialization
void Start ()
{
facingRight = true;
MyRigibody = GetComponent<Rigidbody2D> ();
myAnimator = GetComponent<Animator> ();
}
void Update()
{
HandleInput();
}
// Update is called once per frame
void FixedUpdate()
{
float horizontal = Input.GetAxis ("Horizontal");
OnGround = IsGrounded();
HandleMovement(horizontal);
Flip(horizontal);
HandleLayers ();
}
private void HandleMovement (float horizontal)
{
if (MyRigibody.velocity.y < 0)
{
myAnimator.SetBool ("land", true);
}
if (!Attack && (OnGround || airControl))
{
MyRigibody.velocity = new Vector2 (horizontal * movementSpeed, myAnimator.velocity.y);
}
if (Jump && MyRigibody.velocity.y == 0)
{
MyRigibody.AddForce(new Vector2(0, jumpForce));
}
myAnimator.SetFloat ("speed", Mathf.Abs (horizontal));
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.Space))
{
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
}
}
private void Flip (float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
private bool IsGrounded()
{
if (MyRigibody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
return true;
}
}
}
}
return false;
}
private void HandleLayers()
{
if (!OnGround)
{
myAnimator.SetLayerWeight (1, 1);
}
else
{
myAnimator.SetLayerWeight (1, 0);
}
}
}
screen-shot-2016-05-07-at-150451.png
(206.8 kB)
Comment
Best Answer
Answer by TBruce · May 07, 2016 at 04:24 PM
return false; was outside the function and you had an extra curly brace
public class Player : MonoBehaviour
{
private Animator myAnimator;
[SerializeField]
private float movementSpeed;
private bool facingRight;
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
[SerializeField]
private bool airControl;
[SerializeField]
private float jumpForce;
public Rigidbody2D MyRigibody { get; set; }
public bool Attack { get; set; }
public bool Jump { get; set; }
public bool OnGround { get; set; }
// Use this for initialization
void Start ()
{
facingRight = true;
MyRigibody = GetComponent<Rigidbody2D> ();
myAnimator = GetComponent<Animator> ();
}
void Update()
{
HandleInput();
}
// Update is called once per frame
void FixedUpdate()
{
float horizontal = Input.GetAxis ("Horizontal");
OnGround = IsGrounded();
HandleMovement(horizontal);
Flip(horizontal);
HandleLayers ();
}
private void HandleMovement (float horizontal)
{
if (MyRigibody.velocity.y < 0)
{
myAnimator.SetBool ("land", true);
}
if (!Attack && (OnGround || airControl))
{
MyRigibody.velocity = new Vector2 (horizontal * movementSpeed, myAnimator.velocity.y);
}
if (Jump && MyRigibody.velocity.y == 0)
{
MyRigibody.AddForce(new Vector2(0, jumpForce));
}
myAnimator.SetFloat ("speed", Mathf.Abs (horizontal));
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.Space))
{
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
}
}
private void Flip (float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
private bool IsGrounded()
{
if (MyRigibody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
return true;
}
}
}
return false;
}
private void HandleLayers()
{
if (!OnGround)
{
myAnimator.SetLayerWeight (1, 1);
}
else
{
myAnimator.SetLayerWeight (1, 0);
}
}
}
I've converted your answer to a comment. In the future please use the comment button if you aren't sharing a possible solution.
What do you mean not sharing solution? $$anonymous$$y answer was shared with everyone.
Your answer
Follow this Question
Related Questions
Can't add script to anything error 1 Answer
How do I make do I make a variable go back to its original value for a spell system 1 Answer
Rocket Projectiles in Flight Simulator error 1 Answer
weird problem with scripting 1 Answer
How to know exactly which game objects and objects are not referenced ? 1 Answer