Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by arturoza · Mar 26, 2016 at 06:41 PM · cameravector3teleport

Teleport camera to where is pointing at

Hi, so I have attached an event trigger to a cube and I want my camera to teleport to where the cube is currently at,

so far I just created this method, it works fine but if the cube moves or if I want to add another cube, I would have to adjust the coordinates manually, is there a way to just use the coordinates from an object and stablish them as the new position of my camera?

     public void MoveUp()
 
     {
         transform.position += new Vector3 (6f, 2f, 1f);
     }
Comment
Add comment · Show 1
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 arturoza · Mar 26, 2016 at 07:16 PM 0
Share

I'd also like to add a tag to all the objects, so every cube that has that tag would be teleported to if the camera clicks on it.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Geometrical · Mar 26, 2016 at 11:19 PM

Just quickly wrote up a script to do exactly what you need out of sheer courtesy. All you need to do is change the tag on the cubes you wish the camera to teleport to.

CameraTeleport.cs

 using UnityEngine;
 using System.Collections;
 
 public class CameraTeleport : MonoBehaviour {
 
     // The tag of the cubes you want to teleport to
     public string cameraTeleportTag = "TeleportCube";
 
     // The offset to apply to the centre of the cube when teleporting for fine tuning
     public Vector3 teleportOffset = Vector3.zero;
 
     private void Update () {
         // Check if the left mouse button is being pressed
         if (Input.GetMouseButtonDown (0)) {
             // Stores collision information
             RaycastHit raycastHit;
 
             // Create a ray from screen coordinates of the mouse into the world space
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
             // Cast our ray and see if it hits anything
             if (Physics.Raycast (ray.origin, ray.direction, out raycastHit, float.PositiveInfinity)) {
 
                 // The gameobject we hit
                 GameObject raycastHitTarget = raycastHit.collider.gameObject;
 
                 // Check if we hit a cube to teleport to by its tag
                 if (raycastHitTarget.tag == cameraTeleportTag) {
 
                     // Teleport the cube
                     transform.position = raycastHit.transform.position;
                 }
             }
         }
     }
 }

Comment
Add comment · Show 2 · 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 arturoza · Mar 26, 2016 at 11:43 PM 0
Share

Exactly what I needed, thank you!

avatar image Geometrical arturoza · Mar 27, 2016 at 12:03 AM 0
Share

Welcomerino :)

avatar image
1

Answer by Vallar · Mar 26, 2016 at 07:57 PM

If I understand correctly, you want to set the position of the camera to the cube, right?

In that case, in your camera, add a script that looks like this for example:

using UnityEngine; using System.Collections;

 public class CamerMovement: MonoBehaviour
 {
 
    public GameObject cube;
 
    void Update()
    {
          transform.position = cube.transform.position;
    }

That should set the position of the camera to the exact position of the cube every frame. So if the cube moved, the camera will move to its location.

Hope that helps.

Comment
Add comment · Show 1 · 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 arturoza · Mar 26, 2016 at 08:25 PM 0
Share

Hi, thank you, but I since there are several cubes, is there a way to move the position to the cube I'm clicking on, so if there are 3 cubes, and I click one the camera moves to that cube. I tried to do it with tags but I can't find the solution

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

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

Related Questions

Vector3 is increasing infinitely instead of capping at the if statement limit,Doing a smooth move for a camera but it continues to pan even though it reaches it's x and y offset 0 Answers

How I can make my camera keep a distance between two objects? 0 Answers

Google VR: Teleport the GVRMain Camera on a plane after looking on specified Cube to another Plane 0 Answers

How to teleport an object to 1 of 8 possible locations on collision? 1 Answer

Camera looking at the player at an angle 1 Answer


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