- Home /
Make an object face forward in a snake game.
I think my issue is fairly simple but after several days of searching I have either been unable to find a solution or if I did find the correct one I have been unable to implement it properly in my code, so now I have finally broken down and have decided to ask for help.
I am making a snake-like game but I am using 3d models. The problem is that when I turn the head of the snake to a new direction I can't get it to also face in that direction. I have tried every rotation and eulerangle code have come across but none of them do what I need. I have been able to get the head object to rotate with some of them but then the control inputs don't move the head in the correct direction.
This is the code I have been using:
public class Snake : MonoBehaviour {
Vector3 dir = Vector3.left;
List<Transform> tail = new List<Transform>();
bool ate = false;
public GameObject tailPreFab;
void Start () {
InvokeRepeating("Move", 0.3f, 0.5f);
}
void Update () {
if (Input.GetKey (KeyCode.A)) {
dir = Vector3.right;
} if (Input.GetKey (KeyCode.W)) {
dir = Vector3.back;
} if (Input.GetKey (KeyCode.D)) {
dir = Vector3.left;
} if (Input.GetKey (KeyCode.S)) {
dir = Vector3.forward;
}
}
public void Move () {
Vector3 v = transform.position;
// Quaternion q = transform.rotation;
transform.Translate (dir);
if (ate) {
GameObject g = (GameObject)Instantiate (tailPreFab, v, Quaternion.identity);
tail.Insert (0, g.transform);
ate = false;
}
if (tail.Count > 0) {
tail.Last ().position = v;
tail.Insert (0, tail.Last ());
tail.RemoveAt (tail.Count - 1);
}
}
void OnTriggerEnter(Collider coll) {
if (coll.name.StartsWith("Food")) {
ate = true;
Destroy (coll.gameObject);
}
if (coll.name.StartsWith("Wall")) {
CancelInvoke ("TimerInvoke");
Exit ();
}
if (coll.name.StartsWith("Chick")) {
CancelInvoke ("TimerInvoke");
Exit ();
}
}
public void Exit () {
SceneManager.LoadScene (1);
}
}
I suspect my issues might stem from using dir = Vector3.(direction) but I can't figure out a better way to control the direction of the snake.
Sorry is this is vague or if the answer is obvious. This is the first project I have taken on myself so my knowledge of Unity and C# are still pretty weak.
Thanks in advance.
Answer by Runemark · Sep 28, 2017 at 12:55 PM
Easiest way:
transform.Translate (dir);
// Add this part
Vector3 lookPos = transform.position + dir * 100;
transform.LookAt(lookPos)
This will make your transform look at the point that is 100 unit far from it in the given direction.
I just tried this one my lunch break and it reacted similarly to the other things I have tried. It does rotate the object but it also messes up the direction the object moves when I use WASD.
I imagined your game like the classic sneak game, where the camera is looking to the playground from top. If you are using a camera that follows the sneak head, you have to change the direction from global direction vectors to local ones.
You can do it by changing Vector3.forward to transform.forward, and the same with other directions. (If I misunderstood the problem, sorry its early in the morning. :D also pls attach an image about your game screen if this is the case! :))
Answer by knorke · Sep 28, 2017 at 12:28 PM
Maybe this will help you out:
https://unity3d.com/de/learn/tutorials/topics/multiplayer-networking/creating-player-movement-single-player