- Home /
Question by
endermanrapman · Sep 28, 2020 at 02:16 PM ·
fpsturret
How to smoothly look at a object with offset?
Here is my script: using UnityEngine;
public class Turret : MonoBehaviour
{
public Transform gunTip;
public Transform player;
[Space]
[Header("Sentry Applications")]
public Transform sentryHead;
public Vector3 headRotationOffset;
[Space]
public Collider detCollider;
public float m_Speed;
bool targetLocked = false;
private void Update()
{
if (!targetLocked)
return;
Vector3 targetDir = player.position - transform.position;
sentryHead.rotation = Quaternion.RotateTowards(sentryHead.rotation, Quaternion.LookRotation(targetDir), Time.deltaTime * m_Speed);
}
/// <summary>
/// Detecting if the player goes in or out.
/// </summary>
private void OnTriggerStay(Collider other)
{
if (other.CompareTag("Player"))
{
targetLocked = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
targetLocked = false;
}
}
}
I'm trying to make it so that the turret head looks smoothly twords the player, but when I do it always rotates with the top. I want the barral to rotate twords me, but I don't know how to put offset into it.
Comment
Your answer
Follow this Question
Related Questions
getting out of a vehicle 2 Answers
Where/ How can I find out what is affecting my webplayer window-mode performance? 0 Answers
FPS Character with model 6 Answers
Raycast Positioning 2 Answers
Noob: Question about standard c# mouselook script. And other probably noob questions others may ask. 3 Answers