News Forums RAIN Sample Projects, How To, and Code Raycast shooting custom action

This topic contains 3 replies, has 2 voices, and was last updated by  Sigil 5 months, 1 week ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #38791

    RinAldrin
    Participant

    I am making a custom action for one of my AI’s and I can’t seem to get him to shoot getting a NullReferenceException error on the line where the Raycast is. The problem is though that action should not be happening until he sees the enemy otherwise he should be wandering around like the unarmed AI I have. When I check the screen the condition for the wandering (set to null aka he hasn’t seen anyone) is red but the part that should be running if it doesn’t equal null is not playing. Bellow is the code from my script I will note it is in UnityScript and also in the tree I have a detect saving the enemy target to a form entry. Do I need to set a differently named var in the mount or am I coding it wrong?

    import RAIN.Action;
    import RAIN.Core;
    import RAIN.Representation;
    @RAINAction
    class ShootEnemy extends RAIN.Action.RAINAction
    {
        public var Rangeofgun : Expression;
        function Start(ai:RAIN.Core.AI):void
    	{
            super.Start(ai);
    	}
        function Execute(ai:RAIN.Core.AI):ActionResult
    	{
            var ME = ai.Body.transform;
            var layerMask = 1 << 10;
            var Rangeofgun : float;
            layerMask = ~layerMask;
            var hit : RaycastHit;
            var Enemy = ai.WorkingMemory.GetItem("Enemies");
            if (Physics.Raycast(ME.Vector3, Vector3.forward, hit, Rangeofgun, layerMask)){
            	Debug.Log("It Worked");
            }
            return ActionResult.SUCCESS;
    	}
    	function Stop(ai:RAIN.Core.AI):void
    	{
            super.Stop(ai);
    	}
    }
    #38798

    RinAldrin
    Participant

    Update, I have gotten some code working in default unity so the code works normally it is just porting it over to RAIN that I am having trouble with.

    var ME : AIRig;
        	var Me = ai.Body.TransformPosition(Vector3.forward);
            var layerMask = 1 << 8;
            var Rangeofgun = 118;
        	var fwd = ai.Body.transform.TransformDirection (Vector3.forward);
        	var ray = new Ray(ai.Body.transform.position, ai.Body.transform.forward);
    		var hit : RaycastHit;
    		if (Physics.Raycast(ray, hit, layerMask)){
    			if (hit.distance <= Rangeofgun){
    				Debug.Log("It Worked");
    		}
    #38800

    RinAldrin
    Participant

    Another update bellow is the error message I am getting while the game is running anyone know what it means?

    MissingMethodException: UnityEngine.GameObject.TransformPosition
    Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.ProduceExtensionDispatcher ()
    Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.Create ()
    Boo.Lang.Runtime.RuntimeServices.DoCreateMethodDispatcher (System.Object target, System.Type targetType, System.String name, System.Object[] args)
    Boo.Lang.Runtime.RuntimeServices.CreateMethodDispatcher (System.Object target, System.String name, System.Object[] args)
    Boo.Lang.Runtime.RuntimeServices+<Invoke>c__AnonStorey15.<>m__9 ()
    Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.DispatcherKey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
    Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
    Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
    Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args)
    UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType)
    ShootEnemy.Execute (RAIN.Core.AI ai) (at Assets/AI/Actions/ShootEnemy.js:19)
    RAIN.BehaviorTrees.BTActionNode.Execute (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTParallelNode.Execute (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTSequencerNode.Execute (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTConstraintNode.Execute (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTSelectorNode.Execute (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTParallelNode.Execute (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTConstraintNode.Execute (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTSequencerNode.Execute (RAIN.Core.AI ai)
    RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
    RAIN.Minds.BasicMind.Think ()
    RAIN.Core.AI.Think ()
    RAIN.Core.AIRig.AIUpdate ()
    RAIN.Core.AIRig.Update ()

    #38809

    Sigil
    Keymaster

    Well for that error, you are calling TransformPosition on the GameObject instead of its transform on this line:

    ...
    // var Me = ai.Body.TransformPosition(Vector3.forward);
    var Me = ai.Body.transform.TransformPosition(Vector3.forward);
    ...

    As for the rest of what you are trying to do, how is that working? If you are still having trouble repost the current code you are working with and we can figure it out.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.