Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rgalaxy · Dec 12, 2013 at 09:19 AM · objectmovingpointclicked

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,,

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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..)

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rgalaxy · Dec 12, 2013 at 05:50 PM 0
Share

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?

avatar image Seth-Bergman · Dec 13, 2013 at 02:33 PM 0
Share

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.

avatar image Rgalaxy · Dec 13, 2013 at 08:26 PM 0
Share

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);
avatar image Seth-Bergman · Dec 13, 2013 at 10:38 PM 0
Share

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

avatar image Seth-Bergman · Dec 14, 2013 at 08:37 AM 1
Share

it should be like this:

 var male : GameObject;

 function Start(){
   male = GameObject.Find("Boy");
 }

(I have added it to my answer)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges