Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by Bairne · Aug 30, 2017 at 09:24 AM · movementaivariablespeedfollowing

Getting an Object that is not a child to match another objects speed

Hey Folks,

I'm having some trouble figuring this out so hopefully someone can help!

In the game i'm making I've got characters who, when touched by the player, begin to follow him (something this forum helped me work out!).

This all works fine, but as the movespeed is defined by a public int for the followers, the character falling / being moved at a higher speed than theirs means a large gap is created between them which they then slowly catch up over.

Ideally what i'd like to happen is for them to match the characters speed as it increases, i've looked into using rigidbody2D.velocity, but as the player characters rigidbody is kinematic I believe it doesn't have a velocity for them to match?

Any suggestions would be fantastic, thanks in advance guys!

Quick Edit: I don't want to make the followers children of the player as their movement becomes more rigid and locked to the players! If anyone could tell me how to make it so the followers could move more freely as children (e.g. moving up when the player jumps down a hole etc), then I would consider that as well.

Code Below:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAi : MonoBehaviour {
     //------------Variables----------------//
     public Transform target;
     public int moveSpeed;
 
     public int rotationSpeed;
     public int maxDistance;
     public int minDistance;
     private Transform myTransform;
 
     public GameObject detonate;
     public AudioClip Explosion;
 
     AudioSource boulderBoom;
 
 
     //------------------------------------//    
     
     void Awake()
     {
         myTransform = transform;
 
             
             EnemyAi otherScript = GetComponent<EnemyAi>();
             otherScript.enabled = false;
             
 
     }
     void OnTriggerEnter2D(Collider2D other){
         if (other.tag == "Player") {
 
             EnemyAi otherScript = GetComponent<EnemyAi> ();
             otherScript.enabled = true;
 
         }
     }
     
     void Start ()
     {
         boulderBoom = GetComponent<AudioSource> ();
 
     }
 
     
         
     
     
     
     void Update ()
     {
         if (Vector3.Distance (target.position, myTransform.position) > minDistance) {
             
             Vector3 dir = target.position - myTransform.position;
             
             // Normalize it so that it's a unit direction vector
             dir.Normalize();
 
             
 
         
         }
 
 
         else if (Vector3.Distance(target.position, myTransform.position) > maxDistance)
         {
         
             Vector3 dir = target.position - myTransform.position;
             
         
             dir.Normalize();
             
             
             myTransform.position += dir * moveSpeed * Time.deltaTime;
         }
 
 
 
 
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mixery322 · Aug 30, 2017 at 11:21 AM

Set the player gameobject the tag "Player" then add to your script nameofscript playerscript;

 void Start () {
 
 // To acces the player script
 playerscript = GameObject.FindGameObjectWithTag("Player").GetComponent<nameofscript>();
 }

Then set the movespeed equal to the player move speed:

 void Update () {
 movespeed = playerscript.movespeed;
 }

By the way don't copy and paste this because I wrote it "Free" not in visual studio so I may have forgotten a letter or somethin.

Hope it works.

Edit: You can also use Awake instead of Start and FixedUpdate instead of Update.

Comment
Add comment · Show 1 · 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
avatar image Bairne · Aug 30, 2017 at 12:19 PM 0
Share

That's really handy to know actually! $$anonymous$$akes it a lot easier than individually setting the speed for every follower.

Unfortunately it still doesn't solve the issue of what I assume is the increased velocity based on physics interacting with the character, as technically when falling through space the players movespeed will still be set to 10, so the followers will only move at that speed (since they have kinematic rigidbodies to allow for the ghostly floating effect I want).

What ideally need I guess is a way to interpret the players velocity and get their speed to match that?

Thanks again for that scripting tip though, still really useful!

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

184 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

Related Questions

I want to make a PONG AI that can be beatable. but if i make a AI then either it will be unbeatable or ERROR. please help!!! 1 Answer

Enemy AI using pathfinding to avoid obstacles 1 Answer

Javascript error 1 Answer

Move enemy to a specific movepoint with an array (C#) 1 Answer

Movement and Speed issues 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