Question by
Felipe_Schluepmann · Feb 24, 2021 at 02:16 PM ·
scripting problemfpsscripting beginner
How to restrict rotation?
Hi I'm making a simple fps game for my history class and I made a system that if the player enters in the field of view of an enemy he will rotate to look at the player however I would like to limit hit rotation only in the y axis so he only rotates in one axis however what happens now is that when you are far away he is straight up and when your close he is lying on the ground
Here is my code until now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAi : MonoBehaviour
{
bool detected;
GameObject target;
public Transform enemy;
private var rotation;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (detected)
{
rotation = enemy.LookAt(target.transform);
Mathf.Clamp(rotation, 0f, 0f);
}
}
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
detected = true;
target = other.gameObject;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to make a random object generator that responds to simple touch? 0 Answers
Script error 0 Answers
Read Only float 2 Answers
Joystick Movement Not Smooth Enough 0 Answers