- Home /
Moving to an object i click.
Hello.. im trying to achieve way to do this,,
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,,
Answer by Seth-Bergman · Dec 12, 2013 at 09:59 AM
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
Application.LoadLevel(hit.collider.gameObject.GetComponent(Stage).stage);
(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..)
wow, this is great, i will try to do it like this, but btw what is raycast? i know its a common for people to use this, but i dont raelly understand what he does..?
and also, by u mean checking a collider, hit.collider my character will not collide with the object i clicked?
have a look at the picture here:
http://answers.unity3d.com/questions/500938/what-does-physics-casting-actually-do.html
in this example, I am showing a SPHERECAST... But a raycast is just the same, except ins$$anonymous$$d of a sphere, you are shooting a ray along a single point, so it's just a line(no sphere).. from the origin given, in a specific direction. In my answer above, we define the ray with the line:
Camera.main.ScreenPointToRay (Input.mousePosition);
by using Camera.main, I am assu$$anonymous$$g that the camera being used is the main camera (tagged as "main").. unless you are using multiple cameras, this is a safe assumption, since the default camera is automatically the main camera. You can shoot a ray from any position, but in this case, we want the object which the mouse cursor is over (from our view by the camera being used), so we use the camera version for our ray (ScreenPointToRay)
http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenPointToRay.html
and mouse position
http://docs.unity3d.com/Documentation/ScriptReference/Input-mousePosition.html
(in my picture in the link, however, I am using the player, not the camera, like the example here):
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
essentially, you are shooting from a given point, in a given direction, until you reach something with a collider on it (such as a sphere collider or box collider). "hit" is a RaycastHit, as per this line:
var hit : RaycastHit;
after declaring this variable, we can use it in our raycast:
if(Physics.Raycast (ray, hit, 1000))
whatever we hit, we can then access using "hit". hit.point is the position of the actual point where we hit. To access the position of the gameObject itself, (rather than the specific point on the object we hit), we would use "hit.collider.gameObject.transform.position" ins$$anonymous$$d of "hit.point"
this does not affect whether your player will collide with the object or not.. whether or not your character will collide with the object depends on whether it (the player) has a Rigidbody or CharacterController attached. Either way though, you need to add a collider to your objects to be clicked, since a raycast requires a collider to register a hit.
Thanks you so much @Seth Bergman, u really point me out out there really really helpful :) i'll work on it now Thanks!!
ok, i've tried it now @Seth, i could see that the raycast work on the line
if(Physics.Raycast (ray, hit, 1000)){
Debug.Log("raycastHIT" + i++);
}
is there any possibilities to change where my player will landing? because when i hit the object, my player landing on a little far away from the target..
i try to change this line, but it's just not working..
male.transform.position = Vector3.$$anonymous$$oveTowards(transform.position, Vector3(-14, 3, -41), step);
try:
male.transform.position = Vector3.$$anonymous$$oveTowards(transform.position, hit.collider.gameObject.transform.position, step);
also, if your player has a CharacterController or Rigidbody, he may be falling short because he is bumping into the collider.. if this is the problem, you can use collision layers to fix this
http://docs.unity3d.com/Documentation/Components/LayerBasedCollision.html
it should be like this:
var male : GameObject;
function Start(){
male = GameObject.Find("Boy");
}
(I have added it to my answer)
Your answer
Follow this Question
Related Questions
Why is my object not following my path ?,Why is my object going through the target ? 2 Answers
Moving a object around with mouse, rotating it with arrow keys and placing it. 1 Answer
How can I make an object disappear when clicked? 3 Answers
Dragging Objects with the mouse 1 Answer
Help moving an object towards one it is looking at. 1 Answer