- Home /
How move objects closer to camera when i mouse over it ??
I have seen a tutorial when one person made a 3d menu and when you select it it moves closer to the camera(it pulls out). I made it but the tutorial shows only when i do it with keyboard buttons (up and down). I want to do it with the mouse(when i mouse over it). How you see in MenuManager there is if(Input.GetAxisRaw("vertical"). Is there a way to do it with mouse ?? I mean i can pull out selected objects with up and down button of the keyboard. I want to pull out objects when i mouse over it. Can you pls help ?
Pls help
I will show you the java script:
menuItem
function OnSelected(on:boolean)
{
//I just got selected
if(on)
{
iTween.MoveTo(gameObject,{"z":-5, "time":0.5});
}
else //I just got deselected
{
iTween.MoveTo(gameObject,{"z":-2, "time":0.5});
}
}
MenuManager
var menuItems:menuItem[]; // list of MenuItem(s)
var currentMenuItem:int = 0;
var keyDelay:float = 0.25;
function Start() { menuItems[currentMenuItem].OnSelected(true);
while(true)
{
if(Input.GetAxisRaw("Vertical") > 0.9)
{
menuItems[currentMenuItem].OnSelected(false);
currentMenuItem--;
if(currentMenuItem < 0) currentMenuItem = 0;
menuItems[currentMenuItem].OnSelected(true);
yield new WaitForSeconds(keyDelay);
}
else if(Input.GetAxisRaw("Vertical") < -0.9)
{
menuItems[currentMenuItem].OnSelected(false);
currentMenuItem++;
if(currentMenuItem >= menuItems.length) currentMenuItem = menuItems.length - 1;
menuItems[currentMenuItem].OnSelected(true);
yield new WaitForSeconds(keyDelay);
}
yield;
}
}
Do you also want the object returns to its last position after its deselected/mouseOut?
yes i want the object to return when i mouseout. By selecting i mean mouse over it.
Answer by Kourosh · Apr 13, 2011 at 08:06 PM
Here's something basic for implementing what you have in mind. However you might wanna work on it to make nicer movements.
create buncha cubes and apply this script to you camera.
var speed:float = 0.07;
private var originalPosition:Vector3; private var hoverObject:Transform; private var hover:boolean = false; private var destination:Vector3;
function Update () {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
if(!hover){
hoverObject = hit.transform;
originalPosition = hoverObject.transform.position;
hover = true;
}
} else {
hover = false;
}
if(hover){
var p : Vector3 = camera.ScreenToWorldPoint (Vector3 (Input.mousePosition.x,Input.mousePosition.y,camera.nearClipPlane));
destination = p;
} else {
destination = originalPosition;
}
if(hoverObject != null)
hoverObject.transform.position = Vector3.MoveTowards(hoverObject.transform.position,destination,speed); // Here is the line where you would swap it with iTween functions using destination variable.
}
Good luck!
God thank you i love you dude. That's what i was looking for. Now i must play with it to make the cubes stop when they are close enough. Thank you again
change "camera.nearClipPlane" to any z value you want to control how close they should get. I'm glad it helped.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122543im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Setting a variable to an instantiated object. 1 Answer
touch 3d object open gui 1 Answer
Drawing a 3D object javascipt 0 Answers
Disable GameObject Only Father Not Children 2 Answers
Rotating an Object on Key Press! 3 Answers