casting / interrupting

I’m trying to findout what’s the best way about creating a simple casting system that can be interrupted if the player moves. So if the player casts a spell and it takes 4 seconds to cast, the player must remain still or else the spell will be cancelled. If the player stands in place for 4 seconds then spell will be casted.

I know how to create cooldown timers using time but am unsure how to completely cancel the spell mid cast if the player moves. Any help would be amazing.

what you can do is create a variable that tracks the players position every frame, and gets reassigned very frame, and check to see if the differ, something like this:

private Vector3 lastPosition;

void Update () {
	if (lastPosition != transform.position) {
		//player moved
	}
	lastPosition == transform.position;
}

(this is in C# syntax BTW)

I can easily convert it to Java. I’m still not sure how I would instantly cancel a specific cast. Like if I started a cast that goes for 4 seconds and it’s been 2 seconds how would I stop the casting timer and display in log at the exact time say spell cancelled ( player moved ).

Do you have the code for casting even written? It depends on how you wrote it mainly

I’ve only done cooldown timers which I used waitForSeconds. But for the casting timer I might do something like this. Haven’t tested it yet.

var castTime;
var duration = 4.0;

if(Time.time > castTime){

castTime = Time.time + duration;

}

ok, I just designed this real quick, I dont normally do JS stuff, so some of it may have syntax errors

var castDuration = 4.0;
var castTime = 0.0;
var casting = false;
var lastPosition : Vector3;

function Update () {
	//if (start condition for cast) {
		//casting = true;
		//lastPosition = transform.Position;
	//}
	
	if (casting) {
		if (lastPosition != transform.Position) {
			casting = false;
			castTime = 0;
		}
		
		lastPosition = transform.Position;
		
		if (castTime < castDuration) {
			castTime += Time.deltaTime;
		}
		
		else {
			//casting code goes here
			casting = false;
			castTime = 0;
		}
	}
}

this is the basic concept of it

Thanks a ton! I shall give it a try and see how it works out. Will let you know if I get any errors or I will attempt to fix them.

ok, no problem

Changed the code up a little. It doesn’t like the lastPosition variable for some reason, says not set to an instance of an object. If I remove the vector3 from the variable, the spell will be casted after 4 seconds but can’t be interrupted.

    var castDuration : float = 4.0;

    var castTime : float = 0.0;

    var casting : boolean = false;

    var lastPosition : Vector3;

     

     

  

     

     

     

    function Update () {

        

        if(Input.GetKeyDown("7")) {

        

        casting = true;

        

        }

       

        if (casting) {

            if (lastPosition != transform.Position) {

                casting = false;

                castTime = 0;

                Debug.Log("spell cancelled");

            }

           

            lastPosition = transform.Position;

           

            if (castTime < castDuration) {

                castTime += Time.deltaTime;

            }

           

            else {

                Debug.Log("casted the spell");

                //casting code goes here

                casting = false;

                castTime = 0;

            }

        }

    }

ok, change that line with last position in it to

var lastPosition : Vector3 = Vector3.zero;

also, make sure that you have “lastPosition = transform.position;” in the same if statement where casting is se to true, where its checking if the player pressed 7

I fixed it so there’s no more errors but for some reason it does not cancel when the player moves :confused:

    var castDuration : float = 4.0;

    var castTime : float = 0.0;

    var casting : boolean = false;

    var lastPosition;

     

     

  function Start () {

  

lastPosition = transform.Position;

  

  

  }

     

     

     

    function Update () {

        

        if(Input.GetKeyDown("7")) {

        lastPosition = transform.Position;

       

        casting = true;

        

        }

       

        if (casting) {

            if (lastPosition != transform.Position) {

                casting = false;

                castTime = 0;

                Debug.Log("spell cancelled");

            }

           

             lastPosition = transform.Position;

           

            if (castTime < castDuration) {

                castTime += Time.deltaTime;

            }

           

            else {

                Debug.Log("casted the spell");

                //casting code goes here

                casting = false;

                castTime = 0;

            }

        }

    }

Ill have to check it when i get home, it looks fine from what i see though… Is it attached to the player? And is the player set up to move?

Yes it is attached to the player and the player is able to move.

I dont know, but this code

#pragma strict

var castDuration : float = 4.0;
var castTime : float = 0.0;
var casting : boolean = false;
var lastPosition;

function Update () {
	if(Input.GetKeyDown("7")) {
		lastPosition = transform.position;
		casting = true;
	}
	
	if (casting) {
		if (lastPosition != transform.position) {
			casting = false;
			castTime = 0;
			Debug.Log("spell cancelled");
		}
		
		lastPosition = transform.position;
		
		if (castTime < castDuration) {
			castTime += Time.deltaTime;
		}
		
		else {
			Debug.Log("casted the spell");
			//casting code goes here
			casting = false;
			castTime = 0;
		}
	}
}

works perfectly for me…

Does work perfect! Thanks a ton!

no problem