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 mandeep · Mar 13, 2012 at 11:43 AM · cameratargetcamera-looktargeting

Camera re-target

Hi, everyone

Is there a script that when I click a certain object, the object should become new target of camera each time i click.

i am using this code for camera.

 //WASD to orbit, left Ctrl/Alt to zoom
 using UnityEngine;
 using System.Collections;
 
 [AddComponentMenu("Camera-Control/Keyboard Orbit")]
 
 public class Guikeys : MonoBehaviour {
     public Transform target;
     public float distance = 20.0f;
     public float zoomSpd = 2.0f;
 
     public float xSpeed = 240.0f;
     public float ySpeed = 123.0f;
 
     public int yMinLimit = -723;
     public int yMaxLimit = 877;
 
     private float x = 0.0f;
     private float y = 0.0f;
     
     private float GUIHorizontal = 0;
     private float GUIVertical = 0;
     
     public Texture uptexture;
     public Texture lefttexture;
     public Texture righttexture;
     public Texture downtexture;
     
     public void Start () {
         Vector3 angles = transform.eulerAngles;
         x = angles.y;
         y = angles.x;
 
         // Make the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }
 
     
     
 void OnGUI()
 {
     if(Event.current.type == EventType.Repaint)
     {
         GUIHorizontal = 0;
         GUIVertical = 0;
     }
     GUILayout.BeginVertical();
     if(GUI.RepeatButton(new Rect(70,600,30,30), uptexture))
     {
         GUIVertical = 1;
     }
     GUILayout.BeginHorizontal();
     if(GUI.RepeatButton(new Rect(35, 630, 30, 30), lefttexture))
     {
         GUIHorizontal = -1;
     }
     if(GUI.RepeatButton(new Rect(103, 630, 30, 30), righttexture))
     {
         GUIHorizontal = 1;
     }
     GUILayout.EndHorizontal();
     if(GUI.RepeatButton(new Rect(70, 658, 30, 30), downtexture))
     {
         GUIVertical = -1;
     }
     GUILayout.EndVertical();
 }
 
 
     public void LateUpdate () {
         if (target) {
             
             
             x -= Input.GetAxis("Horizontal") * xSpeed * 0.02f;
             y += Input.GetAxis("Vertical") * ySpeed * 0.02f;
             
             
             x -= GUIHorizontal * xSpeed * 0.02f;
             y += GUIVertical * ySpeed * 0.02f;
             
             y = ClampAngle(y, yMinLimit, yMaxLimit);
             
         distance -= Input.GetAxis("Fire1") *zoomSpd* 0.02f;
             distance += Input.GetAxis("Fire2") *zoomSpd* 0.02f;
             
             Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
             Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
             
             transform.rotation = rotation;
             transform.position = position;
         }
     }
 
     public static float ClampAngle (float angle, float min, float max) {
         if (angle < -360.0f)
             angle += 360.0f;
         if (angle > 360.0f)
             angle -= 360.0f;
         return Mathf.Clamp (angle, min, max);
         
     
     }
 }

Thanx

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

Answer by Bazsee · Mar 13, 2012 at 03:44 PM

I'm a bit new to Unity but i think I know a solution. (Sorry if it's not good)

You should make the 'target' variable static in the orbit script, then put a script on the objects that are clickable:

 function OnMouseDown () { 
 
  //Supposed your orbit script's name is MouseOrbit.js or MouseOrbit.cs  
  MouseOrbit.target = transform.transform;
 
 }
Comment
Add comment · Show 12 · 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 mandeep · Mar 14, 2012 at 09:22 AM 0
Share

Hi, @Bazsee

thanx for replay,

i m also new to unity and c# and java.

i understood second part but i don't know how to make static variable.

in my camera orbit script. i need to make static variable, my camera orbit script is c# so, i m trying this,

static var target;

am i anywhere near to ans ?

thnax

avatar image Bazsee · Mar 14, 2012 at 09:29 AM 0
Share

try:

static public Transform target;

"static var target" is javascript

avatar image mandeep · Mar 14, 2012 at 09:41 AM 0
Share

i tried this variable it's gives an error ..

$$anonymous$$ identifier: 'Guikeys'. The type Guikeys' already contains a definition for target'

i used this script on object

function On$$anonymous$$ouseDown () {

Guikeys.target = transform.transform; }

"Guikeys" is my camera orbit script name.

thanx

avatar image Bazsee · Mar 14, 2012 at 09:51 AM 0
Share

try changing:

static public Transform target;

to

public static Transform target;

(i wrote that down incorrectly before)

avatar image Bazsee · Mar 14, 2012 at 09:52 AM 0
Share

oh and make sure that the script on the object is not C# but JavaScript

Show more comments
avatar image
0

Answer by Bazsee · Mar 14, 2012 at 12:22 PM

try:

static public Transform target;

(or without the public tag, but i suppose you should leave that there)

'static var target' is javascript

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Player camera targeting/locking another object 0 Answers

Camera target on the GameObject which collision 1 Answer

Change the object the camera looks at? 1 Answer

Change 3D Camera to 2D 0 Answers

I need help with a model 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