- Home /
Problem with enemy rotation
The problem is that the enemy is rotating but always looking to the right instead of facing the player. Thank u for ur help, here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShoot : MonoBehaviour
{
[SerializeField]
GameObject projectile;
private Transform target;
[SerializeField]
Transform shootPoint;
[SerializeField]
float turnSpeed = 5f;
float fireRate = 0.2f;
private void Start()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
}
private void Update()
{
fireRate -= Time.deltaTime;
Vector3 direction = transform.position - target.position;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), turnSpeed * Time.deltaTime);
if (fireRate <= 0)
{
fireRate = 0.2f;
Shoot();
}
}
void Shoot()
{
Instantiate(projectile, shootPoint.position, shootPoint.rotation);
}
}
Answer by endasil_unity · Apr 23, 2020 at 08:38 PM
Your setting it to look AWAY from the player, try inverting your direction statement. Vector3 direction = target.position- transform.position ; Yes I have to admit that I made this mistake a few times too. :)
Thank your for ur answer. But now is the enemy always looking to the left :D Is there any way to rotate him inside the function -90 or 90 degrees? His default rotation is 0/0/0
Ok i found out the problem, i just put the "shootPoint" to his left side. He was facing me all the time ... but the shootPoint was just placed wrong dumb fail :D
Your answer
![](https://koobas.hobune.stream/wayback/20220612232310im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
change direction by rotating object around y axis and adding relative torque 1 Answer
Rotation Question in Top-down 3D Game 0 Answers
How to use mathf.clamp? 2 Answers
How can i rotate 3D object with joystick in all directions? 0 Answers
Rotate around sphere using Quaternions with full radius 1 Answer