- Home /
Question by
Hansariche · Dec 08, 2020 at 01:26 PM ·
movement3denemyfollow
Vector3 MoveTowards character flying up when told to follow,Vector3 MoveTowards object flying up when I press play
I have my "enemy" following another "enemy", yet instead of following, it flies up in the air right off the bat and moves on all the axis.
public class BASICMOVEMENT : MonoBehaviour
{
Vector3 targetpos;
bool enemyseen;
object target;
public float movspeed = 1f;
public float step;
bool inAttackRange;
// Start is called before the first frame update
void Start()
{
enemyseen = false;
}
// Update is called once per frame
void FixedUpdate()
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
RaycastHit RCC;
if (Physics.Raycast(transform.position, fwd, out RCC, 10)) {
if (RCC.collider.name == "hitbox")
{
enemyseen = true;
target = RCC.collider.transform.parent.gameObject;
targetpos = RCC.collider.transform.position;
Debug.Log(RCC.distance);
if (RCC.distance < 0.5)
{
inAttackRange = true;
}
}
}
}
void Update()
{
if (enemyseen == true)
{
/*StartCoroutine(Attack());*/
if (inAttackRange == false)
{
step = movspeed * Time.deltaTime;
this.transform.parent.gameObject.transform.position = Vector3.MoveTowards(transform.position, new Vector3(targetpos.x, transform.position.y, targetpos.z), step);
}
}
}
}
Comment
I think you might mean for your target position of the $$anonymous$$oveTowards function to have a y value of the object you are moving ‘transform.parent.position.y’
Your answer
Follow this Question
Related Questions
Navmesh follow and stop at a distance 2 Answers
Canduct and movement of an enemy 0 Answers
Player not following touch after camera is rotated? 0 Answers
Make an enemy follow along a wall 2 Answers