Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ma22be61 · Jul 28, 2014 at 08:19 PM · guimain camera

Move the Main Camera with OnMouseUp

Hello guys I need to move my Main Camera when my Select level is pressed (I use OnMouseUp)... I use for destination an empty gameobjectalt text

immagine.png (138.0 kB)
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
0
Best Answer

Answer by TeoL · Jul 29, 2014 at 01:18 AM

You'll need to reference your empty game object somehow, either by including it in your camera script as a public var, or by giving it some kind of name and pulling it in that way. The second method is perhaps more flexible, so I'll use that one.

In your script that handles the OnMouseUp for your select level menu item, you'll need to make a few changes. First, you'll need to add a variable to hold the empty game object that is the camera's target. Do that in the class definition, before your Start funtion:

 Private GameObject cameraTarget;

Then, add the following line to your Start function to reference the empty game object you made:

 void Start() {
   //...your other code here
   cameraTarget = GameObject.find("SelectLevelCameraTarget");
 }

For that to work, you'll need to give your empty game object the name "SelectLevelCameraTarget", so go ahead and do that.

Then, in your OnMouseUp function:

 void OnMouseUp() {
   //..anything you need to happen before camera move
   Camera.main.transform = new Vector3(Camera.main.transform.x, cameraTarget.transform.y, Camera.main.transform.z);
   //..anything you need to happen after camera move
 } 

NOW - what this will do is set the camera's Y position equal to the Y position of your SelectLevelCameraTarget. I'm assuming Y because you said you need your camera to move "up" - if you need it to move in other dimensions, you can modify accordingly. HOWEVER - this script will "jump" the camera instantly to that position. If you want to animate it "smoothly" over time, you'll have to lerp the camera's position. This is probably best done with a coroutine. These can seem a little bit intimidating, but they're not so bad - there's a great little tutorial vid here: http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines

To lerp the camera's position so it animates instead of just jumping, you'll need to change the above code to be something more like this:

 void OnMouseUp() {
   //..anything you need to happen before camera move
   StartCoroutine(MoveCamera());
   //..anything you need to happen after camera move
 } 
 
 IEnumerator MoveCamera() {
   while(Vector3.Distance(Camera.main.transform.position, cameraTarget.transform.position) > 0.05f) {
     Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, cameraTarget.transform.position, Time.deltaTime)
     yield return null;
   }
 }


If you want to change the speed at which the camera moves, you'll need to create a float called "camSpeed" or something similar right above where you declare cameraTarget, and then in your coroutine multiply Time.deltaTime * camSpeed.

Hope that helps.

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

Answer by nomitmayankraj2000 · Apr 21, 2021 at 12:36 PM

Some how i manage to move my game object but my game object moves toward Camera can any one correct my code and send me. please! public class NewBehaviourScript : MonoBehaviour {

  public float speed = 1.5f;
  private Vector3 target;
 
  void Start () {
      target = transform.position;
  }
  
  void Update () {
      if (Input.GetMouseButtonDown(0)) {
          target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
          target.z = transform.position.z;
      }
      transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
  }    

}

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

What's wrong with my function? It keeps telling me 'loseText' is not a member of 'UnityEngine.GUIText'. Please help, please and thank you! 0 Answers

Create big city for car game? 2 Answers

Tracking Down GUI Errors 0 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