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 cthugan · Oct 14, 2011 at 10:55 PM · waypointcursor-customization

Waypoint problem

I'm trying to turn my Mouse-Cursor into a waypoint. I've done this by making a game object hold a piece of code (which i will show below) which changes the mouse-cursor image and holds a waypoint script.

But when it comes to getting my object to go to the waypoint, it goes toward the gameobject holding the code rather than the actual cursor.

Is there a way to make my object go to the cursor rather than the gameobject which holds the code?

This is the 'cursor-customising' code i'm using (Thanks to 'Stelimar' from a differnt U.A. question):

 var cursorImage : Texture;

 function Start() {
     Screen.showCursor = false;
 }
 
 function OnGUI() {
     var mousePos : Vector3 = Input.mousePosition;
     var pos : Rect = Rect(mousePos.x,Screen.height - mousePos.y,cursorImage.width,cursorImage.height);
     GUI.Label(pos,cursorImage);
 }

 

This is the waypoint code i'm using:

 var waypoint : Transform;
 var speed : float = 20;
 
 function Update () {
 var target : Vector3 = waypoint.position;
 var moveDirection : Vector3 = target - transform.position;
 
 var velocity = rigidbody.velocity;
 
 if(moveDirection.magnitude < 1){
 velocity = Vector3.zero;
 }
 else{
 velocity = moveDirection.normalized * speed;
 }
 
 rigidbody.velocity = velocity;
 
 }


Help?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kevork · Oct 14, 2011 at 11:32 PM

You need to update the position of the "waypoint" to a new 3D position that corresponds to the current 2D position of the cursor.

In the first script you want to add something like:

 Update() {
      transform.position = Camera.main.ScreenToWorldPoint(Vector3.zero + Input.mousePosition);
 }

This will move the object in 3-space corresponding to the cursor's movement. Unfortunately without knowing more about your specific project it is difficult to give you a more exact answer.

Comment
Add comment · Show 1 · 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 cthugan · Oct 18, 2011 at 11:52 AM 0
Share

Thank you for answering, i'm gonna play around with this tonight :)

avatar image
0

Answer by aldonaletto · Oct 15, 2011 at 12:01 AM

You can find the 3D point the mouse pointer is over with ScreenPointToRay and Raycast. The logic of your script must change a little, because there's no waypoint object - just a target point. If there's only one object using this script, you add to it the new code:

var target : Vector3; // use the target point instead of a Transform var speed : float = 20;

function Start(){

 target = transform.position; // initialize target point

}

function Update(){

 var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 var hit: RaycastHit;
 if (Physics.Raycast(ray, hit)){
     target = hit.point; // only update target if the ray hit something
 }
 var moveDirection : Vector3 = target - transform.position;
 moveDirection.y = 0; //# ignore vertical distance #
 var velocity = rigidbody.velocity;
 if(moveDirection.magnitude < 1){
     velocity = Vector3.zero;
 }
 else{
     velocity = moveDirection.normalized * speed;
     velocity.y = rigidbody.velocity.y; //# keep rigidbody vertical velocity #
 }
 rigidbody.velocity = velocity;

} To avoid problems with height differences between the moving object and the point clicked, you can ignore the difference in Y when measuring the distance, and set only the X and Z velocity components in order to preserve gravity. The instructions that implement this are marked with # in the comments. If you don't want this features, just comment out or delete these lines.

Comment
Add comment · Show 1 · 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 cthugan · Oct 18, 2011 at 11:52 AM 0
Share

Thanks again Aldonaletto, i'm gonna play around with this tonight :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Waypoint Error. 1 Answer

Waypoint system teeling player where to go 1 Answer

An object to follow another object through the waypoint - problem: taking a shortest path instead of walking through the waypoint. 1 Answer

waypoint script - stop at targets 2 Answers

Movement AI 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