Ok so I got this error “A namespace cannot directly contain members such as fields or methods”, how can I solve this? Here is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerWalk : MonoBehaviour
{
private Rigidbody2D myBody;
private float speed = 2f;
void Start()
{
myBody = GetComponent();
}
// Update is called once per frame
void Update()
{
DetectInput();
}
public void MoveLeft()
{
myBody.velocity = new Vector2(-speed, myBody.velocity.y);
}
public void MoveRight()
{
myBodt.velocity = new Vector2(speed, myBody.velocity.y);
}
public void StopMoving()
{
myBody.Velocity = new Vector2(0f, myBody.velocity.y);
}
}
void DetectInput()
{
float x = Input.GetAxisRaw(“Horizontal”);
if (x > 0)
{
MoveRight();
}
else if (x < 0)
{
MoveLeft();
}
else
{
StopMoving();
}
}
eses
December 9, 2019, 11:48am
2
Hi
Its your first post, so - yeah, why not familiarize yourself with the forum etiquette?
Formatting your code blocks correctly:
If you see someone who needs to know about code tags, please link them to this post:
Please use code tags when posting code.
You can "tag" your code by typing [984347--130826--Screenshot 2015-03-16 10.33.58.png] around your code.
There is no overt "Code" tag button that automatically tags a selection as code.
That being said, however, we can Insert code directly into the post using the "Insert Code" button:
[image]
This will bring up an "Insert" window were we can paste our code and c…
Also “2D Touch Player Movement” is not a question.
My guess is that you don’t have enough of these curly braces: }
You will need to go through and make sure you have them all. What I think your error message is saying, is that you have a method or constructor living outside of your class. To fix this, insert the } in the correct place. You should have a red squiggly line somewhere in your code. That’s a good place to start looking.
Try to check the syntaxis as it seems to me that the problem is in that field.