- Home /
How to move an object allong an axis on Mouse Over
I am trying to make a menu that moves when the mouse is hovering over objects. Basically I have a plane made up of different sized and shaped boxes that serves as my menu. When the mouse hovers over one of the boxes I want it to rise up along the Y axis as well as play a short click sound. As soon as the mouse has moved away, it should fall back into its original position. I found this code here (http://answers.unity3d.com/questions/58175/how-move-objects-closer-to-camera-when-i-mouse-ove.html) but I cant get that to work. The boxes just fly towards the camera instead of up (I should mention that my camera is set at an angle) and only one box moves forever while the rest are motionless.
The most useless kind of "question" that gets posted here all the time is "I copy-pasted somebody else's script that I don't understand well enough to debug, can someone rewrite it for me so that it works?" If this is a hobby, $$anonymous$$ch yourself scripting fundamentals and take on a simpler project first that is within your understanding . If it's a job, pay someone else to write and debug the code you need.
I do try to keep my questions to the point, therefore I don't go through all the things I've tried and failed at. If my question is useless feel free to not answer it. $$anonymous$$eep in $$anonymous$$d though, that this is a place to ask questions, useless or not ;)
@Hueman - @$$anonymous$$iloblargh has a point. Unity answers is to help you code or solve your own solutions to a problem. On the surface your question was asking for a fix to a third-party script (that you did not understand). I elected to answer the question because 1) solving the specific issue was a simple and quick piece of code to write and understand, and 2) because someone else searching for an answer might benefit. But it could be argued that the question should have been rejected in the moderation queue or closed.
The takeways: 1) is is possible that similar question asked in the future would be rejected or closed, and 2) it is always better to bring your own code to the question rather than third-party code.
@agorobertbu - I understand. However, due to the fact that I am new to coding I did not want to white a novel to explain every thing I tried (which resulted in a very messed up code). So, for the ease of the would-be-answerer I choose to post the code that already had some success (Also showing that I did research, not just asked as soon as it came into my head). Despite the fact that my useless question passed moderation, I will not post anymore third party code in this manner. I apologize for the question, but I do thank you for your help and explanation.
Answer by robertbu · Mar 01, 2014 at 04:56 AM
Here is a starter script. The idea is to define two positions: upPos and dnPos. The code in Update() is always try to move the object to currPos which is set to either upPos or dnPos:
#pragma strict
public var upAmount = 0.2;
public var speed = 0.5;
private var dnPos : Vector3;
private var upPos : Vector3;
private var currPos : Vector3;
function Start() {
dnPos = transform.position;
upPos = transform.position + Vector3.up * upAmount;
currPos = dnPos;
}
function Update() {
transform.position = Vector3.MoveTowards(transform.position, currPos, speed * Time.deltaTime);
}
function OnMouseEnter() { currPos = upPos; }
function OnMouseExit() { currPos = dnPos; }
Thank you very much. I will play around with this. Cheers.
Your answer