Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Facing the target

Discussion in 'Editor & General Support' started by JeevanjotSingh, Jan 4, 2015.

  1. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    Greetings! I have a problem with my traps . While my knifes that have to face towards to my player like with transform.lookat(myvariable); and my knife just stand there and rotate by facing to the player position so after sometime it will instantiate and i can use addforce later to damage player when collide with knife but when i use lookat function then when game starts the knife stands with 90 degrees of anger and does no even face on it(I think there must something other to face the knife to player) and knife just stand there with a 90 degrees angel upwards . Please help me to solve this .

    Images Attached Before runtime and After Runtime :-
     

    Attached Files:

    • 1.png
      1.png
      File size:
      131.7 KB
      Views:
      795
    • 2.png
      2.png
      File size:
      139.4 KB
      Views:
      759
    Last edited: Jan 4, 2015
  2. Buffy

    Buffy

    Joined:
    Jan 3, 2015
    Posts:
    3
    I believe this was resolved in the IRC.

    Namely, putting the LookAt script on the Knife rather than Knife1 (Which should be used just as a mesh) and rotating it accordingly.
     
  3. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    If you can figure this out please let me know! I have been trying to figure this out for an automated turret... I need to lock things to one axis, not rotate 360 degrees on every axis to look at player lol.
     
  4. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,789
    Get the Vector3.Angle difference and rotate it around the Y axis by that difference. You can use just the X and Z positions and zero out the Y for both the turret and looked at target.
     
  5. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    Well, I thought about it like that, but I'm making an Online game, and I need it to recognize a tagged player.
    I just can't figure out how.

    It' a Turret, I need it to rotate around Y Axis and X axis.... (Y to rotate the turrent) and X to rotate the barrel up and down just a bit.

    But I just don't know how to do it with a tagged object..

    Such as, if a Tag of Red enters the trigger, try to aim at it and kill it.
    I just want it to search for a tag that enters it, not a specific object.
     
  6. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,789
    For getting the tag on entering the trigger

    Code (CSharp):
    1. function OnTriggerEnter (other : Collider) {
    2.    
    3.     if (other.transform.CompareTag("Red")) {
    4.         AimTurret();
    5.     }
    6. }
    Then to rotate the turret use the Y axis and Vector3.Angle. Then to rotate the gun up and down on the X axis do similar. You may want to use a Lerp function and a currentAngle and wantedAngle as the two parameters fed to the function and feed the result to the turret and gun.
     
  7. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884

    That's the part that confuses me.

    First off, I've been curious - How do you do the Code stuff on fourms? I only ever see it on the Answers page to make the code in the box like you did. I've been wondering this lol.


    But anyways, I understand how the tags part work, but as you put, it goes to another function called AimTurret();

    How can I make something outside the trigger part know what to look at without being in the Trigger function?

    Like for example void OnTriggerStay(Collider col);
    if(col.gameObject.tag == "Red){
    AimTurret();
    }

    void AimTurret(){

    How do I make it know what to aim at in here, without being in the TriggerStay?

    }
     
  8. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,789
    For the code stuff there is an insert button on the tools menu at the top. It looks like a text page just after the film clips and before the floppy disc.

    On the AimTurret() use the transform from my version and pass it to the AimTurret(Transform target). Then use the target.position per frame to track it within the AimTurret function. Also.. You only need to get this on OnTriggerEnter and then dismiss it at OnTriggerExit if I get what you want to do correctly.
     
  9. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,789
    As well.. you may want to create a boolean named something like isCurrentlyTargetting. Then when that is true in Update you can call the AimTurret function. Probably make the currentTarget a class variable as well and then you don't have to pass it to the AimTurret function as it will only track it when the boolean is true and the currntTarget is set in the OnTriggerEnter function.
     
  10. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,605
  11. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    Adding script to knife (parent)doesn't work , when i add script there then ir will follow the player by making some distance from first position .
     
  12. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    Yeh sure that's rotation transform to work of knife in my game , https://www.google.co.in/url?sa=t&r...=Y2pNPkw89s4E1Z_mfFs0Ag&bvm=bv.82001339,d.c2E


    Here is tutorial about this , hope it helps you .
     
  13. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    I scrolled through the timeline of the video and didn't see a script editor open, so how's that going to help LOL LOL LOL.
     
  14. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251



    LOL

    I added wrong video link ,

    LOL


    Here is that one.


    Script reference attached on description .
     
  15. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    Guys please suggest me something it's stopped here .
     
  16. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,789
    Keep trying. I have had to try 30-50 iterations and refactoring of code basics to get the special situation I wanted working at times. Get use to it. There is a meme in the Game Dev Memes thread where the guy looks at the computer with a puzzled look and the caption is something like "Hmm..code worked first time" like that is suspicious. It is. You will get used to throwing it at the wall till it sticks. That is why it is called development and not making.
     
  17. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    Great said, thanks