Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Move different object on the same axis in correlation to another object's axis

Discussion in 'Scripting' started by giovij50, May 12, 2022.

  1. giovij50

    giovij50

    Joined:
    May 6, 2022
    Posts:
    9
    Hello, I have a problem: I was making a game where you can switch between a 2D view and a Top-Down view (GTA 1-2 style)
    To do so, I created 2 models with as many controller scripts and Cameras which are activated and de-activated pressing a key.
    The Cameras are all setted, the characters moves well, but when I press the switch key, the second character reactives and remains where I put it in the editor, while it had to be moved along the X axis in parallel to the first character.
    I tried many different solutions, but I havent find a solution and this project must be delivered by 20th May.
    Can somebody help me? I'm completely desperated
     
  2. robotscott725

    robotscott725

    Joined:
    Nov 4, 2020
    Posts:
    23
    You could set the hidden gameobject's position to the normal one when you switch the camera.
     
  3. giovij50

    giovij50

    Joined:
    May 6, 2022
    Posts:
    9
    What do you mean?
     
  4. robotscott725

    robotscott725

    Joined:
    Nov 4, 2020
    Posts:
    23
    In your script, the one that switches the camera, set the hidden character's transform.position to the normal character's transform.position. for example:


    public class ExampleScript : MonoBehaviour
    {
    public GameObject character1;
    public GameObject character2;

    void OnCameraSwitch()
    {
    character2.transform.position = character1.transform.position;
    }
    }


    Do something like this in your camera switching method.
     
    Last edited: May 12, 2022
  5. giovij50

    giovij50

    Joined:
    May 6, 2022
    Posts:
    9
    The Camera works, they follow the player when it moves, my problem is that the character needs to move when it is reactivated

    Also, I need the players to be only on the same X-axis when they switch

    If you want, I write you the code when I can use the PC
     
  6. robotscott725

    robotscott725

    Joined:
    Nov 4, 2020
    Posts:
    23
    Okay, if you could post your script Ill do it.
     
  7. giovij50

    giovij50

    Joined:
    May 6, 2022
    Posts:
    9
    This is the 3D Player Controller


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.InputSystem;
    using UnityEngine.SceneManagement;
    using UnityEngine.UI;
    using TMPro;
    using System;

    public class AnimationAndMovementController : MonoBehaviour
    {
    PlayerInput3 playerInput;
    CharacterController characterController;
    Animator animator;

    int isWalkingHash;
    int isRunningHash;

    bool win = false;


    Vector2 currentMovementInput;
    Vector3 currentMovement;
    Vector3 currentRunMovement;
    bool isMovementPressed;
    bool isRunPressed;
    float rotationFactorPerFrame = 15f;
    float runMultiplier = 3.0f;

    float savedPos;
    public string nextLevel;
    public string currentLevel;
    public AnimationAndMovementController Player3D;
    public AnimationAndMovementController2 Player2D;

    void Awake()
    {
    playerInput = new PlayerInput3();
    characterController = GetComponent<CharacterController>();
    animator = GetComponent<Animator>();

    isWalkingHash = Animator.StringToHash("isWalking");
    isRunningHash = Animator.StringToHash("isRunning");

    playerInput.CharacterControls.Move.started += onMovementInput;
    playerInput.CharacterControls.Move.canceled += onMovementInput;
    playerInput.CharacterControls.Move.performed += onMovementInput;
    playerInput.CharacterControls.Run.started += onRun;
    playerInput.CharacterControls.Run.canceled += onRun;
    }

    void onRun(InputAction.CallbackContext context)
    {
    isRunPressed = context.ReadValueAsButton();
    }

    void handleRotation()
    {
    Vector3 positionToLookAt;

    positionToLookAt.x = currentMovement.x;
    positionToLookAt.y = 0.0f;
    positionToLookAt.z = currentMovement.z;

    Quaternion currentRotation = transform.rotation;

    if (isMovementPressed)
    {
    Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt);
    transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * Time.deltaTime);
    }
    }

    void onMovementInput(InputAction.CallbackContext context)
    {
    currentMovementInput = context.ReadValue<Vector2>();
    currentMovement.x = currentMovementInput.x;
    currentMovement.z = currentMovementInput.y;
    currentRunMovement.x = currentMovementInput.x * runMultiplier;
    currentRunMovement.z = currentMovementInput.y * runMultiplier;
    isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
    }

    void handleAnimation()
    {
    bool isWalking = animator.GetBool(isWalkingHash);
    bool isRunning = animator.GetBool(isRunningHash);

    if (isMovementPressed && !isWalking)
    {
    animator.SetBool(isWalkingHash, true);
    }

    else if (!isMovementPressed && isWalking)
    {
    animator.SetBool(isWalkingHash, false);
    }

    if ((isMovementPressed && isRunPressed) && !isRunning)
    {
    animator.SetBool(isRunningHash, true);
    }

    else if ((!isMovementPressed || !isRunPressed) && isRunning)
    {
    animator.SetBool(isRunningHash, false);
    }


    }

    void handleGravity()
    {
    bool isFalling = currentMovement.y <= 0.0f;
    if (characterController.isGrounded)
    {
    float groundedGravity = -.05f;
    currentMovement.y = groundedGravity;
    currentRunMovement.y = groundedGravity;
    }
    else if(isFalling) {
    float gravity = -9.8f;

    float fallMultiplier = 2.0f;
    float previousYVelocity = currentMovement.y;
    float NewYVelocity = currentMovement.y + (gravity * fallMultiplier * Time.deltaTime);
    float NextYVelocity = Mathf.Max((previousYVelocity + NewYVelocity) * .5f, -20.0f);
    currentMovement.y = NextYVelocity;
    currentRunMovement.y = NextYVelocity;
    }
    }

    void Update()
    {
    handleGravity();
    handleRotation();
    handleAnimation();

    if (isRunPressed)
    {
    characterController.Move(currentRunMovement * Time.deltaTime);
    }
    else
    {
    characterController.Move(currentMovement * Time.deltaTime);
    }

    if (transform.position.y < -5)
    {
    SceneManager.LoadScene(currentLevel);
    }

    if (Input.GetKeyDown(KeyCode.Space))
    {


    Debug.Log("Step 1");

    Player2D.gameObject.SetActive(true);

    savedPos = Player2D.transform.position.z;

    Debug.Log("Step 2");

    Player2D.transform.position = transform.position + new Vector3(0, 1, 0);
    Player2D.transform.position = new Vector3(Player2D.transform.position.x, transform.position.y, savedPos);

    Debug.Log("Step 3");


    Player3D.gameObject.SetActive(false);
    }
    }

    void OnEnable()
    {
    playerInput.CharacterControls.Enable();
    }

    void OnDisable()
    {
    playerInput.CharacterControls.Disable();
    }

    void OnTriggerEnter(Collider other)
    {

    if (other.CompareTag("Finish"))
    {
    win = true;

    if (win == true)
    {
    SceneManager.LoadScene(nextLevel);
    }
    }
    }
    }


    This is the 2D Player Controller


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.InputSystem;
    using UnityEngine.SceneManagement;
    using UnityEngine.UI;
    using TMPro;
    using System;

    public class AnimationAndMovementController2 : MonoBehaviour
    {
    PlayerInput2 playerInput;
    CharacterController characterController;
    Animator animator;

    int isWalkingHash;
    int isRunningHash;

    Vector2 currentMovementInput;
    Vector3 currentMovement;
    Vector3 currentRunMovement;
    bool isMovementPressed;
    bool isRunPressed;

    public string nextLevel;
    public string currentLevel;
    public AnimationAndMovementController Player3D;
    public AnimationAndMovementController2 Player2D;

    float rotationFactorPerFrame = 15f;
    float runMultiplier = 4.0f;
    float gravity = -9.8f;
    float groundedGravity = -.05f;

    Vector3 savedPos;
    bool isJumpPressed = false;
    float initialJumpVelocity;
    float maxJumpHeight = 4.0f;
    float maxJumpTime = 0.75f;
    bool isJumping = false;
    int isJumpingHash;
    int jumpCountHash;
    bool isJumpAnimating = false;
    int jumpCount = 0;
    Dictionary<int, float> initialJumpVelocities = new Dictionary<int, float>();
    Dictionary<int, float> JumpGravities = new Dictionary<int, float>();
    Coroutine currentJumpResetRoutine = null;

    bool win = false;

    void Awake()
    {
    playerInput = new PlayerInput2();
    characterController = GetComponent<CharacterController>();
    animator = GetComponent<Animator>();

    isWalkingHash = Animator.StringToHash("isWalking");
    isRunningHash = Animator.StringToHash("isRunning");
    isJumpingHash = Animator.StringToHash("isJumping");
    jumpCountHash = Animator.StringToHash("jumpCount");

    playerInput.CharacterControls.Move.started += onMovementInput;
    playerInput.CharacterControls.Move.canceled += onMovementInput;
    playerInput.CharacterControls.Move.performed += onMovementInput;
    playerInput.CharacterControls.Run.started += onRun;
    playerInput.CharacterControls.Run.canceled += onRun;
    playerInput.CharacterControls.Jump.started += onJump;
    playerInput.CharacterControls.Jump.canceled += onJump;

    setupJumpVariables();
    }

    void Start()
    {

    }

    void setupJumpVariables ()
    {
    float timeToApex = maxJumpTime / 2;
    gravity = (-2 * maxJumpHeight) / Mathf.Pow(timeToApex, 2);
    initialJumpVelocity = (2 * maxJumpHeight) / timeToApex;
    float secondJumpGravity = (-2 * (maxJumpHeight + 2)) / Mathf.Pow((timeToApex * 1.25f), 2);
    float secondJumpInitialVelocity = (2 * (maxJumpHeight + 2)) / (timeToApex * 1.25f);
    float thirdJumpGravity = (-2 * (maxJumpHeight + 4)) / Mathf.Pow((timeToApex * 1.5f), 2);
    float thirdJumpInitialVelocity = (2 * (maxJumpHeight + 4)) / (timeToApex * 1.5f);

    initialJumpVelocities.Add(1, initialJumpVelocity);
    initialJumpVelocities.Add(2, secondJumpInitialVelocity);
    initialJumpVelocities.Add(3, thirdJumpInitialVelocity);

    JumpGravities.Add(0, gravity);
    JumpGravities.Add(1, gravity);
    JumpGravities.Add(2, secondJumpGravity);
    JumpGravities.Add(3, thirdJumpGravity);
    }

    void handleJump()
    {
    if(!isJumping && characterController.isGrounded && isJumpPressed)
    { if (jumpCount < 3 && currentJumpResetRoutine != null)
    {
    StopCoroutine(currentJumpResetRoutine);
    }
    animator.SetBool(isJumpingHash, true);
    isJumpAnimating = true;
    isJumping = true;
    jumpCount += 1;
    animator.SetInteger(jumpCountHash, jumpCount);
    currentMovement.y = initialJumpVelocities[jumpCount] * .5f;
    currentRunMovement.y = initialJumpVelocities[jumpCount] * .5f;
    } else if (!isJumpPressed && isJumping && characterController.isGrounded) {
    isJumping = false;
    }
    }

    IEnumerator jumpResetRoutine()
    {
    yield return new WaitForSeconds(.5f);
    jumpCount = 0;
    }

    void onJump (InputAction.CallbackContext context)
    {
    isJumpPressed = context.ReadValueAsButton();
    }

    void onRun(InputAction.CallbackContext context)
    {
    isRunPressed = context.ReadValueAsButton();
    }

    void handleRotation()
    {
    Vector3 positionToLookAt;

    positionToLookAt.x = currentMovement.x;
    positionToLookAt.y = 0.0f;
    positionToLookAt.z = currentMovement.z;

    Quaternion currentRotation = transform.rotation;

    if (isMovementPressed)
    {
    Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt);
    transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * Time.deltaTime);
    }
    }

    void onMovementInput(InputAction.CallbackContext context)
    {
    currentMovementInput = context.ReadValue<Vector2>();
    currentMovement.x = currentMovementInput.x * 1.5f;
    currentRunMovement.x = currentMovementInput.x * runMultiplier;
    isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
    }

    void handleAnimation()
    {
    bool isWalking = animator.GetBool(isWalkingHash);
    bool isRunning = animator.GetBool(isRunningHash);

    if (isMovementPressed && !isWalking)
    {
    animator.SetBool(isWalkingHash, true);
    }

    else if (!isMovementPressed && isWalking)
    {
    animator.SetBool(isWalkingHash, false);
    }

    if ((isMovementPressed && isRunPressed) && !isRunning)
    {
    animator.SetBool(isRunningHash, true);
    }

    else if ((!isMovementPressed || !isRunPressed) && isRunning)
    {
    animator.SetBool(isRunningHash, false);
    }


    }

    void handleGravity()
    {
    bool isFalling = currentMovement.y <= 0.0f || !isJumpPressed;
    float fallMultiplier = 2.0f;

    if (characterController.isGrounded)
    { if (isJumpAnimating)
    {
    animator.SetBool(isJumpingHash, false);
    isJumpAnimating = false;
    currentJumpResetRoutine = StartCoroutine(jumpResetRoutine());
    if (jumpCount == 3)
    {
    jumpCount = 0;
    animator.SetInteger(jumpCountHash, jumpCount);
    }
    }
    currentMovement.y = groundedGravity;
    currentRunMovement.y = groundedGravity;
    }
    else if (isFalling)
    {
    float previousYVelocity = currentMovement.y;
    float NewYVelocity = currentMovement.y + (JumpGravities[jumpCount] * fallMultiplier * Time.deltaTime);
    float NextYVelocity = Mathf.Max((previousYVelocity + NewYVelocity) * .5f, -20.0f);
    currentMovement.y = NextYVelocity;
    currentRunMovement.y = NextYVelocity;
    }
    else
    {
    float previousYVelocity = currentMovement.y;
    float NewYVelocity = currentMovement.y + (JumpGravities[jumpCount] * Time.deltaTime);
    float NextYVelocity = (previousYVelocity + NewYVelocity) * .5f;
    currentMovement.y = NextYVelocity;
    currentRunMovement.y = NextYVelocity;
    }
    }

    void Update()
    {
    handleRotation();
    handleAnimation();

    if (isRunPressed)
    {
    characterController.Move(currentRunMovement * Time.deltaTime);
    }
    else
    {
    characterController.Move(currentMovement * Time.deltaTime);
    }

    if (transform.position.y < -5)
    {
    SceneManager.LoadScene(currentLevel);
    }

    if (Input.GetKeyDown(KeyCode.Space))
    {


    Debug.Log("Step 1");

    Player3D.gameObject.SetActive(true);



    Debug.Log("Step 2");

    Player3D.transform.position = transform.position - new Vector3(0, 1, -2.66f);

    Debug.Log("Step 3");

    Player2D.gameObject.SetActive(false);
    }

    handleGravity();
    handleJump();
    }

    void OnEnable()
    {
    playerInput.CharacterControls.Enable();
    }

    void OnDisable()
    {
    playerInput.CharacterControls.Disable();
    }

    void OnTriggerEnter(Collider other)
    {

    if (other.CompareTag("Finish"))
    {
    win = true;

    if (win == true)
    {
    SceneManager.LoadScene(nextLevel);
    }
    }
    }



    }


    This is the 2D Camera Controller


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Cameracontroller1 : MonoBehaviour
    {
    public AnimationAndMovementController2 Player2D;
    public Cameracontroller Camera3D;
    public Cameracontroller1 Camera2D;
    Vector3 offset;

    // Start is called before the first frame update
    void Start()
    {
    offset.z = transform.position.z - Player2D.transform.position.z;
    }

    // Update is called once per frame
    void Update()
    {
    if (Player2D != null)
    {
    transform.position = Player2D.transform.position + offset;
    }

    if (Input.GetKeyDown(KeyCode.Space))
    {
    Camera3D.gameObject.SetActive(true);
    Camera2D.gameObject.SetActive(false);
    }
    }
    }




    And this is the 3D Camera Controller


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Cameracontroller : MonoBehaviour
    {
    public AnimationAndMovementController Player3D;
    public Cameracontroller Camera3D;
    public Cameracontroller1 Camera2D;
    Vector3 offset;

    // Start is called before the first frame update
    void Start()
    {
    offset.y = transform.position.y;
    }

    // Update is called once per frame
    void Update()
    {
    if (Player3D!= null) {
    transform.position = Player3D.transform.position + offset;
    }

    if (Input.GetKeyDown(KeyCode.Space))
    {
    Camera2D.gameObject.SetActive(true);
    Camera3D.gameObject.SetActive(false);
    }
    }
    }




    Excuse me in advance for the length of the code
    Also, I hope this is formatted this correctly
     
  8. robotscott725

    robotscott725

    Joined:
    Nov 4, 2020
    Posts:
    23
    Okay It should make the player objects be in the same spot on switch now. Let me know if it works.
    Also, why do you add so many extra lines? Anyways;

    edit: I think all the lines are because of unity forums. as soon as I posted it there were twice as many empty lines.


    CameraController1.cs:

    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;



    public class Cameracontroller1 : MonoBehaviour

    {

    public AnimationAndMovementController2 Player2D;

    public Cameracontroller Camera3D;

    public Cameracontroller1 Camera2D;

    Vector3 offset;



    // Start is called before the first frame update

    void Start()

    {

    offset.z = transform.position.z - Player2D.transform.position.z;

    }



    // Update is called once per frame

    void Update()

    {

    if (Player2D != null)

    {

    transform.position = Player2D.transform.position + offset;

    }



    if (Input.GetKeyDown(KeyCode.Space))

    {

    Camera3D.gameObject.SetActive(true);

    Camera2D.gameObject.SetActive(false);

    Camera3D.gameObject.transform.position.x = Camera2D.gameObject.transform.position.x;

    }

    }

    }


    CameraController.cs:



    [ICODE]
    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;



    public class Cameracontroller : MonoBehaviour

    {

    public AnimationAndMovementController Player3D;

    public Cameracontroller Camera3D;

    public Cameracontroller1 Camera2D;

    Vector3 offset;



    // Start is called before the first frame update

    void Start()

    {

    offset.y = transform.position.y;

    }



    // Update is called once per frame

    void Update()

    {

    if (Player3D != null)
    {

    transform.position = Player3D.transform.position + offset;

    }



    if (Input.GetKeyDown(KeyCode.Space))

    {

    Camera2D.gameObject.SetActive(true);

    Camera3D.gameObject.SetActive(false);

    Camera2D.gameObject.transform.position.x = Camera3D.gameObject.transform.position.x;

    }

    }

    }
    [/ICODE]

    AnimationController:

    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;

    using UnityEngine.InputSystem;

    using UnityEngine.SceneManagement;

    using UnityEngine.UI;

    using TMPro;

    using System;



    public class AnimationAndMovementController : MonoBehaviour

    {

    PlayerInput3 playerInput;

    CharacterController characterController;

    Animator animator;



    int isWalkingHash;

    int isRunningHash;



    bool win = false;





    Vector2 currentMovementInput;

    Vector3 currentMovement;

    Vector3 currentRunMovement;

    bool isMovementPressed;

    bool isRunPressed;

    float rotationFactorPerFrame = 15f;

    float runMultiplier = 3.0f;



    float savedPos;

    public string nextLevel;

    public string currentLevel;

    public AnimationAndMovementController Player3D;

    public AnimationAndMovementController2 Player2D;



    void Awake()

    {

    playerInput = new PlayerInput3();

    characterController = GetComponent<CharacterController>();

    animator = GetComponent<Animator>();



    isWalkingHash = Animator.StringToHash("isWalking");

    isRunningHash = Animator.StringToHash("isRunning");



    playerInput.CharacterControls.Move.started += onMovementInput;

    playerInput.CharacterControls.Move.canceled += onMovementInput;

    playerInput.CharacterControls.Move.performed += onMovementInput;

    playerInput.CharacterControls.Run.started += onRun;

    playerInput.CharacterControls.Run.canceled += onRun;

    }



    void onRun(InputAction.CallbackContext context)

    {

    isRunPressed = context.ReadValueAsButton();

    }



    void handleRotation()

    {

    Vector3 positionToLookAt;



    positionToLookAt.x = currentMovement.x;

    positionToLookAt.y = 0.0f;

    positionToLookAt.z = currentMovement.z;



    Quaternion currentRotation = transform.rotation;



    if (isMovementPressed)

    {

    Quaternion targetRotation = Quaternion.LookRotation(positionToLookAt);

    transform.rotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * Time.deltaTime);

    }

    }



    void onMovementInput(InputAction.CallbackContext context)

    {

    currentMovementInput = context.ReadValue<Vector2>();

    currentMovement.x = currentMovementInput.x;

    currentMovement.z = currentMovementInput.y;

    currentRunMovement.x = currentMovementInput.x * runMultiplier;

    currentRunMovement.z = currentMovementInput.y * runMultiplier;

    isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;

    }



    void handleAnimation()

    {

    bool isWalking = animator.GetBool(isWalkingHash);

    bool isRunning = animator.GetBool(isRunningHash);



    if (isMovementPressed && !isWalking)

    {

    animator.SetBool(isWalkingHash, true);

    }



    else if (!isMovementPressed && isWalking)

    {

    animator.SetBool(isWalkingHash, false);

    }



    if ((isMovementPressed && isRunPressed) && !isRunning)

    {

    animator.SetBool(isRunningHash, true);

    }



    else if ((!isMovementPressed || !isRunPressed) && isRunning)

    {

    animator.SetBool(isRunningHash, false);

    }





    }



    void handleGravity()

    {

    bool isFalling = currentMovement.y <= 0.0f;

    if (characterController.isGrounded)

    {

    float groundedGravity = -.05f;

    currentMovement.y = groundedGravity;

    currentRunMovement.y = groundedGravity;

    }

    else if (isFalling)
    {

    float gravity = -9.8f;



    float fallMultiplier = 2.0f;

    float previousYVelocity = currentMovement.y;

    float NewYVelocity = currentMovement.y + (gravity * fallMultiplier * Time.deltaTime);

    float NextYVelocity = Mathf.Max((previousYVelocity + NewYVelocity) * .5f, -20.0f);

    currentMovement.y = NextYVelocity;

    currentRunMovement.y = NextYVelocity;

    }

    }



    void Update()

    {

    handleGravity();

    handleRotation();

    handleAnimation();



    if (isRunPressed)

    {

    characterController.Move(currentRunMovement * Time.deltaTime);

    }

    else

    {

    characterController.Move(currentMovement * Time.deltaTime);

    }



    if (transform.position.y < -5)

    {

    SceneManager.LoadScene(currentLevel);

    }



    if (Input.GetKeyDown(KeyCode.Space))

    {





    Debug.Log("Step 1");



    Player2D.gameObject.SetActive(true);



    savedPos = Player2D.transform.position.z;



    Debug.Log("Step 2");



    Player2D.transform.position = transform.position + new Vector3(0, 1, 0);

    Player2D.transform.position = new Vector3(Player2D.transform.position.x, transform.position.y, savedPos);



    Debug.Log("Step 3");





    Player3D.gameObject.SetActive(false);

    }

    }



    void OnEnable()

    {

    playerInput.CharacterControls.Enable();

    }



    void OnDisable()

    {

    playerInput.CharacterControls.Disable();

    }



    void OnTriggerEnter(Collider other)

    {



    if (other.CompareTag("Finish"))

    {

    win = true;



    if (win == true)

    {

    SceneManager.LoadScene(nextLevel);

    }

    }

    }

    }
     
  9. giovij50

    giovij50

    Joined:
    May 6, 2022
    Posts:
    9

    When I put the codes you passed me, there is an Error CS1612
     
  10. robotscott725

    robotscott725

    Joined:
    Nov 4, 2020
    Posts:
    23
    Oh I know whats wrong... hang on a second... ill paste it in this post

    cameraController1:

    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;



    public class Cameracontroller1 : MonoBehaviour

    {

    public AnimationAndMovementController2 Player2D;

    public Cameracontroller Camera3D;

    public Cameracontroller1 Camera2D;

    Vector3 offset;



    // Start is called before the first frame update

    void Start()

    {

    offset.z = transform.position.z - Player2D.transform.position.z;

    }



    // Update is called once per frame

    void Update()

    {

    if (Player2D != null)

    {

    transform.position = Player2D.transform.position + offset;

    }



    if (Input.GetKeyDown(KeyCode.Space))

    {

    Camera3D.gameObject.SetActive(true);

    Camera2D.gameObject.SetActive(false);

    Camera3D.gameObject.transform.position.x = new Vector3(Camera2D.gameObject.transform.position.x, Camera2D.gameObject.transform.position.y, Camera2D.gameObject.transform.position.z);

    }

    }

    }


    cameraController:



    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;



    public class Cameracontroller : MonoBehaviour

    {

    public AnimationAndMovementController Player3D;

    public Cameracontroller Camera3D;

    public Cameracontroller1 Camera2D;

    Vector3 offset;



    // Start is called before the first frame update

    void Start()

    {

    offset.y = transform.position.y;

    }



    // Update is called once per frame

    void Update()

    {

    if (Player3D != null)
    {

    transform.position = Player3D.transform.position + offset;

    }



    if (Input.GetKeyDown(KeyCode.Space))

    {

    Camera2D.gameObject.SetActive(true);

    Camera3D.gameObject.SetActive(false);

    Camera2D.gameObject.transform.position.x = new Vector3(Camera3D.gameObject.transform.position.x, Camera3D.gameObject.transform.position.y, Camera3D.gameObject.transform.position.z);

    }

    }

    }


    Hopefully It'll work now. I forgot to set the transform for the objects as a Vector3.
     
    Last edited: May 12, 2022
  11. giovij50

    giovij50

    Joined:
    May 6, 2022
    Posts:
    9
    Anyway, what have you changed in the Third Controller?
     
  12. robotscott725

    robotscott725

    Joined:
    Nov 4, 2020
    Posts:
    23
    I just changed the part in the Update() method where you switch the camera. I added a line that set Camera2D to Camera3D and vice versa when space is pressed.
     
  13. giovij50

    giovij50

    Joined:
    May 6, 2022
    Posts:
    9
    It still gives me Error CS1612
     
  14. giovij50

    giovij50

    Joined:
    May 6, 2022
    Posts:
    9

    However, the problem are NOT the Cameras, the problem is the position in the X-axis of the player when I switch model
     
  15. giovij50

    giovij50

    Joined:
    May 6, 2022
    Posts:
    9

    If you want, I can give you my Discord ID so we can talk about this thread
     
  16. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Please know that you can edit your posts; you don't need to make multiple posts within minutes.

    Also, when posting code, please use code-tags.
     
  17. robotscott725

    robotscott725

    Joined:
    Nov 4, 2020
    Posts:
    23
    Sure! Its probably best so we dont add to many posts.