Feeling dumb about Turrets...

Been looking around at the 50 billion turret tuts on the net. Still having a problem…

public class SlowTurretController : MonoBehaviour {
	
	public GameObject targetFollow;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		transform.LookAt(targetFollow.transform);
		
	}
}

The turret is rotated at a weird angle when I playtest the scene, suggesting it’s trying to follow the player…sort of, but it won’t update after the initial load…

Before:

After:

It’s a bit confusing because you have 2 different camera angles in your screenshots. LookAt points the object’s Z axis at the target Vector3, so it would appear to be doing exactly what it’s supposed to - pointing the center of the cylinder at the capsule.

It definitely rotates, once anyway when the level first starts. Beyond that initial movement though it never moves again.

transformlookat should make it look at the player every frame should it not? Right now it only looks once and then stops, even though I have it in void update. Or am I supposed to give it a condition to check to see if the player has moved before trying again?

Nope. I would start by using something other than a capsule which looks the same regardless of if it rotates.

Is this a better example?
Anyway, nothing seems to be happening now. So maybe I was mistaken about it actually moving.

The turret is the box. As you can see I moved the player and the turret did not move.

However I added a print at the end of the code and that’s updating every frame so it’s definitely running through the code, it’s just not doing anything I can see.



Here is a code I use. I will post a project example for you in a second.

	// Update is called once per frame
	void Update () {
		
		Transform datarget = transform.Find("/Dude");
		
		transform.LookAt(datarget);
		
	
	}
}

Alright, I made this real quick. A cylinder, with a fire particle on it. Run (LShift) or walk WASD around the cylinder and the cylinder and fire follow you.

http://www.mediafire.com/?gdqb99m3jfhc0gv

Target (named dude) and code from previous post is attached to cylinder. The flame is a child object of the cylinder. Hope it helps somewhat. I think it just wasn’t locating your object.