Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by enricogp · Aug 01, 2020 at 03:20 AM · snapsnappingsnakesticksticking

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AllTheGoodNamesWereTaken · Aug 01, 2020 at 02:48 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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. alt text

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.


snake.png (5.3 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Create Grid and Snap Rigidbodies to Grid 1 Answer

Camera animation issue 0 Answers

[Closed]Player Sticking to Floor in Zero Grav 1 Answer

white line when snapping objects 0 Answers

Snap object to grid with offset? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges