- Home /
transform position for x and z only
Hi, im new to unity. i was searching through some old forums to get the enemies to chase after the player. the problem is the positition of 'y' keeps changing too so the enemies floating above the air and keeps getting up. pls help (sorry for bad english) this is my code :
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class zombieNavigation : MonoBehaviour { [Header("Main Setting")] public GameObject player; public GameObject toBeDestroyed; public float speed = 1f; public Transform target;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
if (!player)
{
Debug.Log("ERROR could not find Player!");
}
}
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance(player.transform.position, transform.position);
transform.LookAt(target);
if (distance < 60)
{
Debug.Log("player is close");
var delta = player.transform.position - transform.position;
delta.Normalize();
var moveSpeed = speed * Time.deltaTime;
transform.position = transform.position + (delta * moveSpeed);
}
else
{
Debug.Log("not close yet " + distance);
}
}
}
Answer by MomkeyDev · Apr 12 at 10:09 AM
Try to use a Rigidbody and stick to AddForce. If you can't then you could either constraint the y or just remove the y from your target vector like this: targe = new Vector3(x, 0, z)
Your answer
Follow this Question
Related Questions
How to make the camera acquire the position of a gameobject and start following it? 1 Answer
Recalculate linerenderer when moving gameobjects 1 Answer
Instanitiate PreFabs and let them move to different locations 1 Answer
smoothly change position of an Object 1 Answer
Maintain object position when using raycasting to multiple height 2 Answers