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 /
avatar image
0
Question by romeo046 · May 06, 2014 at 06:44 PM · gameobjectmovemouse-over

Moving gameObject forward

Hey,

I'm trying to move my gameobject forward for specific vector by using MouseOver.When MouseExit it should move back. Momently my object moves forward.... just forward with speed. I don't know how to say how far it should move and how to go back...?

 void OnMouseOver() {
     gameObject.renderer.material.color = Color.cyan;
     gameObject.transform.position = new Vector3(transform.position.x,   transform.position.y, transform.position.z + 0.1f);
 }
     
 void OnMouseExit(){
     gameObject.renderer.material.color = Color.white;
 }

Thanks for taking a look

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · May 06, 2014 at 09:49 PM

As with most things in Unity, there are multiple approaches and solutions. Here is one solution:

 using UnityEngine;
 using System.Collections;
 
 public class Bug25b : MonoBehaviour {
 
     public float speed = 2.0f;  // Units per second 
 
     private Vector3 startPos;
     private Vector3 forwardPos;
     private Vector3 toPos;
 
     void Start() {
         startPos = transform.position;
         toPos = startPos;
         forwardPos = transform.position + transform.forward * 1000000.0f;
     }
 
     void Update() {
         transform.position = Vector3.MoveTowards(transform.position, toPos, speed * Time.deltaTime);
     }
 
     void OnMouseEnter() {
         gameObject.renderer.material.color = Color.cyan;
         toPos = forwardPos;
     }
     
     void OnMouseExit(){
         gameObject.renderer.material.color = Color.white;
         toPos = startPos;
     }
 }
                 
Comment
Add comment · Show 4 · 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
avatar image romeo046 · May 07, 2014 at 10:40 AM 0
Share

Hi, your code works but when my object is on its start position it is unsettled. Don't know how to discribe it better... It doesn't stand still at its position. Do you know why? And how to change it? Another problem is, that I want to limit mouseEnter. So that it can just move forward one time. I think a if-loop would save this problem, not?

avatar image ThePunisher · May 08, 2014 at 04:25 PM 0
Share

romeo046, you've been given a very good head-start by robertu. Re-post the script with changes you've attempted to make and we can take it from there.

avatar image Ancy · Sep 30, 2015 at 09:03 AM 0
Share

public Vector3 orgPos,fwdPos; void Start() { orgPos=transform.position; fwdPos=(transform.position.x,transform.position.y,transform.position.z-0.1f); } void On$$anonymous$$ouseEnter() { transform.position=fwdPos; }void On$$anonymous$$ouseExit() { transform.position=orgPos; }

avatar image meat5000 ♦ · Sep 30, 2015 at 09:04 AM 0
Share

@Ancy Format your code

avatar image
0

Answer by Ancy · Nov 06, 2015 at 02:26 PM

public Vector3 orgPos,fwdPos; void Start() { orgPos=transform.position; fwdPos=(transform.position.x,transform.position.y,transform.position.z-0.1f); } void OnMouseEnter() { transform.position=fwdPos; } void OnMouseExit() { transform.position=orgPos; }

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Moving GameObject to various position ? 1 Answer

Problem dragging, scaling, moving objects in #scene view with mouse 0 Answers

Use Vector Lerp or other to move back with arc 2 Answers

Move child gameobject in x axis (Lerp) 1 Answer

check if object is moving 5 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