Hi guys,
I’m having a bit of trouble writing a script to bring a paper toss style throwing mechanism into my game, but I’m having trouble with the camera perspective.
Basically, I want to move the object round the screen and it not be affected by the perspective, so its kinda of 2D, but then when it is thrown by the player, its affected again.
So far, I’ve been working with the idea that there are 2 camera’s in the world, one for the main scene, and one for the projectile. The projectile is created upon touching the screen on its own layer, and the projectile camera’s culling mask is set to that layer only. As I move the projectile around the screen, rather than moving the camera, I leave both camera and projectile at 0,0,0, but move the viewport, which gives me the effect to a degree, but while the viewport is moving, the projectile is flickering constantly.
Is this the right way to do it? What are the alternatives?
Here’s my code so far:
public class Player : MonoBehaviour
{
public GameObject projectilePrefab;
public GameObject projectileCamPrefab;
public int maxProjectiles = 3;
#region Private
private ArrayList projectileCamera;
private GameObject[] projectiles;
private int projectilesThrown = 0;
private Vector3 startPos;
private Vector3 pos;
#endregion
void Start ()
{
projectilesThrown = 0;
projectileCamera = new ArrayList ();
for (int i=0; i < maxProjectiles; i++) {
GameObject cam = Object.Instantiate (projectileCamPrefab, Vector3.zero, Quaternion.identity) as GameObject;
cam.transform.parent = transform;
cam.transform.position = new Vector3(0,0,0);
cam.camera.cullingMask = (1 << LayerMask.NameToLayer("Projectile1"));
projectileCamera.Add (cam);
}
}
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
Camera curCam = ((GameObject)projectileCamera [projectilesThrown]).camera;
pos = curCam.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, curCam.nearClipPlane));
var theProjectile = (GameObject)Object.Instantiate (projectilePrefab, new Vector3 (0,0, -3), Quaternion.Euler (new Vector3 (75, 0, 0)));
theProjectile.layer = LayerMask.NameToLayer("Projectile1");
theProjectile.rigidbody.isKinematic = true;
} else if (Input.GetMouseButton (0)) {
Camera curCam = ((GameObject)projectileCamera [projectilesThrown]).camera;
pos = curCam.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, curCam.nearClipPlane));
}
}
void LateUpdate()
{
Camera curCam = ((GameObject)projectileCamera [projectilesThrown]).camera;
OffsetVanishingPoint.SetVanishingPoint (curCam, pos);
}
}