News Forums RAIN General Discussion and Troubleshooting How to do a raycast in RAIN custom actions?

This topic contains 1 reply, has 1 voice, and was last updated by  RinAldrin 1 year, 3 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #38803

    RinAldrin
    Participant

    Hello I have been googling about this issue like crazy and yet I have gotten no where and haven’t been able to get the code working. I am coding in UnityScript using the template made from RAIN when you make a new custom action and I keep getting back a null reference exception error during it. Note I have moved this over to here cause I think I was posting in the wrong area.

    The code that works in a regular script (triggered by a button on the UI):

    function shoot (){
    	var layerMask = 1 << 8;
        var Rangeofgun : float = 118;
        var impactpoint: float;
        var fwd = transform.TransformDirection (Vector3.forward);
        var ray = new Ray(transform.position, transform.forward);
    	var hit : RaycastHit;
    	if (Physics.Raycast(ray, hit, layerMask)){
    		if (hit.distance <= Rangeofgun){
    			Debug.Log("It Worked");
    		}
    	}
    }

    This is the code in RAIN with my attempts to alter it:

    import RAIN.Action;
    import RAIN.Core;
    import RAIN.Representation;
    @RAINAction
    class ShootEnemy extends RAIN.Action.RAINAction
    {
        public var Rangeofgun2 : Expression;
        var Rangeofgun : float = 118;
        function Start(ai:RAIN.Core.AI):void
    	{
            super.Start(ai);
    	}
        function Execute(ai:RAIN.Core.AI):ActionResult
    	{
    		var ME : AIRig;
        	var Me = ai.Body.TransformPosition(Vector3.forward);
            var layerMask = 1 << 8;
        	var fwd = ai.Body.transform.TransformDirection (Vector3.forward);
        	var ray = new Ray(ai.Body.transform.position, ai.WorkingMemory.GetItem("Zombles"));
    		var hit : RaycastHit;
    		if (Physics.Raycast(ray, hit, layerMask)){
    			if (hit.distance <= Rangeofgun){
    				Debug.Log("It Worked");
    		} 
    	}
            return ActionResult.SUCCESS;
    	}
    	function Stop(ai:RAIN.Core.AI):void
    	{
            super.Stop(ai);
    	}
    }

    The only options I am really seeing if this doesn’t work is telling the custom action to broadcast to a different script and trigger it that way but that hasn’t really worked ether. Is this possible or am I barking up the wrong tree?

    • This topic was modified 1 year, 3 months ago by  RinAldrin. Reason: Clarification
    #38806

    RinAldrin
    Participant

    I managed to find a fix for this it seems UnityScript is rather bad when it comes to RAIN so what I did instead was I set an expression in the Behavior Tree to change a bool in the memory to true. A script on the AI component then sees that and it does the raycast then setting the bool back to false. It worked very well and now everything is working great with it.

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

You must be logged in to reply to this topic.