- Home /
Simple Sentry Guard Creation Help
I need to make a couple of sentry type guards for my game, and they would basically just be standing still, and need to fire at the player when he enters a certain zone.
I need help shooting at the player. I would just have to change the if statement on this script to something that tracks the player... Thanks in advance.
public Transform firePoint; public GameObject projectilePrefab; GameObject projectileInstance;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.V)) {
Instantiate (projectilePrefab, firePoint.position, firePoint.rotation);
}
}
Are you asking how to check when the player is within a certain distance or has hit a trigger, or how to look at the player?
@$$anonymous$$agso I know how to use triggers and area, but looking at the player is where I am currently struggling.
Answer by Magso · Jul 31, 2019 at 01:29 PM
You can use LookAt but that will lock the objects rotation on to the player. If you want a smooth rotation you can use LookRotation and Slerp like this.
float smoothness;
void LateUpdate () {
var playerDirection = Quaternion.LookRotation(player.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, playerDirection, Time.deltaTime / smoothness);
}
@$$anonymous$$agso It works perfectly except it shoves my prefab under the map? I tried freezing the rotation and position but that didn't work. Any ideas on a fix? Also, in my script, once the player enters a certain area, the guard starts to fire, how can I space out the shots ins$$anonymous$$d of them firing every single frame? Thanks again!
It shouldn't shove it under the map, it's not altering the position in any way. The only way I could think is that the guard is a child with a localPosition.z under -5 of the object this script is on which would cause it to pendulate. Also for your second question, look up 'coroutines'.
Answer by KingSloth · Jul 31, 2019 at 11:56 PM
@Magso It works perfectly except it shoves my prefab under the map? I tried freezing the rotation and position but that didn't work. Any ideas on a fix?
Also, in my script, once the player enters a certain area, the guard starts to fire, how can I space out the shots instead of them firing every single frame?
Thanks again!
Your answer
Follow this Question
Related Questions
Get last seen position when target behind wall 1 Answer
Double reference to waypoints in FSM 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Creating a More Efficient Script 4 Answers