- Home /
Question by
BBoffa · Apr 15, 2020 at 12:38 AM ·
2derror2d-platformerplatformererror build
error CS0101 and error cs0111
Im making basic movement and jumping for a 2d platformer i try to add in code for jumping after im finished i have 4 errors, one on cs0101 to do with already containing a defiintion of playermovement and 3 cs0111 for each void here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour {
public Animator animator;
public bool isGrounded = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Jump();
animator.SetFloat("Horizontal", Input.GetAxis("Horizontal"));
Vector3 horizontal = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position = transform.position + horizontal * Time.deltaTime;
}
void Jump()
{
if (Input.GetButtonDown("Jump") && isGrounded == true)
{
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
}
}
}
Comment
Answer by GregSmash · Apr 16, 2020 at 03:20 AM
I'am not sure is this solved all problems, but it can help you to get the animator correctly. I hope I helped you :D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour {
private Animator _animator;
//public Animator animator;
private Rigidbody2D _rigBody;
public bool isGrounded = false;
// Start is called before the first frame update
void Start()
{ //Get animator from the player
_animator = GetComponent<Animator>();
//Get RigBod from the player
_rigBody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
Jump();
_animator.SetFloat("Horizontal", Input.GetAxis("Horizontal"));
//Here idk why a vector 3, instead vector2, if this works is good, however work in same vectors
Vector3 horizontal = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
//I add this. because the game dont know which is the transform.position gameobject, this is a way to solved. Also
//public Gameobject obj is a value that replace this.
this.transform.position = this.transform.position + horizontal * Time.deltaTime;
}
void Jump()
{
if (Input.GetButtonDown("Jump") && isGrounded == true)
{
//that knows gameobject rigidbody
_rigBody.AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612231438im_/https://answers.unity.com/themes/thub/images/avi.jpg)