Search Unity

Turret Aiming?

Discussion in 'Scripting' started by monsterjamp, Feb 15, 2013.

  1. monsterjamp

    monsterjamp

    Joined:
    Feb 10, 2013
    Posts:
    30
    I can't seem to get my turret to look at my character properly. It always rotates at wrong angles. Also instead of the tip of my turret facing my player the center of my turret looks at my player.

    Here is what my cannon looks like.
     
  2. lockbox

    lockbox

    Joined:
    Feb 10, 2012
    Posts:
    519
    Try this...

    1. Create an empty and place it at the correct rotation/pivot point inside of your turret
    2. Parent the turret to the empty
    3. Remove the script from the turret and attach it to the parent

    Does it work right now?
     
  3. monsterjamp

    monsterjamp

    Joined:
    Feb 10, 2013
    Posts:
    30
    Nope the center of the cannon is still the part that looks at me.

    Here is a web-player that includes simple scene with my turret and player:
    https://dl-web.dropbox.com/get/Online.html?w=AADeYnwJZVcYRtmAbUylD1I1fp-lFuQ8i5HylA9rta37NQ
     
    Last edited: Feb 15, 2013
  4. lockbox

    lockbox

    Joined:
    Feb 10, 2012
    Posts:
    519
    That link is no good. Can you post the public link?
     
  5. Andulvar

    Andulvar

    Joined:
    Feb 15, 2013
    Posts:
    6
    what about moving the origin of the turret to the point thus making not the centre face you, but the tip itself. hope that helps
     
  6. monsterjamp

    monsterjamp

    Joined:
    Feb 10, 2013
    Posts:
    30
    http://gamesafterdawn.tk/Unity/Online.html
    This is just a quick scene I made, my character model seems to have messed up because the quality is on fastest. Now it seems that the center isn't facing me but it's still not the tip.
    @Andulvar: I beleive I tried that before but I will try again.
     
  7. nulldiver

    nulldiver

    Joined:
    Oct 14, 2007
    Posts:
    22
    My guess, looking at the behavior, is that your cannon is modeled (or parented) so that what visually appears to be forward isn't aligned to the local z axis (it appears to be the local x axis) and that your script is making the assumption (by convention) that +z is forward. If that is the case, you can rotate the model in your modeling software (this would be best), rotate it inside of a parented GameObject that has the targeting script (this makes your hierarchy messy, but is an easy fix -- it is what lockbox suggested above) or change your script so that it references the local +x instead of forward (this seems like a bad idea because eventually Unity's convention of +z = forward will come back to bite you.).

    If that isn't the case -- if you are 100% certain that the local z axis points forward and this is indeed a scripting problem, I don't think anybody is going to be able to really help you without seeing the script you are using for targeting.
     
  8. monsterjamp

    monsterjamp

    Joined:
    Feb 10, 2013
    Posts:
    30
    This isn't really my own code I took it from the internet because I was having trouble making a script. I'm much better at C#.
    Code (csharp):
    1.     var goTarget : GameObject;
    2.     var maxDegreesPerSecond : float = 30.0;
    3.     private var qTo : Quaternion;
    4.      
    5.     function Start () {
    6.     qTo = goTarget.transform.localRotation;
    7.     }
    8.      
    9.     function Update () {
    10.     var v3T = goTarget.transform.position - transform.position;
    11.     v3T.y = 0.0;
    12.     qTo = Quaternion.LookRotation(v3T, Vector3.up);
    13.     transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, maxDegreesPerSecond * Time.deltaTime);
    14.     }
     
  9. nulldiver

    nulldiver

    Joined:
    Oct 14, 2007
    Posts:
    22
    Even if you don't write a script yourself, you should really take the time to make sure you fully understand what it is doing. I'm not implying that you don't/didn't -- but in case somebody just learning to write scripts comes along with a similar question in the future, here is the process that I think through when reading your Update() loop line-by-line:

    Code (csharp):
    1.  
    2. var v3T = goTarget.transform.position - transform.position;
    3.  
    By subtracting the current position of the cannon from the target's position, we end up with a vector that describes the difference between the two -- you can think of it as the vector from the cannon to the target, in a sense.

    Code (csharp):
    1.  
    2.  v3T.y = 0.0;
    3.  
    The y value can just be ignored since the cannon's targeting is, effectively, just two dimensional.

    Code (csharp):
    1.  
    2. qTo = Quaternion.LookRotation(v3T, Vector3.up);
    3.  
    This creates a rotation that looks along the vector that you made (keeping the local +y aligned to the global +y)

    Code (csharp):
    1.  
    2. transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, maxDegreesPerSecond * Time.deltaTime);
    3.  
    This spherically interpolates between the cannon's rotation and the rotation you just created, with rotation clamped to a maximum speed -- this value gets assigned back into the cannon's rotation, so next time it is using the modified value and so forth -- animating the cannon's rotation.

    It seems like this code is doing exactly what it should be doing, which makes me believe this isn't really a scripting problem -- it is more likely that it has to do with assumptions about how the GameObjects are set up:

    question 1: Does your cannon GameObject a child to any other GameObjects in the hierarchy?
    question 2: When you select your cannon in the Scene View, and you have the 3 transform arrows so that you can move it around, what is the color of the arrow that points along the cannon's barrel?
     
    Last edited: Feb 17, 2013
  10. monsterjamp

    monsterjamp

    Joined:
    Feb 10, 2013
    Posts:
    30
    Answer 1:
    The "gameObject" is an "empty".
    Answer 2: The red one (X).

    Also I never got what qTo meant.
     
    Last edited: Feb 17, 2013
  11. nulldiver

    nulldiver

    Joined:
    Oct 14, 2007
    Posts:
    22
    So, this is why your cannon points the side at the target rather than the barrel -- when we rotate to look at the target, we are assuming that our forward direction is on the Z axis. There are three ways that you can fix it:

    1. turn the artwork 90 degrees in your modeling software so that it is aligned correctly. This is really the best way.
    2. rotate the cannon 90 degrees relative to the empty game object, and have the targeting script on that instead of the cannon.
    3. change your script -- this isn't hard BUT it doesn't solve the underlying issue at all

    I would strongly encourage you to go with option #1 unless the cannon is artwork that you've downloaded and you don't have any modeling software -- in that case, I would say to go with #2. Remember though, that it is the parent that needs to have the script attached, not the cannon artwork.
     
  12. monsterjamp

    monsterjamp

    Joined:
    Feb 10, 2013
    Posts:
    30
    Thanks, I tried the first option it works now. :)
    http://gamesafterdawn.tk/Unity/Fix.html
    But, I plan on adding wheels that don't rotate with the cannon. I tried doing this with my cone but the other 2 parts of the cannon don't seem to stay on top of the cone while rotating. Can I fix this with a Empty Object? If so how?
     
  13. nulldiver

    nulldiver

    Joined:
    Oct 14, 2007
    Posts:
    22
    How are they behaving? In what way are they coming out of alignment? Is it a visual misalignment or are transform positions actually changing?

    If it is visual misalignment, I think you probably have some artwork that is offset from origin on the X/Z plane so when it rotates, it appears to move. If it is actually moving (the transform position), something more complex is happening.
     
  14. monsterjamp

    monsterjamp

    Joined:
    Feb 10, 2013
    Posts:
    30
    That's what I was thinking.
    You can "play" the mini scene here: http://gamesafterdawn.tk/Unity/Fix.html
    Well after I remake my turret I'll post back if I have any issues.