If player hits certain X value in opposition to Camera, shift camera question.
As a beginner i encounter something i am unable to figure out.
I thought i would be able to put the player object onder the Maincamera (Player_Cam) Than i have movement on the camera behavior script attacked to the camera. I set speed and a serialized field private gameobject called _playere. And put a sprite on it.
I want initially, to have the player under Camera walk to it's transform on X from starting -12f to -3.5f. After that set the _playerCenterStage to true and left to false. Now i want the player to still move, but now it's camera moving along with the player. so now i want the opposite effectright instead of running, to run left. If i am on the left side of the screen the player should not move but the camera should (as of the player was walking.) ...............
Come to think of it this is silly while i type it, the player would be held walking later on in place.
But i simply want the camera to follolw the player, only if player is in the center state. When he is left and walking right, move player but camera should stay still, intill centerstage is hit When player is on center and moves left, center stage camera shifts so the plater will go in the rightside of the screen, andwhen player walks right again, stop camera untill he is center stage again.
What would be my best route?
Code so far that i need to redo.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera_Behavior : MonoBehaviour {
[SerializeField]
private GameObject _player;
[SerializeField]
private float _speed = 3f;
private bool _playerCenterStage = false;
private bool _playerLeftStage = true;
private bool _playerRightStage = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
Movement();
}
private void Movement()
{
float HorizontalInput = Input.GetAxis("Horizontal");
//float VerticalInput = Input.GetAxis("Verticle");
if (_player.transform.position.x <= -12f || _playerLeftStage == true)
{
_player.transform.Translate(Vector3.right * HorizontalInput * _speed * Time.deltaTime); //move
_player.transform.position = new Vector3(-12f, _player.transform.position.y, _player.transform.position.z); //move no further left
_playerLeftStage = true;
if (_player.transform.position.x == -3.5f)
{
_player.transform.position = new Vector3(-3.5f, transform.position.y, transform.position.z);
_playerCenterStage = true;
_playerLeftStage = false;
_playerRightStage = false;
}
if (_player.transform.position.x == -3.5f || _playerCenterStage == true)
{
gameObject.transform.Translate(Vector3.right * HorizontalInput * _speed * Time.deltaTime);
}
}
}
}
and now as well for some odd reason whenever i start my game the _player objects shifts from the default -12F on x to -16 on the X and some even on the Y, while i did tell it to.
Your answer
Follow this Question
Related Questions
cinemachine 2d look-ahead problem in respawn 2 Answers
How to make camera follow player from point to point 2 Answers
Camera flickers while chasing the player 0 Answers
C# Script On Camera Hides Player? 1 Answer
Mouvement issue with mouse input. 0 Answers