0
\$\begingroup\$

I am creating an 2d platformer and I am newbie to Unity; any help will be highly appreciated. I want the camera to be positioned on the player and also the X axis should move continuously by .05f. And if player is not moving or stuck somewhere and if player is behind camera player (because X axis is automatically moving by .05f) then player should die.

Is it doable? I am using Cinemachine and any script logic will be appreciated.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ It looks to me like there are plenty of existing tutorials about setting up an auto-scroller camera like this in Unity. What have you tried based on your research so far? Did you have any specific trouble following one of these guides? \$\endgroup\$
    – DMGregory
    Jan 29, 2021 at 18:24
  • \$\begingroup\$ @DMGregory♦ Thanks for the time. I was able to find the solution. My mistake was my camera was following player. Now I created a gameobject and added camera follow to this and attached transform position script to it. \$\endgroup\$ Jan 30, 2021 at 14:01
  • \$\begingroup\$ Do you want to post an Answer below detailing the steps of your solution? Or would you rather delete this question if it's no longer needed? \$\endgroup\$
    – DMGregory
    Jan 30, 2021 at 14:02

1 Answer 1

0
\$\begingroup\$
  1. Create a gameObject and attached cinemachine to this object

  2. Attached script to this , FollowObject is the player

    public GameObject FollowObject;
    private Rigidbody2D rb;
    
    private void Start()
    {
        rb = FollowObject.GetComponent<Rigidbody2D>(); 
    }
    
    private void FixedUpdate(){     
        float MoveSpeed = rb.velocity.magnitude > Speed ? rb.velocity.magnitude : Speed;
        transform.position += Vector3.right * MoveSpeed * Time.deltaTime;
    }
    
  3. Player script, if player is not visible by main camera he dies(currently reloading the screen)

     void Update()
     {
         var planes = GeometryUtility.CalculateFrustumPlanes(Camera.main);
         var isvisible = GeometryUtility.TestPlanesAABB(planes, collider2D.bounds);
         if (!isvisible)
         {
             SceneManager.LoadScene("Gamestart4");
         }        
     }
    

PS: Suggestions for improvement is accepted

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .