- Home /
Turret Rotation Not correct
Alright, so I followed this tutorial to learn how to make a turret http://cgcookie.com/unity/lessons/1-autoturret/ And I ended with this code: using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour {
public GameObject zero;
public GameObject one;
public GameObject firePos;
public GameObject myTarget;
public Transform EnemyTur;
Quaternion desiredRotation;
public float bulletImpulse = 20f;
public float range = 20f;
public float turnSpeed = 3f;
public Vector3 target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Debug.Log (myTarget);
if (myTarget != null) {
target = myTarget.transform.position;
CalculateAimPosition(target);
Debug.Log ("IDK");
EnemyTur.rotation = Quaternion.Lerp (EnemyTur.rotation, desiredRotation, Time.deltaTime*turnSpeed);
}
}
void Fire(){
float ranNum;
ranNum = Random.Range (-1.0f, 1.0f);
if (ranNum >=0 && ranNum <=1)
{
GameObject bullet2 = (GameObject)Instantiate(zero, firePos.transform.position + firePos.transform.forward, firePos.transform.rotation);
bullet2.rigidbody.AddForce( firePos.transform.forward * bulletImpulse, ForceMode.Impulse);
}
else
{
GameObject bullet1 = (GameObject)Instantiate(one, firePos.transform.position + firePos.transform.forward, firePos.transform.rotation);
bullet1.rigidbody.AddForce( firePos.transform.forward * bulletImpulse, ForceMode.Impulse);
}
}
void OnTriggerEnter(Collider tar){
if (tar.gameObject.tag == ("Player")) {
myTarget = tar.gameObject;
}
}
void OnTriggerExit (Collider tar){
if (tar.gameObject.transform == myTarget) {
myTarget = null;
Debug.Log ("Null");
}
}
void CalculateAimPosition (Vector3 tarf){
Vector3 aimPoint = new Vector3 (target.x, target.y, target.z);
desiredRotation = Quaternion.LookRotation (aimPoint);
Debug.Log ("Position");
}
}
But instead of rotating and facing the player, the turret floats up, rotates and looks at a random point. It follows the player, as in it rotates as I move around, but no in the way it should. It moves when I get close or farther away from it, now when I go around it. Any idea where I messed up? Thanks for your time.
Something that I see is in the update when you call CalculatrAimPosition you pass in your target which is a vector 3. Which was set to your targets transform. (all O$$anonymous$$ there)
But in your function you rename the passed in variable to tarf but under setting aim Point your referring to the earlier declared target.x, target.y, target.z where calling tarf.x ect should be just fine because that was the value you gave the passed in vector.
Try changing tarf to target and see if that helps.
$$anonymous$$y opinion is that the best way to achieve what you want is to create an empty game object, place it on your player and set this as the "Target" for your script.
Then adjust its position to get the result you want.
Any other (simple)way i think could have side effects.
Alright after a few tries I decided to rewrite the script, I got it working better with a different approach, but I had the problem of the AI ai$$anonymous$$g at the player's food, but @Gatu that(which I never thought of) Fixed the problem, so thank you.
kindly, provide with the code or guide me as I'm facing the same problem...
Regards
I think you asked your turret to Rotate on each axis... In fact, turrets are only rotating on the "Y"! (Depends of each cases... But in general)
(Try changing the wanted axis only!) Bests, $$anonymous$$ath
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Drag And Drop with Perspective Camera 1 Answer
C# problem something in hand 1 Answer
In Game Animation 2 Answers