I have attached pollygon collider 2d on this gameObject.
This gameObject is parent of another gameObject (just small circle).
I want that small circle to randomly spawn somwhere on pollygon collider.
I tried something like this:
private void GetBounds()
{
polygonCollider = transform.parent.gameObject.GetComponent<PolygonCollider2D>();
bounds = polygonCollider.bounds;
}
public void TargetSetPosition()
{
x = Random.Range(bounds.min.x, bounds.max.x);
y = Random.Range(bounds.min.y, bounds.max.y);
transform.localPosition = new Vector2(x, y);
}
private void Start()
{
GetBounds();
TargetSetPosition();
}
This script is attached to that small circle. In 80% cases is good result but sometimes it get weird something like this:
Can someone help me please, becouse i tried almost everything.
The bounds = polygonCollider.bounds is getting the bounds of the bounding box. So in your second pic, the ball is inside the bounds.
To make it be inside the collider of your objects, you need to try something else, like this
Related
Im having problems with position convertions. The way im trying to solve it may be very wrong but thats due to inexperience in that case and im up for any suggestion on how to do it differently.
What im trying to do is a gui with a dot graph envelope that the user can change by draging the dots with the mouse.
This is what i would wan it to look like.
https://imgur.com/FP6f1Cz
First i did the UI like normal in overlay but i couldnt get the line renderer to work so i took the whole ui into world space. This makes the line renderer visible. With the UI in world space ive tried both to put the envelope line renderer in the canvas with the rest of the ui and outside the canvas UI.
Here is the code that renders the lines where the dots are and moves the dots when the mouse drags them :
public class Envelope : MonoBehaviour
{
LineRenderer lineRenderer;
// Start is called before the first frame update
void Start()
{
lineRenderer = GetComponentInChildren<LineRenderer>();
}
// Update is called once per frame
void Update()
{
var points = GetComponentsInChildren<EnvelopePoint>().Select(ep => ep.transform.localPosition).ToArray();
lineRenderer.positionCount = points.Length;
lineRenderer.SetPositions(points);
}
}
public class EnvelopePoint : MonoBehaviour
{
[SerializeField] bool isHeld = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (isHeld)
{
// Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 mousePos = Input.mousePosition;
transform.position = mousePos;
}
}
private void OnMouseDown()
{
isHeld = true;
}
private void OnMouseUp()
{
isHeld = false;
}
}
The best result is to put the envelope outside of the canvas.
The lines render well to where the points are but im not able to convert the mouse position to correct coordinates for the dots. When i click on a dot to drag it the dot snaps to a position a bit lower and a bit to the left of the mouse. Like this:
https://imgur.com/3KK6VD3
But then i can drag the dots and the lines adjust perfectly.
I guess i have two questions:
How should i get the mouse position conversion correctly?
Is this a strange or over complicated way of doing this? Is there a more reasonable way?
Id love some tip as well on what i should read up on to better understand the different screen types and how to convert between them.
RectTransformUtility.ScreenPointToWorldPointInRectangle: Transform a screen space point to a position in world space that is on the plane of the given RectTransform.
There is also ScreenPointToLocalPointInRectangle but since you are modifying Line Renderer's points (which are in world space), I think ScreenPointToWorldPointInRectangle best suits your needs.
I am attempting to create a 2d top-down car racing game. This game will have a random road map each time the player plays the game. I have thought about doing this in two different ways: A tilemap, or just generate the roads by placing different prefabs (straight roads, turns, etc). I have decided to go with the prefab route.
The way I believe it should work is to have prefab square "tiles" which have their own colliders set on the edges so I can tell if a player goes off the track in which case they blow up. I would have a MapGenerator Script which will generate an initial random map by keeping track of the last tile placed (including its location and road type: left turn, straight, right, etc). This script will then keep adding onto the road randomly as the player gets closer and closer to the end which makes it an infinite road.
I just want to know if this is just not efficient or if I am thinking of this completely wrong.
Here are a couple of images showing my road tiles which I made in photoshop and then one prefab for a straight road (take note of the colliders on its edges).
A similar game to one I want to make is Sling Drift which I can provide the link if you want. I don't know the policy on adding links to forum chat.
Also, here is my code for the map generator:
//Type of tyle, types are normal (straight road or horizontal road) and turns
public enum MapTileType
{
NORMAL,
N_E,
N_W,
S_E,
S_W
}
//structure for holding the last tile location and its type.
public struct TypedTileLocation
{
public TypedTileLocation(Vector2 pos, MapTileType tyleType)
{
m_tileType = tyleType;
m_position = pos;
}
public Vector2 m_position;
public MapTileType m_tileType;
}
public class MapGenerator : MonoBehaviour
{
//Map Tiles
public GameObject m_roadTile;
public GameObject m_turnNorthWestTile;
//holds all the tiles made in the game
private List<GameObject> m_allTiles;
//Map Tile Widths and Height
private float m_roadTileWidth, m_roadTileHeight;
//Used for generating next tile
TypedTileLocation m_lastTilePlaced;
private void Awake()
{
//store the initial beginning tile location (0,0)
m_lastTilePlaced = new TypedTileLocation(new Vector2(0,0), MapTileType.NORMAL);
//set height and width of tiles
m_roadTileWidth = m_roadTile.GetComponent<Renderer>().bounds.size.x;
m_roadTileHeight = m_roadTile.GetComponent<Renderer>().bounds.size.y;
m_allTiles = new List<GameObject>();
}
// Start is called before the first frame update
void Start()
{
SetupMap();
}
void SetupMap()
{
//starting at the beginning, just put a few tiles in straight before any turns occur
for (int i = 0; i < 6; ++i)
{
GameObject newTempTile = Instantiate(m_roadTile, new Vector2(0, m_roadTileHeight * i), Quaternion.identity);
m_lastTilePlaced.m_tileType = MapTileType.NORMAL;
m_lastTilePlaced.m_position.x = newTempTile.transform.position.x;
m_lastTilePlaced.m_position.y = newTempTile.transform.position.y;
m_allTiles.Add(newTempTile);
}
//now lets create a starter map of 100 road tiles (including turns and straigt-aways)
for (int i = 0; i < 100; ++i)
{
//first check if its time to create a turn. Maybe I'll randomly choose to either create a turn or not here
//draw either turn or straight road, if the tile was a turn decide which direction we are now going (N, W, E, S).
//this helps us determine which turns we can take next
//repeat this process.
}
}
void GenerateMoreMap()
{
//this will generate more map onto the already existing road and then will delete some of the others
}
// Update is called once per frame
void Update()
{
}
private void OnDrawGizmos()
{
}
}
Thanks!
Have you tried splines? They let you make curvy paths like race tracks easily. If not, here is a video that might help: https://www.youtube.com/watch?v=7j_BNf9s0jM.
I have a ball which we can throw here and there in my game. I want my cinemachine vcam to move only in the x direction with the ball. And just look up to the ball while its in air. I don't want the camera to move in y direction along it too.
I have set the look at and follow fields to the ball only. All i want it the camera to not follow the ball in y. (Basically, follow the ball in x, and keep looking at it in the air)
How can I achieve this is the most trivial and beautiful way possible? (open to scripts, but the lesser code the better)
as i've just tested, You can write a script that attaches to your camera, where you create a new vector3 with the x of your ball, and the frozen Y values that you want here's an exemple:
public class CameraController : MonoBehaviour
{
//Assign your ball in the inspector
public Transform target;
// Update is called once per frame
void Update()
{
//Here i assumed that you want to change the X
Vector3 newCamPosition = new Vector3(target.position.x, yourValue, yourValue);
gameObject.transform.position = newCamPosition;
}
}
Hope It helped <3
I am trying to cast a ray from one object to another but it is not working properly.
Result:
Selected object is "EnemyTank" and ray should point to "PlayerTank" but it is not as you can see.
My code:
void FixedUpdate () {
Vector3 dir = player.transform.position - rayOrigin.transform.position;
RaycastHit hitInfo;
dir = dir.normalized;
Debug.DrawRay(rayOrigin.transform.position, dir*maxCheckDistance,Color.red);
}
player variable points to "PlayerTank"
Playertank location:
So there are a few issues that I can see, but straight to the point:
FixedUpdate Runs on a set interval, it isn't every frame. the Method DrawRay() Has a parameter for duration. by default it is set to 0. This means it will only be visible for a single frame. You have 2 choices you can pass in a duration, or you can put this method in update which does run every frame.
void Update () {
Vector3 dir = player.transform.position - rayOrigin.transform.position;
dir = dir.normalized;
Debug.DrawRay(rayOrigin.transform.position, dir*maxCheckDistance,Color.red);
}
However if you are trying to draw a line from one object to another just use Debug.DrawLine()
Debug.DrawLine(rayOrigin.transform.position, player.transform.position, Color.red);
Lastly, avoid using a color for your line that is the same as one of your objects, I am referring to your red cube, and red line. Use a color that will stand out. Say black in this case.
FixedUpdate example:
void FixedUpdate () {
Vector3 dir = player.transform.position - rayOrigin.transform.position;
dir = dir.normalized;
Debug.DrawRay(rayOrigin.transform.position, dir*maxCheckDistance,Color.red, 1.0f);
}
For fun, to have the line change colors using your maxCheckDistance value:
void Update () {
Color lineColor = color.Black;
if(Vector3.Distance(rayOrigin.transform.position, player.transform.position) < maxCheckDistance) {
lineColor = color.White;
}
Debug.DrawLine(rayOrigin.transform.position, player.transform.position, lineColor);
}
EDIT:
It is important to know where your objects actually are, in your question you have a Player object, that you made the parent of 2 cubes. It appears as though you moved those 2 cubes into where you wanted the player to be in the world instead of moving the Player object itself. So your line is drawing correctly, as it is getting the position to the player object, In the future move the parent object instead of the children object.
From your description and screenshot.
You want to draw ray from "EnemyTank" to "PlayerTank".
and in your code "PlayerTank" is player and "EnemyTank" is rayOrigin.
There has draw a small ray from "EnemyTank" to some other direction. So you definitely miss to define your "PlayerTank" in player object.
The direction parameter in DrawRay is a vector in global space.
Thus your ray always points more or less to the origin.
Sorry, my answer is wrong.
I have the following simple prefab:
When I add this to my scene, it looks like this:
Very neat!
Then I have the following script on my Character:
public class MageController : MonoBehaviour {
public GameObject Spell;
public float SpellSpeed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.H)) {
GameObject newSpell = Instantiate(Spell);
newSpell.transform.position = transform.position;
newSpell.transform.rotation = Quaternion.LookRotation(transform.forward, transform.up);
Rigidbody rb = newSpell.GetComponent<Rigidbody>();
rb.AddForce(newSpell.transform.forward * SpellSpeed);
}
}
}
The goal is of course to make sure that the fireball is spawned correctly (with the tail behind it)
This works when I stand at 0.0.0; it looks like this:
However, if I turn around it looks like this:
As you can see, the rotation of the fireball is not correct (in the above incorrect image it is flying away from me, however, the tail is in front).
What am I doing wrong? How can I make sure that the tail is always correctly placed?
Update after following the guidance of PlantProgrammer
it still turns incorrectly :(
Look at the image below!
You want to use the forward direction of the player and not it's rotation, when instantiating the fireball. (Remember: transform in your script is the player transform not the fireball transform.) Check https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html. LookRotation will return the rotation based on player's forward and up vectors.
GameObject newSpell = Instantiate(Spell);
newSpell.transform.position = transform.position;
newSpell.transform.rotation = Quaternion.LookRotation(transform.forward, transform.up);
Not part of your question, but I would also suggest letting the fireball fly in the forward direction of itself not the player (as this leaves more room for later modifications)
rb.AddForce(newSpell.transform.forward * SpellSpeed);