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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Searzocom · Oct 06, 2012 at 07:38 PM · transform.positiondistancevectorray

How to make the object stop moving within a certain distance with the target.

My first post here and I have a problem, (as well as some other generic questions.)

OK so, I have made a script for making an object move towards a position that I click on with my mouse. Problem here is, when my object hits the location specified, it does not stop, but rather spins around non-stop in a tight circle around the location. My question is how do I get this thing to stop?

Also if anyone can tell me how to calculate the distance between my object and the position I clicked on, that would be amazing.

Also, Input.mousePosition gives me the co ordinate on my screen of where my mouse is, is there a way to easily convert the point to a transform.position in the world?

Note: this code is basically a noob's "Grumping" of this one http://www.unifycommunity.com/wiki/index.php?title=LookAtMouse #pragma strict

 var moveOrders:Vector3;
 
 var speed = 2.0;
 
 function Update ()
 
 {
 	
 	var playerPlane = new Plane (Vector3.up, transform.position);
 	
 	
 	if (Input.GetKey (KeyCode.Mouse1))
 	{
 		
 	moveOrders= Input.mousePosition;
 	
 	
 	}
 
 	Debug.Log (moveOrders);
 	var ray = Camera.main.ScreenPointToRay (moveOrders);
 	var hitdist = 0.0;
 	if (moveOrders != Vector3 (0.0,0.0,0.0))
 	{
 		if (playerPlane.Raycast (ray, hitdist))
 		{
 			var targetPoint = ray.GetPoint(hitdist);
 			
 			var targetRotation = Quaternion.LookRotation (targetPoint - transform.position);
 			 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
 			
 			  transform.position += transform.forward * speed * Time.deltaTime;
 			if (transform.position== moveOrders)
 				{
 					moveOrders= Vector3(0,0,0);
 				}
 			}
 		if (transform.position- moveOrders == Vector3.zero)
 		{
 			moveOrders= Vector3(0,0,0);
 		}
 	}
 
 }
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
0

Answer by whydoidoit · Oct 06, 2012 at 07:43 PM

So the position you clicked on in the world -

This can be kind of tricky - you can use Camera.main.ScreenToWorldPoint but you have to specify at what Z distance you are clicking. So you probably have some kind of ground that you want to click on? If you do you can cast a ray from the camera, through the screen point and use the place it hits your ground. Fastest way to do that is to use the collider attached to your ground and do a collider based Raycast - the example in that link shows doing it with a screen position. The resulting RaycastHit contains a .point variable which is the point on the collider that was clicked on.

To measure the distance just use Vector3.Distance passing the two positions.

Clearly stopping your thing is a matter of measuring the distance and stopping if it is < some value (don't do == 0, it's not accurate enough).

Comment
Add comment · 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

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

10 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

Related Questions

How to determine multiple points along a ray? 1 Answer

Bounds IntersectRay distance isnt correct 1 Answer

Finding Nearest Object Both Positive And Negative? 1 Answer

3 vector3 distance checks, only one works 1 Answer

Aim a transform at a point along a vector 0 Answers


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