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 surgesus · Mar 29, 2013 at 07:25 PM · turretaim

Turret aimming is off.

I am having an issue with aiming of my turret. The turret seems to be aiming and shooting right above the cube (enemy). I believe the problem is somewhere in this code.

 function CalculateAimPosition(targetPos : Vector3){
     
     var aimPoint = Vector3(targetPos.x-transform.position.x+aimError, targetPos.y-transform.position.y+aimError, targetPos.z-transform.position.z+aimError);
     
     desiredRotation = Quaternion.LookRotation(aimPoint);
     
     }

I tried using Quaternion.LookAt(pos) but the turret would rotate incorrectly. I give up trying to figure this out, if anyone has ideas please let me know. I just want it to shoot and hit that darn cube… . . . .

Here is my turret code if you see something else that can be changed

   var myProjectile : GameObject;
     var reloadTime : float = 1f;
     var turnSpeed : float = 5f;
     var firePauseTime : float = .25f;
     var muzzleEffect : GameObject;
     var errorAmount : float = .001;
     var myTarget : Transform;
     var muzzlePositions : Transform[];
     var turretBall : Transform;
     
     private var nextFireTime : float;
     private var nextMoveTime : float;
     private var desiredRotation : Quaternion;
     private var aimError : float;
 
      
      
     function Start ()
     {
   
     }
     function Update ()
     {
     if(myTarget)
     {
     if(Time.time >= nextMoveTime)
     {
     CalculateAimPosition(myTarget.position-transform.position);
     turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime*turnSpeed);
      
     }
      
     if(Time.time >= nextFireTime)
     {
     FireProjectile();
      
     }
     }
     }
      
     function OnTriggerEnter(other : Collider)
     {
     if(other.gameObject.tag == "Enemy")
     {
     nextFireTime = Time.time+(reloadTime*.5);
     myTarget = other.gameObject.transform;
     }
     }
      
     function OnTriggerExit(other : Collider)
     {
     if(other.gameObject.transform == myTarget)
     {
     myTarget = null;
     }
     }
      
       function CalculateAimPosition(targetPos : Vector3)
     {
 
     
     var aimPoint = Vector3(targetPos.x-transform.position.x+aimError, targetPos.y-transform.position.y+aimError, targetPos.z-transform.position.z+aimError);
     desiredRotation = Quaternion.LookAt(aimPoint);
 }
 
 
      
     function CalculateAimError()
     {
     aimError= Random.Range(-errorAmount, errorAmount);
     }
     function FireProjectile(){
      
     audio.Play();
     nextFireTime = Time.time+reloadTime;
     nextMoveTime = Time.time+firePauseTime;
     CalculateAimError();
      
     for(theMuzzlePos in muzzlePositions)
     {
     
     //Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
      //Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
     var go = Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
     go.rigidbody.AddRelativeForce(Vector3.forward * 20000);
     }
     }
Comment
Add comment · Show 1
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 hexagonius · Mar 29, 2013 at 07:54 PM 0
Share

$$anonymous$$y guess is it's line 28 try using the transform of the target only.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Mar 29, 2013 at 08:10 PM

I agree with @hexagonius. You are subtracting out transform.position twice. Once on line 28, and a second time on line 62. You can change either to one. Typically a turret rotates but does not elevate. Your code does both. Since you are assigning the rotation directly, rather than than Lerping() to the angle, you could simplify your code by using Transform.LookAt(). Then you would not need to subtract out the 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

11 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

Related Questions

Syncing 2-part turret design with mouse on rotating patform 1 Answer

target tracking with lead time 1 Answer

multipe targets 1 Answer

Freeze Camera Rotation that is attached to the object in certain moments 0 Answers

Axis-clamping a transform rotated with a Quaternion 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