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 azmundai · Jun 21, 2010 at 08:12 AM · cameramovetargetexceptionmissingcomponentexception

Camera and Target help.

I am trying to layout a quick and easy top down checkers like game, but I am so noob at this I can't even get the basics as of yet.

I have a scene with 2 objects (call them checkers) a camera and a light.

What do I have to do to :

A. Make it so when I play the game, I can select the two different objects? Do I have to add a script to each object to make them targetable? Is there one already written?

B. Have the camera move from one object to the other while keeping it's current offset.

EDIT 1 : Ok this is what I have, but im getting an error :{

function Update () { var ray = camera.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit)) { TargetPiece = hit.rigidbody; }

 if(Input.GetButtonDown("Mouse0"))
 {
     camera.main.transform.position = TargetPiece.position + Vector3(1,1.414214,1);
 }

}

Error =

MissingComponentException: There is no 'Camera' attached to the "RC_Knight" game object, but a script is trying to access it. You probably need to add a Camera to the game object "RC_Knight". Or your script needs to check if the component is attached before using it.

ChangeTarget.Update () (at Assets\Pieces\Pieces_Scripts\ChangeTarget.js:3)

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

3 Replies

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

Answer by StephanK · Jun 21, 2010 at 08:42 AM

A: Yes the easiest way to do this is to attach a script to both objects, that implements void OnMouseDown() and changes the target of the camera. You could also have a single script attached to the camera, that uses raycasting to detect which object you selected and change camera.

B: If you want to move the camera immediately you can just move it to the position like this:

void SetCameraTo(Transform position) {
  // assuming that you have a Vector3 offset setup somewhere in your script
  transform.position = position + offset;
}

You can also use a coroutine and/or Vector3.Lerp to gradually move the camera to its new position.

Comment
Add comment · Show 5 · 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 azmundai · Jun 21, 2010 at 09:10 AM 0
Share

Thanks, I am working on it but I cannot figure out how to get the position of the target. The offset would be : vector3(1,1.414213,1)

I havent used raycasting before for anything let alone to find a target. Can you point me to a good source for understanding that?

avatar image StephanK · Jun 21, 2010 at 09:41 AM 0
Share

Look up the camera.ScreenPointToRay and Physics.Raycast functions in the docs

avatar image azmundai · Jun 21, 2010 at 10:54 AM 0
Share

Updated OP if you have a sec to check code and error <,< thx!

avatar image StephanK · Jun 21, 2010 at 02:20 PM 0
Share

try Camera.main.ScreenPointToRay

avatar image azmundai · Jun 22, 2010 at 12:02 AM 0
Share

9000 points! It works! Thanks!

avatar image
0
Best Answer

Answer by azmundai · Jun 22, 2010 at 12:08 AM

Thanks to Spree here is working code for centering your camera on an object you left click.

var cameraOffset = Vector3(1,1.414214,1);

function Update () { var MyCamera = GameObject.Find("Main Camera"); var MytargetPosition = MyCamera.transform.position; var ray = camera.main.ScreenPointToRay(Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit)) { MytargetPosition = hit.transform.position; }

 if(Input.GetButtonDown("Mouse0"))
 {
     camera.main.transform.position = MytargetPosition + cameraOffset;
 }

}

Note : "mouse0" is a keybind. Goto EDIT > PROJECT SETTINGS > INPUT. In the Inspector, change the size by +1. Name the new one Mouse0. Change the Positive Button to mouse 0 (with space).

You can name it anything you want really, but then you have to change it in the script, obviously. mouse 0 (no upper case) needs to be the exact phrase in Positive Button for the left mouse button.

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 Student · Apr 14, 2011 at 12:53 PM

Azmundai or someone, could you please convert your code to C#? Thank you.

Edit: As a newbie: I added following code with no success

UnityEngine; using System.Collections;

 public class hitted : MonoBehaviour

{

RaycastHit myhit = new RaycastHit();

 Ray myray = new Ray();
 private GameObject mycamera;
 private Transform MytargetPosition;

void Update(){

 mycamera=GameObject.Find("Main Camera");
     myray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if (Physics.Raycast(myray, out myhit, 1000.0f) && Input.GetMouseButtonDown(0))
         print(myhit.collider.name);
     Debug.DrawLine(myray.origin, myhit.point);
     MytargetPosition.transform.position = myhit.transform.position;
     mycamera.transform.position=MytargetPosition.transform.position;
 }

}

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

No one has followed this question yet.

Related Questions

Moving Camera With 2 Players 3 Answers

How to move the camera depending on the score? 1 Answer

Moving two objects and avoiding overlap with each other 1 Answer

3rd person player 0 Answers

Camera focusing on an object specified in a script 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