so its like choosing a stage, and then my character will move to that point ! just as simple as that…
the stage like every stage-choosing-menu, like candy crush but i want to play an animation where my character will move to the object i clicked…
i try to figure out how to do this, but just not sure how to code it… (and i dont know if the logic is right or not either)
function Update () {
if(Input.GetKeyDown(KeyCode.Mouse0)){
if(objectclicked==stage1){
//move to stage1 point
}
else if (objectclicked==stage2){
//move to stage2 point
}
}
}
or i think of 1 other way using function OnMouseDown() im a bit confused thou… if anybody could help me to figure out how to do this, it would be great,
it would be better to create a more reusable alternative… You can create a script which will go on every stage-item to be clicked (called Stage.js). For now, just one line of code:
var stage : int;
then, in your click script, you can use a raycast to see if you hit one:
var target : Vector3;
var clicked : boolean = false;
var speed: float = 5;
// Speed in units per sec. (adjust as needed)
var male : GameObject;
function Start(){
male = GameObject.Find("Boy");
}
function Update () {
if(Input.GetKeyDown(KeyCode.Mouse0)){
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast (ray, hit, 1000)){
if(hit.collider.gameObject.GetComponent(Stage)){
//you hit a Stage object, so move there
clicked = true;
target = hit.collider.gameObject.transform.position;
}
}
}
if(clicked){
// The step size is equal to speed times frame time.
var step = speed * Time.deltaTime;
male.transform.position = Vector3.MoveTowards(male.transform.position, target, step);
}
}
note, once you click on an object (which of course must contain a collider), you can then use:
hit.collider.gameObject.GetComponent(Stage)
to access the script attached, so in each script, you could give a unique stage number… then use
(just as an example… of course you would want to add code to wait until the player is done moving…)
(code untested, but hopefully this gives you an idea…)