Object selecting

Basically, I have got game object selecting to work, like you click some object in-game and something happens, like an animation or something.

But what’s not right, is that for example if I make the player to perform an animation when he clicks on an in-game object, lets say table, what’s not right is that then the player could just stand on the other side somewhere far away from that table object, click on it and he would perform animation, and it’s just wrong :confused:

What I want to happen is, either:
-If player is far away from that object and click on it nothing would happen, he’d have to be close/ right next to the object and click again for the action to be performed.

or

-Basically what I just said above, but player would automatically walk to the object if he clicks it when far away and the action would perform after he walks to the object.

How could I make this work?
I have object selecting and all that working, only need to know how to add this distance stuff.

Thank’s, Djoscar.

First off use the :

float distanceToObject = Vector3.Distance(playerObject.transform.position, objectClicked.transform.position);

That will get you the distance to the object clicked. Now you can do what you like with it. For instance in my game I have a loot chest when an enemy dies. If the player clicks on the chest and is more a foot or so away I display the message “Too far to loot the chest”. You could either display a similar message OR you could get the clicked objects position (see code above) and then have the player move to it. If you are using CharacterController on the player use
the Move method on that object.

Something like:

characterController.Move(moveDirection * Time.deltaTime);

make sense?

Thanks, makes sense but I’m kind of new to Unity and gonna have to sweat a little to get it to work :smile:

Could you maybe show your script you use for object selecyion?

I could really use that since I am working on almost the same thing, but I lack the right way to select the object I am clicking on. And then after he should move to the selected object like this thred is about.

MiLu,

Go ahead and put up the code that you are working on yourself. Oscar’s thread is old, and (knowing him) I’m sure he solved his problem months ago. He moves fast!

So put up your code or start a thread all your own and we can help you.

Basically, to be able to click on object you need object to have a collider of some kind. With a collider attached, you make a script with OnMouseOver, OnMouseEnter, OnMouseExit, as shown here:

I’ve changed
var ray = this.transform.position; to
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

Then I got no errors when I save my script, but I get a new error when I try to move to my selected NPC(Object)

Something like this hmm…

NullReferenceException: Object reference not set to an instance of an object
WalkTo.WalkTo () (at Assets\Scripts\Object ManageMent\Talk\WalkTo.js:40)
WalkTo.DialogueStart () (at Assets\Scripts\Object ManageMent\Talk\WalkTo.js:9)
UnityEngine.Component:SendMessage(String, Object, SendMessageOptions)
UnityEngine.Component:SendMessage(String, Object, SendMessageOptions)
UnityEngine.Component:SendMessage(String)
Interactive:Talk() (at Assets\Scripts\Object ManageMent\Interactive.js:39)
UnityEngine.Component:SendMessage(String, Object, SendMessageOptions)
UnityEngine.Component:SendMessage(String, Object, SendMessageOptions)
UnityEngine.Component:SendMessage(String)
Cursor:Update() (at Assets\Scripts\GUI\Cursor.js:61)

Is it possible that there is nothing assigned to the playerActor variable in the inspector?

I’ve tried to aproach it in another angle which I have had another thread running for in a while now. It can be found here: http://forum.unity3d.com/viewtopic.php?p=285108#285108

There I have got my selection to work and all but got some script reference issues before it all should be working correct. I hope! :slight_smile: