Search Unity

Can anyone help please

Discussion in 'Scripting' started by unity_7UFP4-iCkwsgjg, Jun 17, 2019.

  1. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    Hi everyone, i am trying to make an underwater game using only sea creatures. so far i have my AI purse and attacking each other. What i am trying to achieve though is to make my shark catch the fish and carry it in it's mouth but i am unable to do this. So far i have tried attaching a blank game object to the sharks mouth joint, then i have attached the fish as a child object but unfortunately the shark still will not grab the fish in the mouth, the fish is dragged at a distance away. This way also stops my fish swimming (they are on a wander script). This video shows what i am trying to achieve but does anyone know how i can achieve this?



    kind regards
     
  2. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,837
    After you parent the game object to the mouth game object set its local position to Vector3.zero
     
  3. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,837
  4. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    Code (CSharp):
    1. Animator _anim;
    2.     public string MyTrigger;
    3.     public int maxHealth;
    4.     public int currentHealth;
    5.     public Rigidbody m_rigidBody;
    6.     public GameObject bloodEffect;
    7.  
    8.     void Start()
    9.     {
    10.         currentHealth = maxHealth;
    11.         m_rigidBody = GetComponent<Rigidbody>();
    12.         _anim = GetComponent<Animator>();
    13.        
    14.     }
    15.  
    16.  
    17.     void Update()
    18.     {
    19.        
    20.     }
    21.  
    22.     public void DamagePlayer(int Hurt, Vector3 direction)
    23.     {
    24.         currentHealth -= Hurt;
    25.         if (currentHealth <= 50)
    26.         {
    27.             currentHealth = 0;
    28.             {
    29.                 _anim.SetTrigger(MyTrigger);
    30.  
    31.             }
    32.  
    33.             Instantiate(bloodEffect, transform.position, Quaternion.identity);
    34.             m_rigidBody.isKinematic = true;
    35.             Instantiate(deathParticles, transform.position, Quaternion.identity);
    36.             Destroy(gameObject);
    37.         }
    38.  
    39.     }
    health script
     
  5. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    Code (CSharp):
    1.    Animator _anim;
    2.     public string MyTrigger;
    3.     public int Hurt = 1;
    4.  
    5.     void Start()
    6.     {
    7.         _anim = GetComponent<Animator>();
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.  
    13.     }
    14.  
    15.     private void OnTriggerEnter(Collider other)
    16.  
    17.     {
    18.  
    19.         if (other.gameObject.tag == "Prey")
    20.  
    21.  
    22.         {
    23.             _anim.SetTrigger(MyTrigger);
    24.         }
    25.  
    26.         {
    27.  
    28.             Vector3 damageDirection = other.transform.position - transform.position;
    29.             damageDirection = damageDirection.normalized;
    30.  
    31.             FindObjectOfType<AverageHealth>().DamagePlayer(Hurt, damageDirection);
    32.  
    33.  
    34.         }
    35.     }
    36.  
    37. damage script
     
  6. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,837
    I don't understand where the shark takes a bite/the fish gets bitten
     
  7. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    i have it doing an attack animation when it collides (with a damage amount) with the fish and a health script which goes down once it collides with the shark. Is this wrong?
     
  8. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    the fish and shark both have colliders to
     
  9. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    253
    You said you are having a problem attaching a fish to the sharks mouth, but then post a health script that doesn't seem to have anything to do with that, and as such it is hard for us to help you.

    Post the code you wrote that attaches the fish to the shark.
     
    Vryken, angrypenguin and SparrowsNest like this.
  10. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    sorry about the confusion i don't want it attached like a sword to a hand. I want it so that it chases the fish (which it does) then holds it in it's mouth like that video
     
  11. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,837
    You're not making any sense, how is holding a sword with a hand not like holding a fish with a mouth?
    It's just a different graphic.
     
  12. Gor-Sky

    Gor-Sky

    Joined:
    Jan 28, 2018
    Posts:
    196
    Post the script where you changes the parent of the fish to the mouth.
     
  13. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    i haven't got a script only the game object with fish attached to jaw. How would i go about typing this script ?
     
  14. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,563
    For example, if I copy your code from above and add a line:
    Code (csharp):
    1.  
    2.       if (other.gameObject.tag == "Prey")
    3.         {
    4.           _anim.SetTrigger(MyTrigger);
    5.            //added this line
    6.            other.transform.SetParent(transform);
    7.        }
    8.  
    That will make the fish the child object of the object that has the trigger collider.
     
  15. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    that works brilliantly but how do i move it so that only the jaws cause damage and bring it abit close to the jaws of the shark?
     
  16. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    unfortunately I've just notice it seems to stop the animation of attacking working/showing
     
  17. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,563
    Instead of putting that OnTrigger script on the shark, put it on a small object that is inside the shark's mouth. Make that object a a child of the shark's head and change the collider so that it's only a small sphere that only covers the inside of the sharks mouth.
     
  18. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    ah right is that the whole shark script to place or just that trigger line to put on a new script?
     
  19. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,563
    I'm thinking the OnTriggerEnter method specfically, because then the "transform" member will refer to the transform of the mouth object.
     
  20. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    Code (CSharp):
    1. Some like this
    2.  
    3. Animator _anim;
    4.     public string MyTrigger;
    5.  
    6.     private void OnTriggerEnter(Collider other)
    7.  
    8.     {
    9.  
    10.         if (other.gameObject.tag == "Prey")
    11.  
    12.  
    13.         {
    14.             _anim.SetTrigger(MyTrigger);
    15.             other.transform.SetParent(transform);
    16.         }
    17.     }
    18.  
    19.     }
     
  21. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,563
    Sure, but in your original script there were three lines after that too.
     
  22. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    so just place the whole script on the game object and leave it off the shark itself?
     
  23. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,563
    Not necessarily, but the whole OnTriggerEnter method.
     
  24. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    okay after doing that, it is no longer doing the attack animation or colliding with the fish
     
  25. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,563
    Yeah, the _anim variable needs a reference to the animator. Try putting that code back where it was before, but instead of
    SetParent(transform);
    , you'll have to replace transform with a reference to the mouth object's transform.
     
  26. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    so i could use SetParent (Gameobject); ?
     
  27. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,563
    That doesn't make any sense.
    You need a reference to the transform of the object in the shark's mouth.
    Create a member like:
    Code (csharp):
    1.  
    2. public transform sharkMouth
    3.  
    Assign the transform to that member through the inspector than use that as the parameter for SetParameter.
     
  28. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    Code (CSharp):
    1. ah right i think i understand, This is my test script to see if it works attached to the game object with the fish child.
    2.  
    3. public Transform PA_SharkJawJnt;
    4.  
    5.     void Start()
    6.     {
    7.         _anim = GetComponent<Animator>();
    8.  
    9.     }
    10.  
    11.     private void OnTriggerEnter(Collider other)
    12.  
    13.     {
    14.  
    15.         if (other.gameObject.tag == "Prey")
    16.  
    17.  
    18.         {
    19.             PA_SharkJawJnt.transform.SetParent(transform);
    20.         }
    21.     }
     
  29. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,563
    Assuming that script is attached to the shark, that will make the Shark's jaw a child of the shark.
    you want to make the fish a child of the shark jaw

    Remember, if this script is on the shark than "transform" will be the sharks transform. and "other.transform" will be the transform of the object that collides with the collider (i.e. the fish).

    I think you want:
    Code (csharp):
    1.  
    2. other.transform.SetParent(PA_SharkJawJnt.transform);
    3.  
     
  30. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    okay thank you, that is working now only issue i have is it still isn't quite near the sharks mouth?
     
  31. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,563
    Assuming the PA_SharkJawJnt transform is right where you want the fish to be, then try adding the line:
    Code (csharp):
    1.  
    2. other.transform.position = PA_SharkJawJnt.position;
    3.  
    THat should set the fish's position to the same as PA_SharkJawJnt.
     
  32. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    just tried that but it seems to be an issue with my colliders because my fish collider covers the whole fish and the shark one covers the whole shark (with a separate collider for the mouth).
     
  33. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,563
    Are both colliders on the shark trigger colliders, though? OnTriggerEnter will only work for trigger colliders, but if both colliders are trigger colliders, then you won't be able to tell which one is which.
     
  34. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    ah right i have a box collider and a sphere collider on the shark with both (Is Triggers) ticked, the fish has also got a box collider with (Is Trigger ticked). Which ones would need to be un-ticked ?
     
  35. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,563
    So one of the shark's colliders is the mouth collider. Do you need the other collider to be a trigger? Remember that trigger colliders and non-trigger colliders behave very differently, though, so don't just change them arbitrarily.

    Instead of using OnTriggerEnter to determine if the fish is in the shark's mouth- if it's necessary, you can also use one of the collision check functions in the Physics class, such as OverlapSphere:
    https://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html
     
  36. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    tried but looking at my scene it just appear the shark dosen't collider with the body just the collider but the collider sticks out past the fish (if that makes sense, i have tried editing it) i assume the main colliders got to cover the entire fish?
     
  37. unity_7UFP4-iCkwsgjg

    unity_7UFP4-iCkwsgjg

    Joined:
    Dec 13, 2017
    Posts:
    152
    okay i have it working now but can't get part of the fishes body to go in the shark mouth