[SOLVED] How to bend a sprite around the perimeter of the camera screen

Updated on December 22, 2018 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on December 18, 2018

Hi,

 

I’m having difficulty achieving an effect. This isn’t a technical question, I just need a direction for how I can achieve this effect. Please use this gif as a reference.

 

https://gyazo.com/154036d2aca567ae352d121f7b83863b

 

This was created in After Effects, but it’s the stroke of a rectangle that I’m animating. I need this white line to be a sprite on its own and to have a rigid body attached to it. I don’t think this is possible within the Unity Engine itself, so I’m thinking I have to use After Effects. Yet, I’m not too familiar with after effects. If someone is familiar with After Effects and knows how I could make this ‘stroke’ animation I created into a unique sprite that is bounded only to the visible line, that would be helpful.

 

I guess, if this isn’t possible to do in Unity w/o After Effects, I’d have to reach out to an After Effects community instead. Any feedback would be appreciated to help me get a direction for this feature.

 

Thank you kindly.

  • Liked by
Reply
1 on December 18, 2018

you can use a linerenderer to create the line, then with code you can let the linerenderer move around the perimeter of the camera

on December 22, 2018

Thank-you, I was able to achieve this effect with the line render.

Show more replies
  • Liked by
Reply
Cancel
0 on December 19, 2018

Or if it’s a static effect that you want (like a loading screen), then I would create a full sprite that has all the white showing as a rounded rectangle, then only display certain portions of it at a time, based on the degrees from the center.
https://www.google.com/search?q=unity+radial+fill

This won’t work though if you’re trying to create a modern remake of snake with rounded turns…

  • Liked by
Reply
Cancel
0 on December 22, 2018

Thank you for your thoughts. I went with bennie3211’s idea to use a line renderer.

The script isn’t perfect, but if someone stumbles into this post, they’re welcome to reference it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(LineRenderer))]
[RequireComponent(typeof(Rigidbody2D))]
public class Paddle : MonoBehaviour
{
LineRenderer line;
private GameManager gameManager;
private Vector3 currentPoint;
 private Vector3 nextPoint;
 private float distance;
 private float counter = 0f;
 private bool ready = false;
 private int direction = 1;
private List<Vector3> points;
 private int currentCorner;
[SerializeField]
 private float speed = 6f;
public void Init(int startingCorner, GameManager gameManager, float lineWidth)
 {
 this.gameManager = gameManager;
points = new List<Vector3>();
line = gameObject.GetComponent<LineRenderer>();
 line.useWorldSpace = false;
 line.numCapVertices = 15;
 line.SetWidth(lineWidth, lineWidth);
currentCorner = startingCorner;
 this.currentPoint = gameManager.GetScreenCorner(startingCorner);
 this.nextPoint = gameManager.GetScreenCorner(startingCorner + 1);
distance = Vector3.Distance(currentPoint, nextPoint);
RenderInitialLine();
 }
private void Update()
 {
 if (ready)
 {
 if (counter < 1)
 {
 counter += .1f / speed;
 float x = Mathf.Lerp(0, distance, counter);
if (x == distance)
 {
 currentCorner++;
 points.Add(nextPoint);
 ChangeDirection();
 return;
 }
Vector3 pointALongLine = x * Vector3.Normalize(nextPoint - currentPoint) + currentPoint;
 points.Add(pointALongLine);
 points.RemoveAt(0);
 }
 line.SetPositions(points.ToArray());
 }
 }
private void RenderInitialLine()
 {
 while (counter < 0.5)
 {
 counter += .1f / speed;
 float x = Mathf.Lerp(0, distance, counter);
Vector3 pointALongLine = x * Vector3.Normalize(nextPoint - currentPoint) + currentPoint;
 line.positionCount = points.Count + 1;
 points.Add(pointALongLine);
 }
line.SetPositions(points.ToArray());
 ready = true;
 }
private void ChangeDirection()
 {
 counter = 0f;
 currentPoint = nextPoint;
 nextPoint = gameManager.GetScreenCorner(GetNextCorner());
distance = Vector3.Distance(currentPoint, nextPoint);
 }
private int GetNextCorner()
 {
 if (currentCorner == 4)
 {
 currentCorner = 0;
 }
int nextCorner = currentCorner + direction;
//if moving counter-clockwise
 if (nextCorner > 3)
 {
 nextCorner = 0;
 }
//if moving clockwise
 if (nextCorner < 0)
 {
 nextCorner = 3;
 }
return nextCorner;
 }
public void ChangeRotation()
 {
 direction = direction * -1;
 }
}
  • Liked by
Reply
Cancel