I have got a camera object and a character object. I want them to keep up with each other (like flappy bird , both objects move automatically to the right and stuff. The problem is that I cant also change the position of the character due to collisions. Is there any way of setting the camera’s velocity or just making it scroll with the same speed the character does?
public class CameraMover : MonoBehaviour {
CharController ctr;
public float offset = 500;
// Use this for initialization
void Start () {
ctr = GameObject.Find ("Character").GetComponent<CharController>();
}
// Update is called once per frame
void Update () {
gameObject.transform.position = new Vector2(gameObject.transform.position.x + ctr.speed , gameObject.transform.position.y);
if (gameObject.transform.position.x > (ctr.gameObject.transform.position.x + offset)) {//if camera didnt keep up with player
//todo - gameover
}
}
}
public class CharController : MonoBehaviour {
public float speed = 1f;//this one is the problematic value - maybe
public float jumpForce = 500;
public bool grounded = true;
void Start () {
}
void Update(){
checkGrounded ();
}
void FixedUpdate () {
//float move = Input.GetAxis("Horizontal");
rigidbody2D.transform.position = new Vector2 (speed , rigidbody2D.velocity.y);
if(Input.GetKeyDown(KeyCode.Space)){
if(grounded){
rigidbody2D.AddForce(Vector2.up * jumpForce);
}
}
}
public void checkGrounded(){
LayerMask mask = LayerMask.NameToLayer("Walls");
//Debug.DrawRay (transform.position, -Vector2.up, Color.green);
RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up , 0.1f , mask.value);
if(hit.collider != null){
grounded = true;
return;
}
grounded = false;
}
}