Hi I want to make good feeling automatic grid mabe movement like in classic snake.
I have something but i think i could do this better.
Im retreiving nextDirection in update function and storing it in variable. Than in Start function i have Invoke(“Move”,0.5f), and in Move() function im tweening object (parent), at the end of the function Move() i have Invoke(“Move”,0.5). On child object i have little jump animation.
Everything is working, but i think it should be working better. I’m not satisfied with smoothness of the movement.
What do you think, how can I improve my movement script?
Im working on my first project.
I used this for mine, the movement reminds me of the original I played as a kid. I had a body segment prefab (just a coloured sphere)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class Manager : MonoBehaviour
{
static public Manager instance;
// create list of body segments
public List<InstantiateBody>bodySegments = new List<InstantiateBody> ();
public GameObject bodyPrefab;
// # of body segments, 2 is the minimum as 1 destroys & instantiates to quick to be seen.
public int bodyNum = 5;
// speed of snake & timer between destroy/instaniate for movement
float speed = 2 ;
float moveTimer = 0.5f;
// direction of movement for instantiation, player starts moving up the screen
public static Vector3 direction;
// player start position
private GameObject startspot;
// set exit gameobject
public GameObject exit;
// Use this for initialization
void Start ()
{
instance = this;
// get position of starting place
startspot = GameObject.Find ("Start");
// set player initial direction
direction = Vector3.forward;
}
// Update is called once per frame
void Update ()
{
// set player move direction by arrow keys
if (Input.GetKey (KeyCode.UpArrow))
direction = Vector3.forward;
if (Input.GetKey (KeyCode.DownArrow))
direction = Vector3.back;
if (Input.GetKey (KeyCode.LeftArrow))
direction = Vector3.left;
if (Input.GetKey (KeyCode.RightArrow))
direction = Vector3.right;
//decrement timer of action to replicate speed.
moveTimer -= speed * Time.deltaTime;
if (moveTimer <= 0) {
// replicate movement along 90 degree paths by destroying the body segment & removing it from the list
// at the end of the snake & instantiating one at the beginning 1 unit in the direction of movement
// from the segment at the start of the snake. Insert this new piece into position 0 of the list.
//remove last body segment in the list which will be the end of the snake
if (InstantiateBody.bodySegments.Count >= bodyNum) {
Destroy (InstantiateBody.bodySegments [InstantiateBody.bodySegments.Count - 1].gameObject);
InstantiateBody.bodySegments.RemoveAt (InstantiateBody.bodySegments.Count - 1);
}
// instantiate the body segments. If it's a new game first piece is at the start position
if (InstantiateBody.bodySegments.Count == 0) {
Instantiate (bodyPrefab, startspot.transform.position, bodyPrefab.transform.rotation);
// keep adding segments to the front of the snake
} else if (InstantiateBody.bodySegments.Count < bodyNum)
Instantiate (bodyPrefab, InstantiateBody.bodySegments [0].transform.position + direction, InstantiateBody.bodySegments [0].transform.rotation);
//reset move timer
moveTimer = 0.5f;
}
// set isTrigger for other segments, not the head, to enable collision detection if the
// snake hits itself.
for (int i = 0; i<InstantiateBody.bodySegments.Count-1; i++) {
if (i != 0) {
InstantiateBody.bodySegments *.GetComponent<Collider> ().isTrigger = true;*
}