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;
}
}
}
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.
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
![](https://koobas.hobune.stream/wayback/20220612133837im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trouble making jumping spider enemies 0 Answers
AI disappears when moving. 1 Answer
How change the destination point if it is inside an obstacle? 0 Answers
Cant change variable speed in script 1 Answer