- Home /
Moving the head of the snake
I am newbie on Unity 3d.I have started practising with a snake game. I have to move snake head forward first.If i press any key then the snake should start moving ahead.For its head i have taken simply a cube. Here is the code. Please tell me where i am doing mistake.
public class SnakeMove: MonoBehaviour {
public bool Move_Up;
public bool Move_Below ;
public bool Move_Right;
public bool Move_Left;
public body first_body;
public float time_movement = .5F;
public float following_movement;
// Use this for initialization
void Start () {
Move_Up = false;
Move_Below = false;
Move_Right = false;
Move_Left= false;
following_movement = Time.time + time_movement;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Move_Up = true;
Move_Below = false;
Move_Right = false;
Move_Left = false;
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
Move_Up = false;
Move_Below = true;
Move_Right = false;
Move_Left = false;
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
Move_Up = false;
Move_Below = false;
Move_Right = true;
Move_Left = false;
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
Move_Up = false;
Move_Below = false;
Move_Right = false;
Move_Left = true;
}
if (Time.time > following_movement)
{
MoveHead();
}
}
void MoveHead()
{
if (Move_Up)
{
first_body.move(this.transform);
this.transform.position += transform.forward *transform.localScale.z;
}
if (Move_Below)
{
first_body.move(this.transform);
this.transform.position += -transform.forward * transform.localScale.z;
}
if (Move_Right)
{
first_body.move(this.transform);
this.transform.position += transform.right * transform.localScale.z;
}
if (Move_Left)
{
first_body.move(this.transform);
this.transform.position += -transform.right * transform.localScale.z;
}
following_movement = Time.time + time_movement;
}
}
The error is:
Assets/Scripts/SnakeMotion.cs(92,8): error CS0246: The type or namespace name `body' could not be found. Are you missing a using directive or an assembly reference?
Should have I to make a body script also? N what should be included in that script? Or Can i run without making separate Body script also? Sorry for my english.
Answer by Kleptomaniac · Mar 07, 2012 at 11:09 AM
I'm not super great with C#, but I think it's pretty simple. You're problem is here: public body first_body; You are declaring a variable of type body, however no type of that kind exists. I'm guessing from your code that what you'd want instead (remember I've barely ever used C#): public GameObject first_body; That should work.
Also, though, why are you doing all your movement in another function which is only called as a result of Time.time? You should really be doing all your movement in Update() instead of using the complicated boolean structure you have there. However I would be of no help reformatting your code, because of my lack of knowledge in the language ... ;)
Anyway, I have no idea if that helped. Hopefully it did. I should really get to learning C# properly ... I'm a JScript man first and foremost, I suppose ...
Klep
Answer by Meltdown · Mar 07, 2012 at 12:45 PM
Your problem is this line..
public body first_body;
'body' is not a recognised type. Either you need to create a class for it, or if it's a gameobject change the line to..
public GameObject first_body;
Your answer
Follow this Question
Related Questions
How to make a stargate work ?? 1 Answer
Flash player slows down when unity player is installed 0 Answers
Assigned variable 1 Answer
Unity3D Crashes when baking terrain 1 Answer
Monodevelope auto complete problem? 1 Answer