Note: I’m flexible at scripting, so if you code in C# I’ll try to convert it into Javascript.
Ok, so my main problem is having the turret not target the player at a certain angle. There’s a ton of ways to code the same thing. I just need a little help in fixing this puzzle. So, right now I got the turret to target at a radius, and when the player goes in that. The turret simply looks at the player within’ that looking radius. I already know about coding the attack. But just right now, I’ll keep it simple.
Another example, think of it this way. The turret is on a wall. So it of course has a 180 degrees looking before it attacks itself or the building/wall it is on. Now, I have the turret set on its maximum rotation 150 degrees and the minimum rotation 30 degrees. Also the turret is a child object inside its parenting object I would also like to note. When I tried to track it in the inspector. The rotation of the child turret was below -0 and over 0, jumping everywhere. Do I need to try Quaternion or Euler angles?. The code is below.
If you want me to make it more simpler, let me know and I’ll also draw a diagram explaining more about my concept I’m trying to accomplish.
/*Name: Zack
Date: 12/21/11
Comments: This wall blaster will be attacted to walls and will act as an AA-Gun across the town skys.
using the X and Z plane, not using the Y plane*/
var turret : Transform; //The turret will be the one connected to the ball so it can rotate
var turretConstantPosition = null;
var target : Transform;
var constantHeight = 20.0; // Height in the world position
var detectRange = 70.0;
var rotationMaximumX : float = 150.0; //Here
var rotationMinimumX : float = 30.0; //Here
function Awake()
{
turretConstantPosition = turret.transform.position;
}
function Start()
{
if ((target == null) (GameObject.FindWithTag("Player")))
{
target = GameObject.FindWithTag("Player").transform;
}
else
{
//Search function later on
}
}
function Update()
{
turret.transform.position = turretConstantPosition;
this.transform.position.y = constantHeight;
var distance = Vector3.Distance(target.transform.position, this.transform.position);
//Here, beside this "if statement"
if ((distance < detectRange) (turret.transform.rotation.x < rotationMaximumX) (turret.transform.rotation.x < rotationMinimumX))
{
lookAtPlayer = true;
}
if (lookAtPlayer == true)
{
turret.transform.LookAt(target);
Debug.Log(distance);
}
}