Search Unity

Rotating a turret/sentry gun

Discussion in 'Scripting' started by pauliina, Jan 28, 2016.

  1. pauliina

    pauliina

    Joined:
    Nov 15, 2015
    Posts:
    35
    Hi! I think this is a fairly simple thing to solve, but I'm fairly new to programming, so I need a bit of help on the way. I have a turret/sentry gun that I want to spin 90 degrees, then stop and spin back 90 degrees, and then stop and spin back and so on and so on.

    I'm thinking that maybe it would be something like, when it's 90 degrees, change direction, when it's 180, change direction again but the opposite way, or something? I tried just using Rotate first, but with that it spins all the way around, no matter what numbers I put in. Because online a lot of people say you can just write
    transform.Rotate(90,0,0); but that doesn't work as it never stops spinning.

    It should be pretty much like the sentry guns in the different Call of Duty games have when they're NOT shooting (ie, when they're just spinning). It was really hard to find a video where you can see it properly, because in most videos they're shooting obviously, but something like this (fast forward to like 17 seconds in):
    .

    Thanks in advance!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    It's much easier to use an animation for this than scripting.
     
  3. pauliina

    pauliina

    Joined:
    Nov 15, 2015
    Posts:
    35
    Oh, hmm. I can't animate things though. Isn't there some way of doing it with code? It doesn't have to look perfect, as long as it does the job.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Why not? If you mean you don't have animation software, yes you do - this level of animation is easily made iwhtin Unity.
     
  5. pauliina

    pauliina

    Joined:
    Nov 15, 2015
    Posts:
    35
    Oh, you can? I didn't think of that. I guess I'll search around for some tutorials for that, then! Thanks! But is that easier/better/quicker than scripting it? If so, why?
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    It's easier because it's WYSIWYG - scripting it involves coroutines, quaternions, and a bunch of other stuff, and unless you know what you're coding, none of it will make any sense until every part is done. Plus, with animation, you get a lot finer, easier, more intuitive control over the timing of it.
     
    pauliina likes this.
  7. pauliina

    pauliina

    Joined:
    Nov 15, 2015
    Posts:
    35
    Huh, what'dya know, it was supereasy! Thank you!! It looks and works great!
     
    StarManta and topherbwell like this.
  8. pauliina

    pauliina

    Joined:
    Nov 15, 2015
    Posts:
    35
    Don't know if I should make a new thread or not, but I'll try asking here first.

    What is the easiest/best way to get the turning turret/sentry gun to look for the player? The turret is (always) turning like the one in the video, so it never stops or similar if it finds a target. I just want it to have like, a trigger that's cone-shaped, and if the player is within that area, you die. My character has a Character controller. I tried doing triggers on the turret, but nothing happens when the player walks into it (I'm guessing because it's a Character controller?). I've tried adding box colliders and rigidbody to the character, but nothing helps. I've also tried childing the player to an Empty GameObject that has a rigidbody etc, but that didn't work either. And, I've tried raycasts, but that doesn't do what I want it to, because it's just a line, and I want a bigger area. It should be like a flashlight often is in games.

    (something similar to this shape)


    Thanks in advance!
     
  9. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Considering everything that has been said about the character controller in the forums and in answers, I'd advise the same which is to avoid using the character controller.

    The problem in your latest post in strange. You created an Empty game-object with a Rigidbody on it. It is not clear if you parented the player to the empty, or the empty to the player.

    Either way you should have attached the empty as a child object of the player. And the fact you refer to this object as an empty suggests to me 2 things have gone wrong even assuming the parenting was done correctly:

    1) you have not put a collider on the empty object.
    2) you have not turned off gravity on the Rigidbody in the empty object.
     
  10. pauliina

    pauliina

    Joined:
    Nov 15, 2015
    Posts:
    35
    But how do I go around this without using the character controller, since that's what my player is using? Do I have to remove it and use something else? If so, what? It's from a first person perspective, and character controller was what most people online recommended in that case.

    Also, I meant that the object was childed to the player. And it does have a collider, and the gravity is off. It's still not working. The console doesn't even detect it.
     
  11. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Fine I apologise. From my own experimenting recently, turns out the CharacterController CAN detect trigger events. Are you sure you are using the latest version of Unity? The version I have is 5.3.1p2, it may not be the latest but it seems late enough to have this feature.
     
  12. pauliina

    pauliina

    Joined:
    Nov 15, 2015
    Posts:
    35
    I have the latest, 5.3.2, but how would I go about detecting triggers on a CharacterController? OnTriggerEnter?
     
  13. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
  14. pauliina

    pauliina

    Joined:
    Nov 15, 2015
    Posts:
    35
    But this only works when the player is moving, no? So, if the player is standing still, but the turrets trigger moves into the player, nothing will happen, right? Because that wont work, then. Ugh. I don't know what to do.
     
  15. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Have you actually tried it out though. In any case, I just tried it out. For the record i suspected it might be because the turret trigger did not have a rigidbody. This was a possibility because in a collision between 2 bodies, at least one of them needs a Rigidbody for collision events to be called.

    The test is simple. Have a trigger box move into a Character Controller capsule.

    Test 1: The trigger box had a Rigidbody. The OnTriggerEnter function got called when the box moved into the capsule.

    Test 2: I removed the Rigidbody from the trigger box. The OnTriggerEnter function still got called when the box moved into the capsule.

    Test 3: Continuing from test 2, I replaced the Character Controller capsule with a normal capsule collider instead. This time the OnTriggerEnter function was not called when the box moved into the capsule.

    I also repeated all 3 tests with the Character Controller moving into the box instead, and got the same results.

    From this test, I can say it is quite safe to assume that, at least where collision is concerned, the Character Controller capsule behaves like a rigidbody.
     
    Last edited: Feb 10, 2016