Unity: draw in game view - unity3d

i want to draw guidlines in game view like in cinemacine
screenshot
Ive tried Handles.DrawLine and GUI.DrawTexture but they only shows up in scene view not game view
how do i do it

You can try using line renderers of the Unity UI System.
Here's an example of how to edit a line from script.
LineRenderer lineRenderer;
void Start()
{
lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.positionCount = 2;
lineRenderer.SetPosition(0, new Vector3(0, 0, 0));
lineRenderer.SetPosition(1, new Vector3(10, 10, 10));
}

Related

how to position a UI Canvas at the same point as it's parent game object

I made a UI Canvas prefab which has an empty game object with the following script attached to it:
float remainderAngle;
Image rotAngleWedge;
Image remAngleWedge;
public Image rotateWedgePrefab;
float zRotation = 0f;
public void PieGraph (float ringRotateAngle, Transform parentTransform) {
remainderAngle = 360f - ringRotateAngle;
rotAngleWedge = Instantiate (rotateWedgePrefab) as Image;
rotAngleWedge.transform.position = parentTransform.position;
rotAngleWedge.transform.SetParent (transform, false);
rotAngleWedge.color = Color.yellow;
rotAngleWedge.fillAmount = ringRotateAngle / 360f;
zRotation = Quaternion.Euler (parentTransform.eulerAngles).z - 45.0f;
rotAngleWedge.transform.rotation = Quaternion.Euler (new Vector3 (0, 0, zRotation));
zRotation -= rotAngleWedge.fillAmount * 360f;
remAngleWedge = Instantiate (rotateWedgePrefab) as Image;
remAngleWedge.transform.position = parentTransform.position;
remAngleWedge.transform.SetParent (transform, false);
remAngleWedge.color = Color.white;
remAngleWedge.fillAmount = remainderAngle / 360f;
remAngleWedge.transform.rotation = Quaternion.Euler (new Vector3 (0, 0, zRotation));
}
Then I called the PieGraph method in the parent game object script after creating the prefab using. I can't seem to get the UI Canvas to be positioned at the same place as its parent object. Please see images of the inspector settings for the Canvas and its child object below. How can I position them at the same place?
UI Canvas Prefab Inspector Settings
UI Canvas Prefab Child Game Object Inspector Settings
You can try to assign RenderMode to world space.

Make rectangle in Unity

I stuck on the basics of Unity. I want to make a scene for mobiles in which there are four different coloured rectangles that take 25% of the screen each.
I tried making an GameObject Image, "registering" it as a prefab in Inspector.
Below code is an example of how I tried to make a single red rectangle and position it on the (x,y,z) => (0,0,0) coordinates on my scene.
Several problems are present:
Rectangle did not appear
I don't know how to programatically specify width and height of the rectangle
This is how it looks:
public class SceneScript : MonoBehaviour {
public GameObject prefab;
void Start () {
Vector3 pos = new Vector3(0, 0, 0);
GameObject gameObject = Instantiate(prefab);
Image image = gameObject.GetComponent<Image>();
image.color = new Color(1.0F, 0.0F, 0.0F);
gameObject.transform.position = new Vector3(0, 0, 0);
}
// Update is called once per frame
void Update () {
}
}
Is there an easier solution, or is this the best practice + could you please provide me some hints what should I do?
You probably don't have a Canvas in your heirarchy. Here's how you can programmatically create your Canvas, your Image and their containing GameObjects:
Vector3 pos = new Vector3(0, 0, 0);
GameObject parentGameObject = new GameObject();
Canvas canvas = parentGameObject.AddComponent<Canvas>();
GameObject imageGameObject = new GameObject();
imageGameObject.transform.SetParent(canvas.transform);
Image image = imageGameObject.AddComponent<Image>();
image.color = new Color(1.0F, 0.0F, 0.0F);
imageGameObject.transform.position = pos;
This will create a full-screen, red rectangle. Play around with the RectTransform settings in the inspector after these are created and you should be able to figure out how to size them properly.

How can I move an UI Image to a touch position?

I have an image on the scene. Now when I touch on my mobile device to a specific position, I want, that the image moves to this position.
How can I do it?
I wrote this:
transform.position = Input.mousePosition;
But this don't work. Ok, it's working, but the image is not anymore on the screen. It's somewhere on the right side.
I found this. But it's also not working:
gameObject.GetComponent<RectTransform>().localPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Here is the problem, that when I touch on the screen bottom left (mobile) the object is in the center of the screen.
You can move the UI Image by putting the following in a script attached to it:
void Update() {
for (var i = 0; i < Input.touchCount; i++) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
// assign new position to where finger was pressed
transform.position = new Vector3 (Input.GetTouch(i).position.x Input.GetTouch(i).position.y, transform.position.z);
}
}
}
use
gameobject.transform.position = camera.ScreenToWorldPoint(Input.mousePosition);
for Desktop. for mobile you want to replace InputmousePostion with input.getTouch[i].position.
I know this is an old post but this took way longer than it should have so thought i'd share my solution in case it helps other trying to do the same thing.
var touchpoint = Instantiate(Resources.Load("prefabs/touchpoint"), new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
//set it parent to the main canvas
touchpoint.transform.parent = MainCanvas.transform;
//convert touch position to localposition reletive to canvas
Vector2 localPos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(MainCanvasRect, new Vector3(t.position.x, t.position.y, 0), null, out localPos);
//set position on canvas
touchpoint.transform.localPosition = localPos;

How to let a camera follow a rolling ball with photon in unity

I created a multiplayer game with photon in unity. The player is a rolling ball, i want to set a camera for each player but it can't be a child of the ball otherwise it rotates to. Without photon it worked with a script on the camera but now with the multiplayer the camera doesn't follow the rolling ball. How can i fix it?
You need to create a script and add it to your camera.
public GameObject player = GameObject.Find("Player");
this.transform.position = new Vector3(player.transform.position.x, player.transform.position.y, transform.position.z);
So your player is always in center of your camera.
You should add this piece of code.
GameObject player;
Vector3 cameraOffset;
void Start()
{
player = GameObject.Find("Player");
cameraOffset = new Vector3(0f, 0f, 0f)
}
void Update()
{
transform.position = new Vector3(player.transform.position.x + cameraOffset.x, player.transform.position.y + cameraOffset.y, player.transform.position.z + cameraOffset.z);
}
and attach it to you camera's script. I put the Offset as (0,0,0) but you should set an offset so your camera doesn't go inside of your player GameObject, but the amount is up to your criteria.

Unity3D: Sprite no appearing

Im creating a sprite like this:
Sprite.Create(myTexture,new Rect(0,0,myTexture.width,myTexture.height),new Vector2(1,1),100);
But the sprite is not appearing anywhere. What I'm doing wrong?
Because you also need SpriteRenderer to render your sprite.
void Start () {
Sprite mySprite = Sprite.Create(myTex, new Rect(0, 0, myTex.width, myTex.height), new Vector2(1, 1), 100);
GameObject myObj = new GameObject();
SpriteRenderer spriteRenderer = myObj.AddComponent<SpriteRenderer>();
spriteRenderer.sprite = mySprite;
}