- Home /
How to perfecly snap two objects (control key is not working)
I'm trying to do a snake-like game. The snake's head is a cylinder game object, and every time the player touches food, another cylinder object must appear right before the previous one, and so on. But I don't know how to spawn a new snake "body segment" after the head because I don't know the size of the cylinder so that I can spawn something right next to it (like "glued togheter). I tried, in the inspector, holding the control key and then snapping a body segment into the head, then taking note of it's position in relation to the head's position to know exactly how much distant it must spawn from the other gameobject in order to be "glued" togheter. The problem is that, when I make those objects start moving in one direction (same for both, at the same speed, they should still be stuck togheter), a little vain between then appears after they move a certain amount. I believe this indicates that they weren't perfectly next to each other to begin with, and then the difference started to be noticiable when they started to move and then stray apart. Anyone know how this can be solved?
If you can add any pictures that would help. I would try coding it so the transform of both objects are always a certain distance so they stay stuck together. Do they both have a Rigidbody? Collider? Joints?
Answer by Kaart · Aug 01, 2020 at 04:00 PM
You can use the collider on the snake segments to get their size. Use the example code bellow to spawn a new segment in the correct position.
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class SnakeExample : MonoBehaviour
{
[SerializeField]
private GameObject _segmentPrefab;
List<GameObject> _segments = new List<GameObject>();
private void Start()
{
//First we add the snakes head to the list of segments.
_segments.Add(gameObject);
// Add new segments.
AddSegment();
AddSegment();
}
private void AddSegment()
{
GameObject lastSegment = _segments.Last();
Vector3 lastSegmentPosition = lastSegment.transform.position;
Vector3 lastSegmentSize = lastSegment.GetComponent<Collider>().bounds.size;
// Position for the new snake segment. This example only works when forward in the z axis.
// TODO: Add some checks and code for when moving in other directions.
// You can spawn the new segment a bit inside the last segment, this looks more realistic
// and prevents them from getting disjointed.
// Use Layer-based collision detection to disable collisions between the segments.
Vector3 position = new Vector3(lastSegmentPosition.x, lastSegmentPosition.y, lastSegmentPosition.z - lastSegmentSize.z);
// Spawns a new segment in the correct position as a child of the snakes head.
// Add it to the list of segments.
GameObject segment = Instantiate(_segmentPrefab, position, transform.rotation, transform);
_segments.Add(segment);
}
}
This gives the following result.
You should a some code to determine the position of the new segment depending on the direction the snake is moving. You can also spawn these segments into each other a bit and disable collision between the segments. This would prevent the segments getting disconnected of each other. Hope this helps.
Your answer
![](https://koobas.hobune.stream/wayback/20220613004608im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to snap objects in game 2 Answers
Snapping to Grids 0 Answers
Creating objects and positioning next to a previous one 0 Answers
How to snap two objects together? 1 Answer
cannot get snap to grid to activate 0 Answers