Hello Guest

Author Topic: How to move a tk2dcamera with C# script  (Read 4918 times)

Mmarzex

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
How to move a tk2dcamera with C# script
« on: August 12, 2013, 06:43:54 pm »
I'm wondering how I would move the tk2dcamera with input, I was trying this code below but it doesn't seem to work.

Code: [Select]
using UnityEngine;
using System.Collections;

public class GameScript : MonoBehaviour {

public tk2dCamera Cam;
public float moveX;
// Use this for initialization
void Start () {
Debug.Log("HERET");
moveX = 0;
}

// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
Debug.Log("HERE");
moveX += 20;
//Cam.viewportRegion = new Vector4(moveX, 0, 100, 100);

//Camera.current.transform.Translate(new Vector3(moveX, 0, 100));
}
}
}

I'm not sure what to do to make it to work. Anyone got ideas?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to move a tk2dcamera with C# script
« Reply #1 on: August 12, 2013, 06:53:05 pm »
The camera attached to the current behaviour is accessed using the camera accessor. But you don't want the camera, you want the transform.
So just
transform.Translate(...) will work.


Mmarzex

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: How to move a tk2dcamera with C# script
« Reply #2 on: August 12, 2013, 08:34:33 pm »
Okay thanks. Also I can't seem to get my script to execute when attached to an empty tk2d game object. Is there something special I have to do to make it execute?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to move a tk2dcamera with C# script
« Reply #3 on: August 12, 2013, 10:47:24 pm »
No, but it'll be moving the game object its attached to.

You should probably read up a bit about how components work in Unity - this is probably one of the most important concepts in Unity.
http://docs.unity3d.com/Documentation/Manual/Scripting42.html
It'll all make a lot more sense then.

Mmarzex

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: How to move a tk2dcamera with C# script
« Reply #4 on: August 12, 2013, 10:50:28 pm »
I have the script attached to an empty game object to move the camera, but it doesn't even execute the start function, which it should. I have a debug log set to fire when start is run but it doesn't. I don't understand why that is happening, it's never been an issue for me in a unity project before.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to move a tk2dcamera with C# script
« Reply #5 on: August 12, 2013, 11:38:57 pm »
No idea what you can do to break this.
If its an empty GameObject then you should get Start called on any attached Monobehaviours. I suggest asking in the Unity answers page.

gary-unikronsoftware

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 74
    • View Profile
Re: How to move a tk2dcamera with C# script
« Reply #6 on: August 13, 2013, 09:50:14 am »
Hi,

I have just tried a variation of your script and it works fine...

Code: [Select]
using UnityEngine;
using System.Collections;

public class GameScript : MonoBehaviour
{
public tk2dCamera Cam;
public float moveX;

void Start ()
{
Debug.Log("HERET");
moveX = 0;
}

void Update ()
{
if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
Debug.Log("HERE");
moveX += 20;

Vector3 pos = Cam.transform.position;

pos.x += moveX;

Cam.transform.position = pos;
}
}
}

Can I suggest a few things to check...

- Have you also added an Awake() function to the script and does this get called and execute correctly?  If this function breaks it would stop the Start and Update functions getting called.
-  Do you get any errors when the code compiles or runs?
-  Did you remember to drag the tk2dCamera object from Hierarchy into the script's Cam slot?

If all of that is okay, I'm at a loss at what more to suggest.

gary-unikronsoftware

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 74
    • View Profile
Re: How to move a tk2dcamera with C# script
« Reply #7 on: August 13, 2013, 10:01:13 am »
Oh, one other thing - do you have debug logs turned off so any errors aren't being shown?  (The buttons on the top-right of the console window toggles errors and warning messages).