Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 stefanvlad199403 · Mar 26, 2021 at 11:12 PM · scripting problemrigidbodycolliderprefabsfollow player

How to make the bodyParts to follow same places and directions as the player?

Hello! I try to make a game like the classic snake game but on a round ground (Earth). On a plane, the script works well, but when i moved it on a sphere, the bodyparts (prefabs), are not working good as you can see in the picture ( they are going right/left, but they do not sit on the spehre and i tried already to use sphere collider, box collider, rigidbody) and i dont know really how to make it so that they will follow 100% correctly the player. Thanks for help! :/ . Here i put the script for the Player and bodyparts:

using System.Collections;

using System.Collections.Generic;

using UnityEngine; using UnityEngine.UI;

public class PlayerMovementScript : MonoBehaviour {

 protected Joystick joystick;
 public float speed;
 [SerializeField] private float turnFactor;
 [SerializeField] private Transform planet;

 public List<Transform> bodyParts = new List<Transform>();

 public float minDistance = 0.25f;

 public int beginSize;

 public float rotationSpeed = 50;

 public float timeFromLastRetry;

 public GameObject bodyprefabs;
 public Text currentScore;
 public Text scoreText;

 public GameObject deadScreen;

 private float dis;
 private Transform curBodyPart;
 private Transform PrevBodyPart;

 public bool isAlive;

  void Start()
 {
     joystick = FindObjectOfType<Joystick>();
     speed = 3f;
     StartLevel();
 }

 public void StartLevel()
 {
     timeFromLastRetry = Time.deltaTime;
     //deadScreen.SetActive(false);

     for (int i = bodyParts.Count -1; i > beginSize; i++)
     {
         Destroy(bodyParts[i].gameObject);

         bodyParts.Remove(bodyParts[i]);
     }

     bodyParts[0].position = new Vector3(0, 0.5f, 0);

     for (int i = 0; i < beginSize - 1; i++)
     {
         AddBodyPart();
     }

     bodyParts[0].position = new Vector3(0, 0.5f, 0);

     bodyParts[0].rotation = Quaternion.identity;

     currentScore.gameObject.SetActive(true);
     currentScore.text = "Score: 0";

     isAlive = true;
 }

 void Update()
 {
     if (isAlive)
           Move();

     if (Input.GetKey(KeyCode.Q))
         AddBodyPart();
     //RotateAroundPlanet();
     var rb = GetComponent<Rigidbody>();

     //rb.velocity = new Vector3(joystick.Horizontal * 100f, rb.velocity.y, joystick.Vertical * 100f);
 }

 public void Move()
 {

     float curspeed = speed;

     //if (Input.GetKey(KeyCode.W))
     //    curspeed *= 2;

     bodyParts[0].Translate(bodyParts[0].forward * curspeed * Time.smoothDeltaTime, Space.World);


     if (Input.GetAxis("Horizontal") != 0)
         bodyParts[0].Rotate(Vector3.up * rotationSpeed * Time.deltaTime * Input.GetAxis("Horizontal"));

     for (int i = 1; i < bodyParts.Count; i++)
     {

         curBodyPart = bodyParts[i];
         PrevBodyPart = bodyParts[i - 1];

         dis = Vector3.Distance(PrevBodyPart.position, curBodyPart.position);

         Vector3 newpos = PrevBodyPart.position;

         newpos.y = bodyParts[0].position.y;

         float T = Time.deltaTime * dis / minDistance * curspeed;

         if (T > 0.5f)
             T = 0.5f;
         curBodyPart.position = Vector3.Slerp(curBodyPart.position, newpos, T);
         curBodyPart.rotation = Quaternion.Slerp(curBodyPart.rotation, PrevBodyPart.rotation, T);



     }
 }
 public void AddBodyPart()
 {

     Transform newpart = (Instantiate(bodyprefabs, bodyParts[bodyParts.Count - 1].position, bodyParts[bodyParts.Count - 1].rotation) as GameObject).transform;

     newpart.SetParent(transform);

     bodyParts.Add(newpart);

     currentScore.text = "Score: " + (bodyParts.Count - beginSize).ToString();
 }

 public void Die()
 {
     isAlive = false;

     scoreText.text = "Your score was: " + (bodyParts.Count - beginSize).ToString();

     currentScore.gameObject.SetActive(false);

     deadScreen.SetActive(true);
 }

alt text

untitled.png (508.0 kB)
Comment
Add comment · Show 2
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 logicandchaos · Mar 27, 2021 at 01:24 PM 0
Share

you can try using a distance or hinge joint.

avatar image stefanvlad199403 logicandchaos · Mar 27, 2021 at 07:27 PM 0
Share

i'm a little noob and a beginner and i don't realy know what you mean by that, or what to do exactly. Can you show me please based on my code? or something like that?

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

264 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

My player's camera displays jerky objects 2 Answers

I want to make the gameobject rigidbody.iskinematic=true for 3s and immune to damage when it triggers the collider and then false again 0 Answers

Help with rigidbodies and follow transform 1 Answer

Whats the difference between a rigid body and a collider? 2 Answers

rigidbody.useGravity doesnt work? 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