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 so3arbelnox · Feb 13, 2012 at 04:44 AM · rotationturrettargeting

Locking on target with angle (mostly working)

Hi, Im a bit new to unity and 3D programming in general so I apologize for this basic question. Furthermore, it appears that is a very common question but after looking around I guess Im just not understanding some key concepts.

At the moment I have a turret object that changes rotation based on the players position. (locked to y) Its working fine. The turret also fires a fireball every second from a "spawn point" in front of it. What I want to happen is to have the spawnPoint, which it fires from, angle down and up based on the players position. I thought I could do this with LookRotation but it fires straight all the time. I assume this is because of the transform.forward when I fire.

Here is my code for my script.

 var LookAtTarget:Transform;
 var damp = 1.0;
 var bulletPrefab:Transform;
 var passedTime = 0.0;
 var shootRate:double = 1.0;
 var homingSpeed:double = 3.0;
 var minDistance:double = 3.0;
 
 function Update () 
 {
     //x and z lock
     if (LookAtTarget)
     {
         //Move toward player position
         
         //float fX = desX-playerX;
         var fx = gameObject.Find("Worm").transform.position.x - transform.position.x;
         var fy = gameObject.Find("Worm").transform.position.y - transform.position.y;
         var fz = gameObject.Find("Worm").transform.position.z - transform.position.z;
 
         var dist = Mathf.Sqrt(fx * fx + fy * fy + fz * fz);
         
         if (dist > minDistance)
         {
             var step = homingSpeed / dist;
         
             transform.position.x += (fx * step) * Time.deltaTime;
             transform.position.z += (fz * step) * Time.deltaTime;
         }
             
         var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
         
         //Move shooting mechanism angeled at player
         var rotateFireball = Quaternion.LookRotation(LookAtTarget.position - transform.position);
         
         transform.FindChild("spawnPoint").transform.rotation = rotateFireball;
         //transform.FindChild("spawnPoint").transform.rotation = Quaternion.Slerp(transform.FindChild("spawnPoint").transform.rotation, newRotateFireball, Time.deltaTime * damp);
         
         passedTime += Time.deltaTime;
         
         //1 second has passed
         if (passedTime >= shootRate)
             Shoot();
     }
     
     transform.rotation.x = 0;
     transform.rotation.z = 0;
 }
 
 function Shoot()
 {
     var bullet = Instantiate(bulletPrefab, transform.Find("spawnPoint").transform.position, Quaternion.identity);
     
     bullet.gameObject.tag = "enemyProjectile";
     bullet.rigidbody.AddForce(transform.FindChild("spawnPoint").transform.forward * 1000);
     passedTime = 0;
 }


Im not even fully sure why LookRotation(target.position - transform.position) is necessary either. But it seems to work for the turret rotation. I figure you would just need LookRotation(target.position)

Anywho, if anyone can help, I would greatly appreciate it. Thanks!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by so3arbelnox · Feb 13, 2012 at 07:07 AM

Fixed.

 var rotateFireball = Quaternion.LookRotation(LookAtTarget.position - transform.position);

Should be:

 var rotateFireball = Quaternion.LookRotation(LookAtTarget.position - transform.FindChild("spawnPoint").transform.position);
Comment
Add comment · Share
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Need Help with random rotatition on game turret 2 Answers

Character rotating on supposedly locked axis 2 Answers

Turret script. Making it aim in heigth only. 1 Answer

3D Turret Rotation 0 Answers

3D nested Turret Prefab Rotation 1 Answer


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