0
\$\begingroup\$

When a character collides with an object in my game, there is a slow motion effect. That's coool, but how would I zoom into a sprite when it collides with another object?

Here is my code currently...

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

public class GameManager : MonoBehaviour
{
    public float slowness = 2f;

    public int index;

    private AudioSource hitSource;

    public Vector3[] Target;
    public Camera Cam;
    public float Speed = 0.012f;


    public void Start()
    {
        Cam = Camera.main;

    }

    public void EndGame()
    {
        Application.targetFrameRate = 300;

        StartCoroutine(RestartLevel());

        hitSource = GetComponent<AudioSource>();

    }


    IEnumerator RestartLevel()
    {
        Cam.orthographicSize = Mathf.Lerp(Cam.orthographicSize, 3, Speed);
        Cam.transform.position = Vector3.Lerp(Cam.transform.position, Target[1], Speed);

        Time.timeScale = 1f / slowness;

        Time.fixedDeltaTime = Time.fixedDeltaTime / slowness;

           yield return new WaitForSeconds(1.5f/slowness);

        Time.timeScale = 1f;
        Time.fixedDeltaTime = Time.fixedDeltaTime * slowness;



        SceneManager.LoadScene(2);

    }

    private void OnCollisionEnter(Collision collision)
    {


    }
}
\$\endgroup\$
0
\$\begingroup\$

You need to 'interpolate' (commonly shortened to 'interp') your 'camera position' towards the object.

Edit

You have correctly identified a collision response event.

However, the name of your class seems to suggest that you are attempting to deal with this collision outside of either the player or the sprite that you have collided with.

  1. Step 1 is to put your collision response inside your player object. Your player should be responsible for handling its own collision - your game manager should not be responsible for handling something else's collision, make sense?
  2. You need to interp your camera position towards the sprite if it has been collided with

To do this:

  1. Calculate the vector from your camera to the collided sprite -
    Vector = CollidedSprite.WorldLocation - Camera.WorldLocation
  2. Each frame (tick), adjust the camera's position a fraction of this vector e.g. Camera.WorldLocation += Vector / 10
  3. It stands to reason that after 10 frames in this example, the camera will have travelled 10 1/10ths of the way towards the sprite's location. You likely don't want it to travel all of the way, so you'll probably want to move the camera by 50-75% of the calculated vector. Make sure that when you have travelled the required amount, you halt whatever repeated process is causing this camera position change.
\$\endgroup\$
12
  • \$\begingroup\$ @UsmaanMahmood If you want help adding features to your code, then you should include that code in your question. \$\endgroup\$ – DMGregory Mar 16 '20 at 16:19
  • \$\begingroup\$ Please refer above. \$\endgroup\$ – Usmaan Mahmood Mar 16 '20 at 16:31
  • \$\begingroup\$ "interpolate" is commonly shortened to "lerp", not "interp". \$\endgroup\$ – Evorlor Mar 16 '20 at 17:09
  • \$\begingroup\$ @Evorlor 'Lerp' is a shortening of 'Linear Interpolation'. 'Interp' is a shortening of 'Interpolate' This is clearly a beginner question, so regardless of which method they use, Linear or otherwise, they will encounter shortenings to 'Interp'. If you require evidence of Interp being a common shortening, please look at various Unreal Blueprint nodes using the term Interp. \$\endgroup\$ – Natalo77 Mar 16 '20 at 17:18
  • \$\begingroup\$ @UsmaanMahmood I'll get back to your query tomorrow when I have the time \$\endgroup\$ – Natalo77 Mar 16 '20 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.