- Home /
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)
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.
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?
Look up the camera.ScreenPointToRay and Physics.Raycast functions in the docs
Updated OP if you have a sec to check code and error <,< thx!
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.
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;
}
}
Your answer
Follow this Question
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