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
1
Question by servetu · May 01, 2015 at 05:29 PM · directiondistancefollowsnaketail

Tail follow object - getting the point behind an object

Hi everyone, I have a problem with making body parts of a "snake" follow the head object.

A picture is worth a thousand words, so here's one: alt text

Here's the code I have:

 public void moveHead(){
         mousePosition = Input.mousePosition;
         mousePosition = Camera.main.ScreenToWorldPoint (mousePosition);
         transform.position = Vector2.Lerp (transform.position, mousePosition, moveSpeed);
         
         directionTransform = Quaternion.LookRotation(transform.position - mousePosition,Vector3.forward);
         directionTransform.x = 0.0f;
         directionTransform.y = 0.0f;
         transform.rotation = Quaternion.Slerp (transform.rotation, directionTransform, Time.deltaTime * 8);
     }
 
     public void moveBodyParts(){
 
         for (int i = 1; i < SnakeBody.Count; i++) {
             **SnakeBody[i].transform.position = Vector2.Lerp (SnakeBody[i].transform.position,
                                                             (SnakeBody[i-1].transform.position-transform.forward*2)
                                                             , moveSpeed);**
         }

and a thread I have used to come up with the erroneous line: http://answers.unity3d.com/questions/722047/get-the-position-behind-an-object-locally-.html

I am pretty sure this is where I mess things up:

 SnakeBody[i].transform.position = Vector2.Lerp (SnakeBody[i].transform.position,
                                                                 (SnakeBody[i-1].transform.position-transform.forward*2)
                                                                 , moveSpeed);

What I aim to do is to move each "circle" to the (previous one's position-it's size*2) where 2 is some arbitrary number that should be the final distance between the two objects.

Would love some pointers on this :) Cheers!

tailfollowproblemfigure1.jpg (89.6 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 servetu · May 01, 2015 at 03:57 PM 0
Share

also tried this, results in the same effect:

 SnakeBody[i].transform.position = Vector2.Lerp (SnakeBody[i].transform.position,
                                                             (SnakeBody[i-1].transform.position-SnakeBody[i-1].transform.forward)
                                                             , moveSpeed);
avatar image servetu · May 01, 2015 at 04:59 PM 0
Share

SOLVED:

The issue was that I was using Vector3.forward on a 2D vector :)

This fixes it:

 for (int i = 1; i < SnakeBody.Count; i++) {
             SnakeBody[i].transform.rotation = Quaternion.Slerp (SnakeBody[i].transform.rotation, directionTransform, Time.deltaTime * 8);
             SnakeBody[i].transform.position = Vector2.Lerp (SnakeBody[i].transform.position,
                                                             (SnakeBody[i-1].transform.position-(SnakeBody[i-1].transform.up))
                                                             , moveSpeed);
 }

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

2 People are following this question.

avatar image avatar image

Related Questions

Follow target script is rotating only clockwise. Something simple? 2 Answers

How to don't face player to camera direction? 1 Answer

Distance between 2 objects without their Z axis. 1 Answer

Allow to move object by the mouse position without missing any collisions 1 Answer

Lerp in direction to certain distance 3 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