- Home /
Move object forward 1 unit then stop
I'm building an animated main menu for my game and I was wondering how I would get it so that when the mouse hovers over the object it moves forward 1 unit, and stops. No more, I have only got it so that it will keep moving forward while the mouse is on it. The only other solution I can think of is placing an invisible wall that it will stop on contact. But if anyone has a proper solution I'd be ecstatic to hear it! Here's my code!
var pLight: Transform;
private var enableLight: boolean = false;
pLight.light.enabled = false;
function OnMouseOver () {
renderer.material.color -= Color(0, 0, 100) * Time.deltaTime;
pLight.light.enabled = true;
}
function OnMouseExit () {
pLight.light.enabled = false;
}
Answer by Linus · Jul 05, 2012 at 11:36 AM
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseEnter.html
function OnMouseEnter(){
transform.position.z += 1; //Change to x if you prefer
}
Hope that works for you.
It does, but I realized it's a lost cause as the items still end up moving forward regardless of me putting the code for it to move back 1 on mouse exit.
Answer by whydoidoit · Jul 05, 2012 at 11:53 AM
You could try this:
var currentPosition : Vector3;
var moveDirection : Vector3 = Vector3(0,0,1); //For example
var t : float;
function OnMouseEnter() {
currentPosition = transform.position;
t = 0;
}
function OnMouseOver() {
t = Mathf.Clamp01(Time.deltaTime + t);
transform.position = currentPosition + (moveDirection * t);
}
function OnMouseExit() {
transform.position = currentPosition;
}