- Home /
How can you make a new main camera?
I have seen posts like this but none seem to answer my question. I accidentally deleted my main camera a while ago so I made a new one and set the tag to main camera. But now when I try to use camera.main it does not seem to work. The goal of my game is to try to keep planets form hitting each other by dragging and dropping the drag and drop does not seem to work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragandDrop : MonoBehaviour
{
bool moveAllowed;
public Collider2D col;
// Start is called before the first frame update
void Start()
{
col = GetComponent<Collider2D>();
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
if (touch.phase == TouchPhase.Began)
{
Collider2D touchedCollider = Physics2D.OverlapPoint(touchPosition);
Debug.Log ("0");
if (col == touchedCollider)
{
moveAllowed = true;
Debug.Log ("1");
}
}
if (touch.phase == TouchPhase.Moved)
{
if (moveAllowed)
{
transform.position = new Vector2(touchPosition.x, touchPosition.y);
Debug.Log ("2");
}
}
if (touch.phase == TouchPhase.Ended)
{
moveAllowed = false;
Debug.Log ("3");
}
}
}
}
That is my code and in the console I get "0" and "3". can anyone help the problem may not even be with the camera I don't know. Also I am a beginner so If you can try to dumb it down for me please.
Your problem is not with the camera -- if Camera.main were null, your code would crash at line 24 and you'd never see your 0 or 3 logged to the console. I would add more informative logging, like log the collider instead of 0 and the touch states etc.
I changed "0" to "touched" and I changed "1" to "moveallowed" I changed "2" to "moving1" (I added the 1 so it is easier to search) and I changed "3" to "donemoving". however I still only get touched and donemoving.
That's not what I said, you need more informative logging, like logging the collider object, the touch state etc, their values, not just some number or text you have written. You could also use a debugger to inspect more what's going on.
Your answer
Follow this Question
Related Questions
UI button adding existing scrip to move 2d character 0 Answers
My dragging script "Sucks" all the other objects when touching it 1 Answer
Camera "Don't Clear" flag doesn't work on Android ? 1 Answer
Limit camera RotateAround for an UAV game 0 Answers
Changing Camera Background Bug (Gear VR) 0 Answers