- Home /
Move the camera
I'm working on a code, if playercontroller = false...then you can move the camera.
but the camera won't work =/
here's the code:
using UnityEngine; using System.Collections;
public class Cammouse : MonoBehaviour {
public float Cameraspeed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Player.Playercontroller == false)
{
float camMoveH = Input.GetAxis("Horizontal") * Cameraspeed * Time.deltaTime;
transform.Translate(Vector3.right * camMoveH);
float camMoveV = Input.GetAxis("Vertical") * Cameraspeed * Time.deltaTime;
transform.Translate(Vector3.down * camMoveV);
}
//transform.position = new Vector3(Input.mousePosition.x,Input.mousePosition.z);
}
}
and this is the Player code were I made the bool for Playercontroller:
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
private float TimeDifference;
public static int Health = 100;
public float Playerspeed;
public static bool Playercontroller = false;
public static Player Instance;
// Use this for initialization
void Awake ()
{
Instance=this;
}
// Update is called once per frame
void Update () {
if (Playercontroller == true)
{
float amtToMoveH = Input.GetAxis("Horizontal") * Playerspeed * Time.deltaTime;
transform.Translate(Vector3.right * amtToMoveH);
float amtToMoveV = Input.GetAxis("Vertical") * Playerspeed * Time.deltaTime;
transform.Translate(Vector3.forward * amtToMoveV);
}
====( http://www.clothes6.us )=====
online store wholesale sneakers,Jerseys, jewelry, glasses, shirt, sports,handbags,clothes ,news, vogue,jeryse at ugg boots,luxury fashion ysl women boots, christian louboutin boots,ed hardy clothes, jordan shoes,nike shoes,hoodies,t-shirts,nfl jerseys,mlb jerseys,nhl jerseys,coach handbags,handbags,wholesale, retail, sunglasses,belts, caps, ed hardy caps,suit,fashion good,newest style goods All the products are free shipping,
====( http://www.clothes6.us )=====
free shipping
competitive price
any size available
accept the paypal
jordan shoes $32
nike shox $32
$$anonymous$$BT shoes 48
NFL jersys 24
NBA jersys 24
Timberland boots 45
Christan Audigier bikini $20
Ed Hardy Bikini $23
Smful short_t-shirt_woman $14
ed hardy short_tank_woman $15
Sandal $26
christian louboutin $60
Sunglass $14
COACH_Necklace $18
handbag $33
AF tank woman $16
====( http://www.clothes6.us )=====
Answer by syclamoth · Sep 14, 2011 at 03:26 AM
The problem is that you are accessing the Player as a static class! it would be simpler and generally easier to debug if you just put a reference to the player in your camera script:
public Player player;
if you put this at the top, you can assign the Player in the unity editor, and then it will work for you. Just use the non-capitalised player instance reference instead of the capitalised Player class reference!
so I should remove the static from the bool, write down public Player player in the upper part of the cam script and change the name of the class reference in the if statement?
Yep! After you do that, remember to assign the player in the editor, otherwise you'll just get null reference exceptions.
Just out of interest, are you instantiating the player, or is it in the scene from startup?
well, now I can move the camera...but lost control of the player =/
Isn't that what you wanted? That you can either control the camera, or the player?
Answer by japanitrat · Sep 13, 2011 at 10:08 AM
you are missing a part of your script, namely the one where "Player" and "Playercontroller" is defined. I guess though, that Player.Playercontroller is an instance of something, so you need to check against null
and not false
:
if(Player.Playercontroller == null)
{
// ...
}
well, Playercontroller is a bool I set in the Player script to "true".
and if if this bool is true, I can control the player object.
I tried to change false to "null", but it seems to make the scripts unreachable =/
Here's the player code:
using UnityEngine; using System.Collections;
public class Player : $$anonymous$$onoBehaviour {
private float TimeDifference;
public static int Health = 100;
public float Playerspeed;
public static bool Playercontroller = false;
public static Player Instance;
// Use this for initialization
void Awake ()
{
Instance=this;
}
// Update is called once per frame
void Update () {
if (Playercontroller == true)
{
float amtTo$$anonymous$$oveH = Input.GetAxis("Horizontal") * Playerspeed * Time.deltaTime;
transform.Translate(Vector3.right * amtTo$$anonymous$$oveH);
float amtTo$$anonymous$$oveV = Input.GetAxis("Vertical") * Playerspeed * Time.deltaTime;
transform.Translate(Vector3.forward * amtTo$$anonymous$$oveV);
}
Please Edit your original question and pull the code from here to there as it makes it easier to read. Also please format the code correctly (just select all code in the text and hit the button with the zeros and ones above the textarea
Your answer
Follow this Question
Related Questions
Rotate cam with object, smooth boost and brake 3 Answers
MoveTowards problem 1 Answer
show a camera in one monitor and another camera in another monitor 1 Answer
Make Arms Move Slow 0 Answers
Stop Motion Camera Effect 0 Answers