With this code I can create a circle using LineRenderer and control in both editor mode and run time mode on the drawn circle properties like radius and width and height.
Now I want to add to this circle a collider not a box collider but a collider on the circle it self only on the drawn part/s. but not to create many colliders for each position. I could create a lot of colliders and position them on the circle positions but I’m looking for a better performance way if there is any.
And also when I’m changing the circle properties like width or height change the collider width and height too.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[ExecuteAlways]
[RequireComponent(typeof(UnityEngine.LineRenderer))]
public class DrawCircle : MonoBehaviour
{
public GameObject centerObject;
[Range(1, 50)] public int segments = 50;
[Range(1, 5)] public float xRadius = 5;
[Range(1, 5)] public float yRadius = 5;
[Range(0.1f, 5)] public float width = 0.1f;
[Range(0, 100)] public float height = 0;
public bool controlBothXradiusYradius = false;
public bool draw = true;
[SerializeField] private LineRenderer line;
private void Start()
{
if (!line) line = GetComponent<LineRenderer>();
if (draw)
CreatePoints();
}
public void CreatePoints()
{
line.enabled = true;
line.widthMultiplier = width;
line.useWorldSpace = false;
line.widthMultiplier = width;
line.positionCount = segments + 1;
float x;
float y;
var angle = 20f;
var points = new Vector3[segments + 1];
for (int i = 0; i < segments + 1; i++)
{
x = Mathf.Sin(Mathf.Deg2Rad * angle) * xRadius;
y = Mathf.Cos(Mathf.Deg2Rad * angle) * yRadius;
points[i] = new Vector3(x, height, y);
angle += (380f / segments);
}
// it's way more efficient to do this in one go!
line.SetPositions(points);
}
#if UNITY_EDITOR
private float prevXRadius, prevYRadius;
private int prevSegments;
private float prevWidth;
private float prevHeight;
private void OnValidate()
{
// Can't set up our line if the user hasn't connected it yet.
if (!line) line = GetComponent<LineRenderer>();
if (!line) return;
if (!draw)
{
// instead simply disable the component
line.enabled = false;
}
else
{
// Otherwise re-enable the component
// This will simply re-use the previously created points
line.enabled = true;
if (xRadius != prevXRadius || yRadius != prevYRadius || segments != prevSegments || width != prevWidth || height != prevHeight)
{
CreatePoints();
// Cache our most recently used values.
prevXRadius = xRadius;
prevYRadius = yRadius;
prevSegments = segments;
prevWidth = width;
prevHeight = height;
}
if (controlBothXradiusYradius)
{
yRadius = xRadius;
}
}
}
#endif
}
I’m trying to build a barrier blocker for my player so my player will not be able to pass any further then the circle radius.
When the player get to the drawn circle to the radius he should stop.
I thought using my script So I can easy make a circle over objects and decide the radius the player can move/walk from the object to the circle. The object is always in the middle of the circle so I can change the circle radius and height and other things and make it as a block.
I know there are other ways but I thought this way can be also a nice one.
I see now that I’m not using yet the variable centerObject. This should be the object that the circle should be drawn around and the centerObject should be always in the middle of the drawn circle. If I have a cube and I assign the cube to the centerObject then the drawn circle should be drawn around the cube.
How can I do it using this variable ? I forgot about it too.
If your point is in global space you just need to add the target position, but if you always draw the boundary around the object how can it ever go outside of it? or does the boundary remain static after created? i don’t understand.
but there are much better ways to restrict movement than creating a collider like that, i’m not sure how to do this with arbitrary shapes but if you can check if a position is inside or outside a given boundary you can just not allow movement instead of trying to stop movement.
if your shape was a perfect circle or a square you can run a distance function, look into this, there are ways to calculate all kinds of shapes, it’s not something I’m very good at so I’ll leave further explanation to your local google or another member here.
At this specific part in the game the player can get to far by foot without a vehicle.
Here is a script I created for it with a target as a distance I mean from the spaceship in the player background to some distance.
but the script is not working good. The player is getting to distance 61 slow down then stop but then I did that if the player rotate facing the other direction he can start walking again in the area that the distance is below 61 the player can move at 61 the player should not move.
but if the player make rotation facing the other direction and make a very small move then he can rotate again and move and pass the 61 distance limit this way with kind of “cheating” he can keep move far far away beyond the limit.
but this script is the idea of what I want to do.
Another problem is that this idea this limit is not working at all in other directions from the space ship. If the player is walking behind the space ship for some reason it all get messed.
I called the script DistanceCheck :
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
public class DistanceCheck : MonoBehaviour
{
public GameObject distanceTarget;
public GameObject longDistanceDescriptionTextImage;
public TextMeshProUGUI text;
private Animator anim;
float timeElapsed = 0;
float lerpDuration = 3;
float startValue = 1;
float endValue = 0;
float valueToLerp = 0;
// Opposite Direction
float timeElapsedOpposite = 0;
float lerpDurationOpposite = 3;
float startValueForOpposite = 0;
float endValueForOpposite = 1;
float valueToLerpOpposite = 0;
float angle;
// Start is called before the first frame update
void Start()
{
angle = transform.eulerAngles.y;
anim = transform.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
var distance = Vector3.Distance(transform.position, distanceTarget.transform.position);
angle = transform.eulerAngles.y;
if (distance > 61f && angle < 180)
{
if (timeElapsed < lerpDuration)
{
valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
anim.SetFloat("Forward", valueToLerp);
timeElapsed += Time.deltaTime;
}
anim.SetFloat("Forward", valueToLerp);
timeElapsedOpposite = 0;
longDistanceDescriptionTextImage.SetActive(true);
text.text = "I can't move that far by foot. I need to find some transportation to move any further.";
}
// Get the angle:
if (angle > 180f && distance > 61f)
{
text.text = "";
longDistanceDescriptionTextImage.SetActive(false);
if (timeElapsedOpposite < lerpDurationOpposite)
{
valueToLerpOpposite = Mathf.Lerp(startValueForOpposite, endValueForOpposite, timeElapsedOpposite / lerpDurationOpposite);
anim.SetFloat("Forward", valueToLerpOpposite);
timeElapsedOpposite += Time.deltaTime;
}
anim.SetFloat("Forward", valueToLerpOpposite);
timeElapsed = 0;
}
if (distance > 61f && Mathf.Abs(anim.GetFloat("Forward")) < 0.003943384f)
{
anim.SetBool("Idle", true);
}
else
{
anim.SetBool("Idle", false);
}
}
}
And this is a very short video clip I recorded showing the player movement and the problem starting from second 37 :
You right but I don’t make a direct stop but a slowdown to stop.
And I tried to make that if the player rotate facing the other direction facing the big space ship he will be able to walk again I don’t want the player to stop walking for ever so I did tried in the script to make that if the player is rotating facing the spaceship area angle he can walk back but then it all messed.
An about objects that have arbitrary shapes that is why I want to to use the line renderer and to draw a circle around this objects this way I can decide how to limit the area more easy so in some places the distance will not be the same but because the circle is a complete one the player will not be able to go any far then the circle radius.
For example :
This way I don’t have to worry about arbitrary shapes object I think. I don’t mind if the distance is not the same from all the directions I just think it’s easier to decide the limit around the object.