Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Blahman919 · Jun 06, 2014 at 06:02 PM · c#

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oasisunknown · Jun 06, 2014 at 08:11 PM 0
Share

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.

avatar image Gatu · Jun 06, 2014 at 10:07 PM 0
Share

$$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.

avatar image Blahman919 · Jun 07, 2014 at 01:45 AM 0
Share

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.

avatar image SyedBilal51472 · Dec 04, 2014 at 03:01 PM 0
Share

kindly, provide with the code or guide me as I'm facing the same problem...

Regards

avatar image M-G-Production · Dec 05, 2014 at 11:34 PM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges