News Forums RAIN General Discussion and Troubleshooting How can this AI shoot?

Tagged: , ,

This topic contains 1 reply, has 2 voices, and was last updated by  prime 1 year, 11 months ago.

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


    I am trying to get the AI to shoot the player. I have set up the sensors so that the player can be detected. Then, the AI moves towards the player. When the player is in range of the AI, the AI should raycast the player and print that it has hit the player. However, I get a NULL REFERENCE EXCEPTION at line 47, where the code raycasts. Could someone please help me with this? Here is the code:

    import RAIN.Core;
    import RAIN.Action;
    class aiShootPlayer extends RAIN.Action.RAINAction
    	private var woodenImpactParticle : GameObject;
    	private var concreteImpactParticle : GameObject;
    	private var dirtImpactParticle : GameObject;
    	private var metalImpactParticle : GameObject;
    	private var bodyImpactParticle : GameObject;
    	private var shootPos : GameObject;
    	private var dist : float = 50;
    	private var showBlood : int = 0;
    	function newclass()
    		actionName = "aiShootPlayer";
    	function Start(ai:AI):void
    	function Execute(ai:AI):ActionResult
    		showBlood = PlayerPrefs.GetInt("GraphicsShowBlood", 0);
            shootPos = ai.WorkingMemory.GetItem("ShootPos") as GameObject;
            woodenImpactParticle = ai.WorkingMemory.GetItem("WoodenImpactParticle") as GameObject;
            concreteImpactParticle = ai.WorkingMemory.GetItem("ConcreteImpactParticle") as GameObject;
            dirtImpactParticle = ai.WorkingMemory.GetItem("DirtImpactParticle") as GameObject;
            metalImpactParticle = ai.WorkingMemory.GetItem("MetalImpactParticle") as GameObject;
            bodyImpactParticle = ai.WorkingMemory.GetItem("BodyImpactParticle") as GameObject;
            return ActionResult.SUCCESS;
       	function Stop(ai:AI):void
    	function Shoot()
    		var hit : RaycastHit;
    		if (Physics.Raycast(this.transform.position, Vector3.forward, hit, 50))//shootPos.transform.position, Vector3.forward, hit, 50))
    			if (hit.gameObject.tag.ToLower() == "player")
    				print("Hit player");
    				Object.Instantiate(bodyImpactParticle, hit.point, bodyImpactParticle.transform.rotation);

    and here is the XML Behaviour Tree:

    <behaviortree version="1.1" repeatuntil="" name="soldierAITree"><parallel tiebreaker="fail" succeed="all" repeatuntil="" name="root" fail="any"><detect sensor=""eyes"" repeatuntil="running" name="detectEyes" entityobjectvariable="entityPlayerPos" aspectvariable="" aspectobjectvariable="" aspect=""entityPlayerVisual"" /><detect sensor=""shootRange"" repeatuntil="running" name="detectShootRange" entityobjectvariable="entityPlayerTarget" aspectvariable="" aspectobjectvariable="" aspect=""entityPlayerVisual"" /><selector usepriorities="False" repeatuntil="" name="ExecuteAll"><constraint repeatuntil="" priority="" name="IfTargetNull" constraint="entityPlayerPos == null && entityPlayerTarget == null"><parallel tiebreaker="fail" succeed="all" repeatuntil="" name="ExecuteAll" fail="any"><waypointpatrol waypointsetvariable="PatrolRoute" waypointactiontype="patrol" traversetype="pingpong" traverseorder="forward" repeatuntil="" pathtargetvariable="" name="patrol" movetargetvariable="nextPos"><move turnspeed="" repeatuntil="" name="move" movetarget="nextPos" movespeed="2" facetarget="" closeenoughdistance="" closeenoughangle="" /></waypointpatrol></parallel></constraint><constraint repeatuntil="" priority="" name="If PlayerFound" constraint="entityPlayerPos != null"><parallel tiebreaker="fail" succeed="all" repeatuntil="" name="parallel" fail="any"><constraint repeatuntil="" name="IfInRange" constraint="entityPlayerTarget != null "><parallel tiebreaker="fail" succeed="all" repeatuntil="" name="ExecuteAll" fail="any"><action repeatuntil="" parametervalues="" parameters="" namespace="(global)" name="ShootPlayer" classname="aiShootPlayer" /></parallel></constraint><constraint repeatuntil="" name="IfNotInRange" constraint="entityPlayerTaget == null"><parallel tiebreaker="fail" succeed="all" repeatuntil="" name="ExecuteAll" fail="any"><move turnspeed="" repeatuntil="" name="move" movetarget="entityPlayerPos" movespeed="2" facetarget="" closeenoughdistance="" closeenoughangle="" /></parallel></constraint></parallel></constraint></selector></parallel></behaviortree>





    Your code is running in a RAINAction, not in a MonoBehaviour. “this” is not a unity component and so “this.transform” is not valid. You probably want “ai.Body.transform”, but you will have to pass that in to your shoot function.

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

You must be logged in to reply to this topic.