Hello everyone, let me explain my problem:
actually i would like the zombie sprite to move to the Player sprite but the zombie moves to a random place
The player moving script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingPlayerScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.Space))
{
transform.Translate(new Vector3(0, 1, 0) * 4f * Time.fixedDeltaTime);
}
}
}
The zombie script to move towards the player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingToPlayer : MonoBehaviour
{
public GameObject player;
public float PlayerPosX;
public float PlayerPosY;
public float PlayerPosZ;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
PlayerPosX = player.transform.position.x;
PlayerPosY = player.transform.position.y;
transform.Translate(new Vector3(PlayerPosX, PlayerPosY) * 1f * Time.fixedDeltaTime);
}
}
And the rotation player script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotationPlayer : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
}
}
Thank you for your help !