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
1
Question by AdiorTheElite · Aug 02, 2013 at 04:47 PM · turretaimingrelative positionautomated

Automated turret targets around origin, regardless of location

Basically I've created a fairly simple automated turret, and I've just discovered that if I move said turret from its position at the origin, it continue to target as if it was still at the origin.

So, imagine you were looking down upon the turret and its surroundings. If I move the turret 5 units to the right, and place an enemy 5 units blow the origin (bare in mind this is relative to the viewpoint), the turret should aim south west, at the target, but instead it aims straight down, as if it's still positioned at the origin.

I've gone over my code and can't identify anything that would cause this, but then again I'm not particularly experienced. Apologies for any inefficiencies in the code, it's the result of a lot of trial, error and fiddling, but here it is:

 #pragma strict
 var myProjectile : GameObject;
 var reloadTime : float = 0.5f;
 var turnSpeed : float = 5f;
 var firePauseTime : float = 0.25f;
 var muzzleEffect : GameObject;
 var errorAmount : float = 0.1;
 var turretBall : Transform;
 var resetDelay : float =3;
 var resetTurnSpeed : float = 3;
 var laserGlow : Light;
 var laserBeam : Light;
 var leftMuzzlePos : Transform;
 var rightMuzzlePos : Transform;
 var stepAmount : float = 10;
 
 private var myTarget : Transform;
 private var nextFireTime : float;
 private var nextMoveTime : float;
 private var desiredRotation : Quaternion;
 private var aimError : float;
 private var originalPos : Quaternion;
 private var resetTime : float;
 private var laserGlowSegments : float;
 private var laserBeamSegments : float;
 
 function Start ()
 {
     originalPos = turretBall.rotation;
     laserGlow.enabled = false;
     laserBeam.enabled = false;
 }
 
 function Update ()
 {
     if(myTarget)
     {
         calculateAimPosition(myTarget.position);
         turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime * turnSpeed);
         if(Time.time >= nextFireTime)
         {
             FireProjectile();
         }
     }
     else
     {
         if(Time.time >= resetTime)
         {
             turretBall.rotation = Quaternion.Lerp(turretBall.rotation, originalPos, resetTurnSpeed * 0.01);
             laserGlow.enabled = false;
             laserBeam.enabled = false;
         }
     }
 }
 
 function OnTriggerEnter(other : Collider)
 {
     if(other.gameObject.CompareTag ("Enemy"))
     {
         nextFireTime = Time.time+reloadTime;
         myTarget = other.gameObject.transform;
         laserGlow.enabled = true;
         laserBeam.enabled = true;
     }
 }
 
 function OnTriggerExit(other : Collider)
 {
     if(other.gameObject.transform == myTarget)
     {
         resetTime = Time.time+resetDelay;
         myTarget = null;
     }
 }
 
 function calculateAimPosition(targetPos : Vector3)
 {
     var aimPoint = Vector3(targetPos.x+aimError, targetPos.y+aimError, targetPos.z+aimError);
     desiredRotation = Quaternion.LookRotation(aimPoint);
 }
 
 function CalculateAimError()
 {
     aimError = Random.Range(-errorAmount, errorAmount);
 }
 
 function FireProjectile()
 {
 //    audio.Play();
     nextFireTime = Time.time+reloadTime;
     nextMoveTime = Time.time+firePauseTime;
     CalculateAimError();
     Instantiate(myProjectile, leftMuzzlePos.position, leftMuzzlePos.rotation);
     Instantiate(myProjectile, rightMuzzlePos.position, rightMuzzlePos.rotation);
 }
 
 function FadeLightOut()
 {
     for(var step = stepAmount; step >= 0; step--)
     {
         laserGlow.intensity = laserGlow.intensity - (0.8/stepAmount);
         laserBeam.intensity = laserBeam.intensity - (0.8/stepAmount);
     }
 }

I think he bit of code that's causing it is the calculateAimPosition function, but I could be wrong. Thanks for any help.

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
Wiki

Answer by perchik · Aug 02, 2013 at 04:56 PM

First, for clarity, originalPos should be renamed originalRot, just so your code makes sense.

Perhaps try changing

 calculateAimPosition(myTarget.position);

to

 calculateAimPosition(transform.position-myTarget.position);

That should calculate the position to the relative target, not it's absolute position

Comment
Add comment · Show 1 · 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
avatar image AdiorTheElite · Aug 02, 2013 at 05:45 PM 0
Share

Good point about the variable name, I've changed that.

However, your other change causes the turret to point in the direction directly opposite the target.

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

16 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

Related Questions

Turret aiming script needs adjustment / fine tuning 1 Answer

first Gun fires not well 2 Answers

Autoaiming Turret Cannon? 3 Answers

Turret + Cannon Mouse movement 1 Answer

Turrets |Aiming don't work 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