Forum rules - please read before posting.

throw objects

How do I make the enemy throw objects towards or near the player?

Comments

  • edited February 2023

    Attack mechanics aren't a part of AC, which is for traditional non-combatative mechanics. For a rock-throwing system, you'd need to rely on a custom script that has a function that causes e.g. a Rigidbody to have a force applied. For example:

    using UnityEngine;
    
    public class RockThrowing : MonoBehaviour
    {
    
        public Rigidbody rockRigidbody;
        public Vector3 force;
    
        private void Awake ()
        {
            rockRigidbody.isKinematic = true;
            rockRigidbody.detectCollisions = false;
        }
    
        public void ThrowRock ()
        {
            Debug.Log ("Applying force to " + rockRigidbody, rockRigidbody);
            rockRigidbody.isKinematic = false;
            rockRigidbody.detectCollisions = true;
            rockRigidbody.AddForce (force);
        }
    
    }
    

    To call this script's "ThrowRock" function from within AC, you can use either the Object: Send message or Object: Call event Actions.

  • I don't know where to put the action list ''Object: Send message or Object: Call event actions'' and in the sequence the Script , do I add both in the enemy's object? I did it that way and it didn't work, I don't know how to make the enemy throw stones towards the player within a time range. For rockThrowing I used the cube object and added a rigibody to the cube.

    https://uploaddeimagens.com.br/imagens/VhtBzAs

  • edited February 2023

    You have to set the Send message function to "Message to send: custom"
    there you call the script in this example "ThrowRock" (that is the name of the function in the script "public void ThrowRock ()").
    And make sure the object has a Rigidbody, then it should work.

    I don't know where in your action list you want to call this function but it should look something like this

    Set the Values higher when testing, so you see that something happens, and adjust after.

  • You'll need to share details of all the objects involved - including the Rock with the Rigidbody.

    The Action is only necessary if you want to call it from within AC. You can click Run now in the ActionList's Inspector to test it, but when you run it automatically will depend on how/when you want to trigger the throwing effect.

  • edited February 2023

    The object I used was a ''cube4'' prefab with a rigid body.

    I put the call on ''on start'' and the ''RockThrowing'' script on the enemy. Nothing happens.

  • edited February 2023

    Of course nothing will happen because the Object is a Prefab in your Folder and present not in your Scene.

    Put the Object in your scene and update the reference in your actionlist, and try it again.

    Logically you have to consider, that you are adding force to a Prefab in your Project Folder, the Game is not happening in there. No matter what you do, it is not in the Scene and therefore nothing can happen to it.

  • I changed the prefab object for what is in the scene and updated the reference in the script and in the action list and nothing happens. No error message appears, the only one is the one that already existed well before referring to the Enemy.

    ''LuloMolusco_1-removebg-preview cannot find Sorting Map to follow!''

  • You don't need to add the RockThrowing script to your NPC - only the rock object that is thrown.

    If you've got the script on the rock in the scene, and have the Action reference that rock, then the function should fire correctly.

    I have updated the script above to print a message when the function is called. Update your script with the above, and then look out for the message. When displayed, you can click the message to "ping" the Rigidbody that it's affecting. Which object is it then selecting in the Hierarchy?

  • edited February 2023

    I made the necessary changes and it still doesn't work.

    The warning appears referring to the rock. It's in print.

    https://uploaddeimagens.com.br/imagens/JxTUZMg

  • edited February 2023

    Looks like the Action is set up OK.

    Keep the rock un-parented for now - we can re-attach it to the NPC later, but let's simplify things and get the basic throwing effect working first.

    I've updated the script above to prevent the Rock from being a physics object until the throw command is called.

    Update the script, and move the Rock outside of the NPC's Hierarchy.

    Keep the Rock's Inspector open at runtime - you should find that its Rigidbody's Is Kinematic property becomes checked when the scene starts, and becomes unchecked when the throw command is called. If it doesn't seem to move at this point, try raising the force values and run the scene again.

  • Now it worked. Thanks. Had to change throw strength from 6 to x 350 and y 2

  • edited February 2023

    I removed the action that calls ''on start'' and used it in a trigger when the NPC goes over the trigger the action is triggered.

    He throws the stone, however this stone does not stay with him until he throws it, I tried to put it as a son and when the NPC throws the stone a few meters away, this stone is being dragged along with the NPC who is walking. Is there any method to solve this?

  • Before the Object: Send message Action, insert an Object: Set parent Action. Set the Object to affect to the stone, and set the Method to Clear Parent.

    This will detach the stone from the NPC's Hierarchy just before the throwing function is called.

  • It worked, thank you

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.