Hey, I am making an asteroids like game and I wanted to make a random direction for the player moves, that part was simples, but I wanted to make the rotation of the object be in the direction that was generated, but for some reason I can’t make it, I tried to using a “Quaternion.LookDirection”, but the player rotates in the Y axis, but I wanted to just rotate in the X and Z, I also tried to make a “new Quaternion(randomDirectionX, 0, randomDirectionY, 0)” but also didn’t work. The code is right below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public int enemyType;
public float speed;
private float randomDirectionX;
private float randomDirectionY;
private float xMovement = 1.0f;
private float yMovement = 1.0f;
private Vector2 randomDirection;
private GameManager gameManager;
// Start is called before the first frame update
void Start()
{
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
HandleRandomMovementAndRotation();
}
// Update is called once per frame
void Update()
{
CheckEnemyType();
}
void CheckEnemyType()
{
switch(enemyType)
{
case 1:
RaiderEnemy();
break;
case 2:
HunterEnemy();
break;
case 3:
BruteEnemy();
break;
}
}
void RaiderEnemy()
{
if(gameManager.isGameActive == true)
{
transform.Translate(randomDirection * speed * Time.deltaTime);;
}
}
void HunterEnemy()
{
if(gameManager.isGameActive == true)
{
Debug.Log("Hunter");
}
}
void BruteEnemy()
{
if(gameManager.isGameActive == true)
{
Debug.Log("Brute");
}
}
void HandleRandomMovementAndRotation()
{
randomDirectionX = Random.Range(-xMovement, xMovement);
randomDirectionY = Random.Range(-yMovement, yMovement);
randomDirection = new Vector2(randomDirectionX, randomDirectionY).normalized;
transform.rotation = Quaternion.LookRotation(randomDirection);
}
}