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 Vizago · Jul 08, 2020 at 06:19 AM · 2d gamemovement script2d-physicsmovements

Snake Movement Problem

Well i have these code that should've make my snake move, for note i follow a youtube tutorial Here Is The Link. Well i'm facing a problem as he did at the 58.48 Minute, but i didn't see him giving a solution for it, so i tried it my self. But i ended up with no solution as well, please if you guys could help me i appreciate it so much. I've been stuck in this for weeks already :"

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SnakeMovement : MonoBehaviour
 {
     [Header("Manager : ")]
     [SerializeField] private GameController gameController;
 
     [Space]
 
     [Header("Snake Properties : ")]
     [SerializeField] public List<Transform> m_BodyParts = new List<Transform>();
     [SerializeField] private float m_MinDistance = 0.25f;
     [SerializeField] private float m_DistanceBetweenBodyParts;
     [SerializeField] private int m_InitialBody;
     [SerializeField] public float m_Speed = 7.0f;
     [SerializeField] private float m_RotationSpeed = 50.0f;
     [SerializeField] private float m_LerpTimeX;
     [SerializeField] private float m_LerpTimeY;
 
     [Space]
 
     [Header("Snake Head Prefab : ")]
     public GameObject m_BodyPrefab;
 
     [Space]
 
     [Header("Parts TextAmount Management : ")]
     public TextMesh m_PartsAmountTextMesh;
 
     [Space]
 
     [Header("Mouse Control :")]
 
     Vector2 m_MousePreviousPosition;
     Vector2 m_MouseCurrentPostion;
 
     [Space]
 
     [Header("Particle System : ")]
 
     public ParticleSystem m_SnakeParticle;
 
     /// <summary>
     /// PrivateField
     /// </summary>
 
     private Transform m_CurrentBodyPart;
     private Transform m_PreviousBodyPart;
     private bool m_bFirstPart = true;
     private Vector3 m_LastPosition;
     private float m_TimeRemaining = 10.0f;
 
     private void Awake()
     {
         m_bFirstPart = true;
 
         if (m_bFirstPart)
             Invoke("SpawnBodyPart", 0.1f);
 
     }
 
     private void Start()
     {
 
         m_LastPosition = m_BodyParts[0].transform.position;
 
     }
 
     private void Update()
     {
 
         if (GameController.m_GameState == GameController.GameState.GAME)
         {
 
             if (m_PartsAmountTextMesh != null)
                 m_PartsAmountTextMesh.text = m_BodyParts.Count + "";
 
             if (m_BodyParts.Count >= 1)
                 m_PartsAmountTextMesh.transform.position = new Vector3(m_BodyParts[0].position.x, m_BodyParts[0].position.y + 0.5f, 0);
 
             if (m_BodyParts.Count == 0 && m_bFirstPart == false)
                 gameController.SetGameOver();
         }
 
         float Distance = Vector3.Distance(m_LastPosition, m_BodyParts[0].position);
 
         if (m_TimeRemaining > Time.deltaTime)
         {
 
             m_TimeRemaining -= Time.deltaTime;
 
         }
         else
         {
             
 
             Debug.Log(Distance);
             m_TimeRemaining = 10.0f;
 
             m_LastPosition = m_BodyParts[0].position;
         }
     }
 
     private void FixedUpdate()
     {
 
         if (GameController.m_GameState == GameController.GameState.GAME) Movement();
 
     }
 
     public void SpawnBodyPart()
     {
 
             for (int i = 0; i < m_InitialBody; i++)
                 Invoke("AddBodyPart", 0.1f);
 
     }
 
     public void Movement()
     {
 
         float l_CurSpeed = m_Speed;
         float l_MaxX = Camera.main.orthographicSize * Screen.width / Screen.height;
 
         if (m_BodyParts.Count > 0)
             m_BodyParts[0].Translate(Vector2.up * l_CurSpeed * Time.smoothDeltaTime);
 
         if (m_BodyParts.Count > 0)
         {
 
             if (m_BodyParts[0].position.x > l_MaxX)
                 m_BodyParts[0].position = new Vector3(l_MaxX - 0.1f, m_BodyParts[0].position.y, m_BodyParts[0].position.z);
 
             else if (m_BodyParts[0].position.x < -l_MaxX)  
                 m_BodyParts[0].position = new Vector3(-l_MaxX + 0.1f, m_BodyParts[0].position.y, m_BodyParts[0].position.z);
         
         }
 
         if (Input.GetMouseButtonDown(0))
             m_MousePreviousPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         else if (Input.GetMouseButton(0))
         {
 
             if (m_BodyParts.Count > 0 && Mathf.Abs(m_BodyParts[0].position.x) < l_MaxX)
             {
                 m_MouseCurrentPostion = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
                 float l_DeltaMousePosition =  Mathf.Abs(m_MousePreviousPosition.x - m_MouseCurrentPostion.x );
                 float l_Sign = Mathf.Sign(m_MousePreviousPosition.x - m_MouseCurrentPostion.x);
 
                 Vector2 Position = (Vector2.right * m_RotationSpeed * l_DeltaMousePosition * -l_Sign);
                 m_BodyParts[0].GetComponent<Rigidbody2D>().AddForce(Position * l_CurSpeed);
                 m_MousePreviousPosition = m_MouseCurrentPostion;
 
             }
             else if (m_BodyParts.Count > 0 && m_BodyParts[0].position.x > l_MaxX)
                 m_BodyParts[0].position = new Vector3(l_MaxX - 1f, m_BodyParts[0].position.y, m_BodyParts[0].position.z);
 
             else if (m_BodyParts.Count > 0 && m_BodyParts[0].position.x < l_MaxX)
                 m_BodyParts[0].position = new Vector3(-l_MaxX + 1f, m_BodyParts[0].position.y, m_BodyParts[0].position.z);
 
         }
 
         /*
          * To Make The Rest Of The Body Following The Head
          */
         for (int i = 1; i < m_BodyParts.Count; i++)
         { 
 
                 m_CurrentBodyPart = m_BodyParts[i];
 
                 if (m_BodyParts.Count > 1)
                     m_PreviousBodyPart = m_BodyParts[i - 1];
 
                 Vector3 l_NewPost = m_PreviousBodyPart.position;
 
                 l_NewPost.z = m_BodyParts[0].position.z;
 
                 Vector3 l_Position = m_CurrentBodyPart.position;
 
                 l_Position.x = Mathf.Lerp(l_Position.x, l_NewPost.x, m_LerpTimeX);
                 l_Position.y = Mathf.Lerp(l_Position.y, l_NewPost.y - m_DistanceBetweenBodyParts, m_LerpTimeY);
 
                 m_CurrentBodyPart.position = l_Position;
                 
         }
 
     }
 
     /// <summary>
     /// Adding Body Part To Snake Body
     /// </summary>
     public void AddBodyPart()
     {
 
         Transform l_NewPart;
 
         if (m_bFirstPart)
         {
 
             l_NewPart = (Instantiate(m_BodyPrefab, new Vector3(0, 0, 0), Quaternion.identity) as GameObject).transform;
 
             m_PartsAmountTextMesh.transform.parent.position = l_NewPart.position + new Vector3(0, 1.5f, 0);
 
             m_bFirstPart = false;
             Debug.Log(m_bFirstPart);
 
         }
         else
         {
 
             l_NewPart = (Instantiate(m_BodyPrefab, m_BodyParts[m_BodyParts.Count - 1].position, m_BodyParts[m_BodyParts.Count - 1].rotation) as GameObject).transform;
 
             l_NewPart.SetParent(transform);
 
         }
 
         m_BodyParts.Add(l_NewPart);
 
 
     }
 }
 


Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

171 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 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

[2D] Moving the player 1 tile at a time using rigidbody movement 0 Answers

How to have coins burst out in random directions after destroying an object in unity? 2 Answers

Stuttering in simple 2D game using interpolation? 1 Answer

To Use or Not to Use 3D Physics in 2D Game 1 Answer

OnTriggerEnter2D(Collider2D other) 2 Answers


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