3D Compass in Hololens - c#

I'm trying to develop a simple 3D Compass for Hololens 2, using Unity, but my sprites aren't aligned with the Cubes.
I wanted it to be like this: https://www.youtube.com/watch?v=3RuOq9ldX9g
If I move the cubes, the compass works as intended, but it still doesn't align with my FOV.
My code for getting the position on the compass is the following:
Vector2 GetPosOnCompass(QuestMarker marker)
{
Vector2 playerPos = new Vector2(Player.transform.position.x, Player.transform.position.z);
Vector2 playerFwd = new Vector2(Player.transform.forward.x, Player.transform.forward.z);
float angle = Vector2.SignedAngle(marker.position - playerPos, playerFwd);
return new Vector2(compassUnit * angle, 0f);
}
Best regards,
Carlos
This is the full Code:
using UnityEngine.UI;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Compass : MonoBehaviour
{
public GameObject iconPrefab;
List<QuestMarker> questMarkers = new List<QuestMarker>();
public RawImage CompassImage;
public Transform Player;
public Text CompassDirectionText;
float compassUnit;
public QuestMarker one;
public QuestMarker two;
public QuestMarker three;
private void Start()
{
compassUnit = CompassImage.rectTransform.rect.width / 360f;
AddQuestMarker(one);
AddQuestMarker(two);
AddQuestMarker(three);
}
public void Update()
{
foreach (QuestMarker marker in questMarkers)
{
marker.image.rectTransform.anchoredPosition = GetPosOnCompass(marker);
}
//Get a handle on the Image's uvRect
CompassImage.uvRect = new Rect(Player.localEulerAngles.y / 360, 0, 1, 1);
// Get a copy of your forward vector
Vector3 forward = Player.transform.forward;
// Zero out the y component of your forward vector to only get the direction in the X,Z plane
forward.y = 0;
//Clamp our angles to only 5 degree increments
float headingAngle = Quaternion.LookRotation(forward).eulerAngles.y;
headingAngle = 5 * (Mathf.RoundToInt(headingAngle / 5.0f));
//Convert float to int for switch
int displayangle;
displayangle = Mathf.RoundToInt(headingAngle);
//Set the text of Compass Degree Text to the clamped value, but change it to the letter if it is a True direction
switch (displayangle)
{
case 0:
//Do this
CompassDirectionText.text = "N";
break;
case 360:
//Do this
CompassDirectionText.text = "N";
break;
case 45:
//Do this
CompassDirectionText.text = "NE";
break;
case 90:
//Do this
CompassDirectionText.text = "E";
break;
case 130:
//Do this
CompassDirectionText.text = "SE";
break;
case 180:
//Do this
CompassDirectionText.text = "S";
break;
case 225:
//Do this
CompassDirectionText.text = "SW";
break;
case 270:
//Do this
CompassDirectionText.text = "W";
break;
default:
CompassDirectionText.text = headingAngle.ToString ();
break;
}
}
public void AddQuestMarker(QuestMarker marker)
{
GameObject newMarker = Instantiate(iconPrefab, CompassImage.transform);
marker.image = newMarker.GetComponent<Image>();
marker.image.sprite = marker.icon;
questMarkers.Add(marker);
}
Vector2 GetPosOnCompass(QuestMarker marker)
{
Vector2 playerPos = new Vector2(Player.transform.position.x, Player.transform.position.z);
Vector2 playerFwd = new Vector2(Player.transform.forward.x, Player.transform.forward.z);
float angle = Vector2.SignedAngle(marker.position - playerPos, playerFwd);
return new Vector2(compassUnit * angle, 0f);
}
}

Related

I want to make a method that returns the position the player is facing

I made a simple script to check what position the player is facing and put that in my animator
1 = up
2 = right
3 = down
4 = left
private Vector2 velocity;
private Animator animator;
private int direction;
private void Awake() {
animator = GetComponent<Animator>();
}
void Update(){
velocity.x = Input.GetAxisRaw("Horizontal");
velocity.y = Input.GetAxisRaw("Vertical");
switch(velocity){
case Vector2(0,1):
direction = 1;
break;
case Vector2(1,0):
direction = 2;
break;
case Vector2(0,-1):
direction = 3;
break;
case Vector2(-1,0):
direction = 4;
break;
}
animator.SetFloat("Facing",direction);
then I get the error
Assets/Scripts/PlayerMovement.cs(21,25): error CS8129: No suitable 'Deconstruct' instance or extension method was found for type 'Vector2', with 2 out parameters and a void return type.
I believe your issue comes with how you are trying to use your switch statement as Vector(1,0) is attempting to call a function not actually reference the value you are looking a viable alternative is to use when like in the following example
case Vector2 v when v.Equals(Vector2.up):
Debug.Log("Up");
break;
case Vector2 v when v.Equals(Vector2.left):
Debug.Log("Left");
break;
case Vector2 v when v.Equals(Vector2.back):
Debug.Log("Back");
break;
case Vector2 v when v.Equals(Vector2.right):
Debug.Log("Right");
break;
I'm also using the shorthand up, left, right and down instead of defining the values manually
Assuming velocity is of type Vector2 you can't set the values of x and y as they are read-only properties.
Use this instead:
velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Vectors are structs and you can't create them without using new e.g. case new Vector2(0f, 1f):. There are some ready made shortcuts for direction built in that you can use:
if(velocity == Vector2.up)
{
print("direction = 1");
} else if (velocity == Vector2.right)
{
print("direction = 2");
} else if (velocity == Vector2.up)
{
print("direction = 3");
} else if (velocity == Vector2.down)
{
print("direction = 4");
} else if (velocity == Vector2.left)
{
print("direction = 1");
}

How do I fix CS1525 error in this procedural generation code in my 2D roguelike dungeon game?

New to Unity C# coding. I'm writing a script to achieve procedural generation in a 2D roguelike game. My idea is to use enum to represent 4 directions (up, down, left, right), then pick a random direction to produce a room from Prefab. Then next room will be generated by the same method. Here are my codes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoomGenerator : MonoBehaviour
{
public enum Direction { up, down, left, right };
public Direction direction;
[Header("Room Information")]
public GameObject roomPrefab;
public int roomNumber;
public Color startColor, endColor;
[Header("Position Controller")]
public Transform generatorPoint;
public float xOffset;
public float yOffset;
public List<GameObject> rooms = new List<GameObject>();
void Start()
{
for (int = 0; i < roomNumber, int++)
{
rooms.Add(Instantiate(roomPrefab, transform.position, Quaternion.identity));
ChangePointPosition();
}
}
void Update()
{
}
public void ChangePointPosition()
{
direction = (Direction)Random.Range(0,4);
switch(direction)
{
case Direction.up:
generatorPoint.position += new Vector3 (0, yOffset, 0);
break;
case Direction.down:
generatorPoint.position += new Vector3 (0, -yOffset, 0);
break;
case Direction.left:
generatorPoint.position += new Vector3 (-xOffset, 0, 0);
break;
case Direction.right:
generatorPoint.position += new Vector3 (xOffset, 0, 0);
break;
}
}
}
Unity is saying "error CS1525: invalid expression term 'int'". How's that possible? Did I miss something? Please help. Thanks in advance!
You are missing variable name in your for-loop:
for (int i = 0; i < roomNumber, int++)

How to I resolve this error? Field never assigned and will always have null value [duplicate]

This question already has answers here:
Field is never assigned to, and will always have its default value null (CS0649)
(2 answers)
Closed 2 years ago.
I have created 4 c# scripts. When I run my 2d unity game I see this warning in my console.
"Assets\Scripts\GameHandler.cs(7,34): warning CS0649: Field
'GameHandler.car' is never assigned to, and will always have its
default value null"
I am creating a game similar to snake, using c# scripts in unity 2d. Never used unity before or c#, this is my first project. So far its going well, However, i keep getting this warning which is causing my game to crash.
I've attached 2 of my scripts the first gamehandler is where this issue is and I think its referring to the class Car which i attached below. It's a lot of code so I do apologise, I just have no clue.
public class GameHandler : MonoBehaviour
{
[SerializeField] private Car car;
private LevelGrid levelGrid;
// Start is called before the first frame update
private void Start()
{
Debug.Log("GameHandler.Start");
//GameObject carHeadGameObject = new GameObject();
//SpriteRenderer carSpriteRenderer = carHeadGameObject.AddComponent<SpriteRenderer>();
//carSpriteRenderer.sprite = GameAssets.instance.carHeadSprite;
levelGrid = new LevelGrid(20,20); //width,height of grid
car.Setup(levelGrid);
levelGrid.Setup(car);
}
}
--------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Car : MonoBehaviour
{
private enum Direction
{
Left,
Right,
Up,
Down
}
private enum State
{
Alive,
Dead
}
private State state;
private Vector2Int gridPosition; //uses ints instead of floats useful for grid positiioning
private float gridMoveTimer; //time remaining until next movement
private float gridMoveTimerMax; // time between moves
private Direction gridMoveDirection;
private LevelGrid levelGrid;
private int carsize;
private List<CarMovePosition> carMovePositionList;
private List<Carsize> carSizeList;
public void Setup(LevelGrid levelGrid) {
this.levelGrid = levelGrid;
}
private void Awake() {
gridPosition = new Vector2Int(10,10); //initalise grid position into middle of grid
gridMoveTimerMax = .2f; //car to move along grid every 1/2 second
gridMoveTimer = gridMoveTimerMax; //0f
gridMoveDirection = Direction.Right; // default move right
carMovePositionList = new List<CarMovePosition>();
carsize = 0;
carSizeList = new List<Carsize>();
state = State.Alive;
}
private void Update()
{
switch (state)
{
case State.Alive:
HandleInput(); // checks for keyboard input
HandleGridMovement();
break;
case State.Dead:
break;
}
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
if (gridMoveDirection != Direction.Down)
{ // can only go up if not going down
gridMoveDirection = Direction.Up;
}
}
//return true on up arrow press
if (Input.GetKeyDown(KeyCode.DownArrow))
{
if (gridMoveDirection != Direction.Up)
{
gridMoveDirection = Direction.Down;
}
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
if (gridMoveDirection != Direction.Right)
{
gridMoveDirection = Direction.Left;
}
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
if (gridMoveDirection != Direction.Left)
{
gridMoveDirection = Direction.Right;
}
}
}
private void HandleGridMovement()
{
gridMoveTimer += Time.deltaTime; // amount of time left since last update
if (gridMoveTimer >= gridMoveTimerMax)//if true then 1 sec since last move
{
gridMoveTimer -= gridMoveTimerMax;
CarMovePosition previousCarMovePosition = null;
if (carMovePositionList.Count > 0){
previousCarMovePosition = carMovePositionList[0];
}
CarMovePosition carMovePosition = new CarMovePosition(previousCarMovePosition, gridPosition, gridMoveDirection);
carMovePositionList.Insert(0, carMovePosition);
Vector2Int gridMoveDirectionVector;
switch (gridMoveDirection) {
default:
case Direction.Right: gridMoveDirectionVector = new Vector2Int(+1, 0);break;
case Direction.Left: gridMoveDirectionVector = new Vector2Int(-1, 0); break;
case Direction.Up: gridMoveDirectionVector = new Vector2Int(0, +1); break;
case Direction.Down: gridMoveDirectionVector = new Vector2Int(0, -1); break;
}
gridPosition += gridMoveDirectionVector;
bool cargotfuel = levelGrid.Trycarfuel(gridPosition);
if (cargotfuel)
{
carsize++;
CreateCarSize();
}
if (carMovePositionList.Count >= carsize + 1)
{
carMovePositionList.RemoveAt(carMovePositionList.Count - 1);
}
foreach (Carsize carsize in carSizeList)
{
Vector2Int carSizeGridPosition = carsize.GetGridPosition();
if (gridPosition == carSizeGridPosition)
{
//print("Gameover");
state = State.Dead;
}
}
transform.position = new Vector3(gridPosition.x, gridPosition.y);
//move transform based on location of gridPosition
transform.eulerAngles = new Vector3(0, 0, GetAngleFromVector(gridMoveDirectionVector) - 90);
//modify transform to face the correct way
UpdateCarSize();
}
}
private void CreateCarSize()
{
carSizeList.Add(new Carsize(carSizeList.Count));
}
private void UpdateCarSize(){
for (int i = 0; i < carSizeList.Count; i++) {
carSizeList[i].SetCarMovePosition(carMovePositionList[i]);
//carSizeList[i].SetGridPosition(carMovePositionList[i].GetGridPosition());
}
}
private float GetAngleFromVector(Vector2Int dir)
{
float n = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
if (n < 0) n += 360;
return n;
}
public Vector2Int GetGridPosition()
{
return gridPosition;
}
//returns car list of positions full with body
public List<Vector2Int> Getcargridpositionlist(){
List<Vector2Int> gridPositionList = new List<Vector2Int>() { gridPosition };
foreach (CarMovePosition carMovePosition in carMovePositionList)
{
gridPositionList.Add(carMovePosition.GetGridPosition());
}
return gridPositionList;
}
private class Carsize{
private CarMovePosition carMovePosition;
private Transform transform;
public Carsize(int sizeIndex){
GameObject carsGameObject = new GameObject("carBody", typeof(SpriteRenderer));
carsGameObject.GetComponent<SpriteRenderer>().sprite = GameAssets.instance.carsSprite;
carsGameObject.GetComponent<SpriteRenderer>().sortingOrder = -sizeIndex;
transform = carsGameObject.transform;
}
public void SetCarMovePosition(CarMovePosition carMovePosition){
this.carMovePosition = carMovePosition;
transform.position = new Vector3(carMovePosition.GetGridPosition().x, carMovePosition.GetGridPosition().y);
float angle;
switch (carMovePosition.GetDirection()){
default:
case Direction.Up:// going up
switch (carMovePosition.GetPreviousDirection()){
default:
angle = 0; break;
case Direction.Left: // was going left
angle = 0 + 45; break;
case Direction.Right:// was going right
angle = 0 - 45; break;
}
break;
case Direction.Down:
switch (carMovePosition.GetPreviousDirection()){
default:
angle = 180; break;
case Direction.Left:
angle = 180 + 45; break;
case Direction.Right:
angle = 180 - 45; break;
}
break;
case Direction.Left:
switch (carMovePosition.GetPreviousDirection()){
default:
angle = -90; break;
case Direction.Down:
angle = -45; break;
case Direction.Up:
angle = 45; break;
}
break;
case Direction.Right: // going right
switch (carMovePosition.GetPreviousDirection()){
default:
angle = 90; break;
case Direction.Down: // previously going down
angle = 45; break;
}
break;
}
transform.eulerAngles = new Vector3(0, 0, angle);
}
public Vector2Int GetGridPosition()
{
return carMovePosition.GetGridPosition();
}
}
private class CarMovePosition{
private CarMovePosition previousCarMovePosition;
private Vector2Int gridPosition;
private Direction direction;
public CarMovePosition(CarMovePosition previousCarMovePosition, Vector2Int gridPosition, Direction direction){
this.previousCarMovePosition = previousCarMovePosition;
this.gridPosition = gridPosition;
this.direction = direction;
}
public Vector2Int GetGridPosition(){
return gridPosition;
}
public Direction GetDirection(){
return direction;
}
public Direction GetPreviousDirection(){
if (previousCarMovePosition == null){
return Direction.Right;
} else {
return previousCarMovePosition.direction;
}
}
}
}
It may be the case of clicking/dragging something in unity2d itself which I had to do before. But I am so lost now.
It's telling you that
[SerializeField] private Car car;
is not initialized in code, but that's fine, because you're initializing it in the inspector. If you want to get rid of the warning, you can set it to null:
[SerializeField] private Car car = null;
The warning occurs because you are not initializing the object "car" and your game crashes when it tries to use this object, which has no value assigned to.
You must initialize the any object before using it in order to avoid crashes.
So, your Start() method must be like this:
private void Start()
{
Debug.Log("GameHandler.Start");
car = new Car(); // you must initialize objects before using them!
//GameObject carHeadGameObject = new GameObject();
//SpriteRenderer carSpriteRenderer = carHeadGameObject.AddComponent<SpriteRenderer>();
//carSpriteRenderer.sprite = GameAssets.instance.carHeadSprite;
levelGrid = new LevelGrid(20,20); //width,height of grid
car.Setup(levelGrid);
levelGrid.Setup(car);
}
Hope this help you!
If you don't want to suppress the error or set a value for all of your parameters, another solution is to set it from "private" to "private protected".
[SerializeField] private protected Car car;

C# procedural generation- how to do always a set number?

Ok, Working in Unity here and trying to get out this core game dynamic. Basically I have all these 3D platforms that have certain weights to them (weight meaning how often they appear in the array from which game can select next platform) -
right now each platform has a set of the 4 cardinal directions (plus up/down but all have those) where it can spawn the next platform for player to walk onto, else they fall. This is an enum and I set this manually.
Before player first steps on a platform, it is halfway alpha, so it just looks like a marker. After they step, it turns 1 alpha and thus doesn't look like a "marker", see here:
As the player walks via joystick, I want the platforms to spawn in square "tiers" - say of length 10 platforms - and I'll do something when tier is full. My problem is I've tried a bunch of different systems but do not know how to implement this. Model picture where there are different configurations but ultimately a 10x10 limit/bounds:
Here's my method of spawning based on direction - problem is direction is subjective, based on player object's point of view:
foreach(Direction d in directionsAvailable)
{
Vector3 pos = transform.position;
float dist = container.GetComponent<Renderer> ().bounds.size.x;
switch (d) {
case Direction.Backward:
pos = new Vector3 (pos.x, pos.y, pos.z-dist);
break;
case Direction.Forward:
pos = new Vector3 (pos.x, pos.y, pos.z+dist);
break;
case Direction.Left:
pos = new Vector3 (pos.x-dist, pos.y, pos.z);
break;
case Direction.Right:
pos = new Vector3 (pos.x+dist, pos.y, pos.z);
break;
case Direction.Down:
pos = new Vector3 (pos.x, pos.y-(2*dist), pos.z);
break;
case Direction.Up:
pos = new Vector3 (pos.x, pos.y+(2*dist), pos.z); //hits itself, might have to do more dist
break;
default:
break;
}
Here is how I test if a platform is at a position or position is open:
public Platform PlatAtPos(Vector3 pos)
{
platformsSpawned = GameObject.FindObjectsOfType<Platform>();
foreach(Platform p in platformsSpawned)
{
if(p.originPos == pos || p.transform.position == pos) //or just delete one of them
{
return p;
}
}
return null;
}
public void checkForPlatsAround()
{
gameController = GameObject.FindObjectOfType<GameController> ();
float dist = container.GetComponent<Renderer> ().bounds.size.x;
foreach (Platform.Direction dir in Enum.GetValues(typeof(Platform.Direction))) {
Vector3 pos = transform.position;
switch (dir) {
case Direction.Backward:
pos = new Vector3 (pos.x, pos.y, pos.z-dist);
break;
case Direction.Forward:
pos = new Vector3 (pos.x, pos.y, pos.z+dist);
break;
case Direction.Left:
pos = new Vector3 (pos.x-dist, pos.y, pos.z);
break;
case Direction.Right:
pos = new Vector3 (pos.x+dist, pos.y, pos.z);
break;
case Direction.Down:
pos = new Vector3 (pos.x, pos.y-(2*dist), pos.z);
break;
case Direction.Up:
pos = new Vector3 (pos.x, pos.y+(2*dist), pos.z); //hits itself, might have to do more dist
break;
default:
break;
}
if(gameController.PlatAtPos(pos) != null)
{
gameController.PlatAtPos (pos).showAsMarker ();
}
}
}
Is there a better/clearer way to go about this? How can I do this procedurally?

Unity - In game TV set

I have some TV model, like
and C# code
using UnityEngine;
using System.Collections;
public class ClicktoPlayWebMovieClass : MonoBehaviour {
// Use this for initialization
void Start () {
}
private Vector3 FindBoundCord (int i, GameObject _GameObject){
/*This is basically where the code starts. It starts out by creating a
* bounding box around the target GameObject.
It calculates the 8 coordinates forming the bounding box, and
returns them all to the for loop.
Because there are no real method which returns the coordinates
from the bounding box I had to create a switch/case which utillized
Bounds.center and Bounds.extents.*/
Bounds _TargetBounds = _GameObject.GetComponent<Renderer>().bounds;
Vector3 _TargetCenter = _TargetBounds.center;
Vector3 _TargetExtents = _TargetBounds.extents;
switch(i){
case 0:
return _TargetCenter + new Vector3(_TargetExtents.x, _TargetExtents.y, _TargetExtents.z);
case 1:
return _TargetCenter + new Vector3(_TargetExtents.x, _TargetExtents.y, _TargetExtents.z*-1);
case 2:
return _TargetCenter + new Vector3(_TargetExtents.x, _TargetExtents.y*-1, _TargetExtents.z);
case 3:
return _TargetCenter + new Vector3(_TargetExtents.x, _TargetExtents.y*-1, _TargetExtents.z*-1);
case 4:
return _TargetCenter + new Vector3(_TargetExtents.x*-1, _TargetExtents.y, _TargetExtents.z);
case 5:
return _TargetCenter + new Vector3(_TargetExtents.x*-1, _TargetExtents.y, _TargetExtents.z*-1);
case 6:
return _TargetCenter + new Vector3(_TargetExtents.x*-1, _TargetExtents.y*-1, _TargetExtents.z);
case 7:
return _TargetCenter + new Vector3(_TargetExtents.x*-1, _TargetExtents.y*-1, _TargetExtents.z*-1);
default:
return Vector3.zero;
}
}
IEnumerator MyMethod() {
//Debug.Log("Before Waiting 2 seconds");
yield return new WaitForSeconds(2);
//Debug.Log("After Waiting 2 Seconds");
}
public string url = "http://becunningandfulloftricks.files.wordpress.com/2013/04/hedgehog_in_the_fog.ogg";
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
Debug.Log("clicked");
// Start download
var www = new WWW(url);
// Make sure the movie is ready to start before we start playing
var movieTexture = www.movie;
while (!movieTexture.isReadyToPlay) {
StartCoroutine(MyMethod());
}
print("LOADED!!!");
Vector2 _ObjectScreenCord, _Xmin = new Vector2(Screen.width,0), _Xmax = Vector2.zero, _Ymin = new Vector2(Screen.height,0), _Ymax = Vector2.zero;
float _Height, _Width;
for(int i = 0; i != 8; i++){
//FindBoundCord() locates the eight coordinates that forms the boundries, followed by converting the coordinates to screen space.
// The entire script starts in FindBoundCord
_ObjectScreenCord = Camera.main.WorldToScreenPoint(FindBoundCord(i, transform.gameObject));
/* After gathering the coordinates and converting them to screen space
we try to locate which of these have the highest/minimum x and y values.
The difference between highest/minimum x and y must correspond to
width and height.*/
if(_ObjectScreenCord.x > _Xmax.x){
_Xmax.x = _ObjectScreenCord.x;
}
else if(_ObjectScreenCord.x < _Xmin.x){
_Xmin.x = _ObjectScreenCord.x;
}
if(_ObjectScreenCord.y > _Ymax.x){
_Ymax.x = _ObjectScreenCord.y;
}
else if(_ObjectScreenCord.y < _Ymin.x){
_Ymin.x = _ObjectScreenCord.y;
}
}
//Too measure the distance between them, I use the Vector2 method Distance.
_Height = Vector2.Distance(_Ymax, _Ymin);
if (_Height > Screen.height || _Height < 0){
_Height = 0;
}
_Width = Vector2.Distance(_Xmax, _Xmin);
if (_Width > Screen.width || _Width < 0){
_Width = 0;
}
print(_Height+" - "+_Width);
GetComponent<Renderer>().material.mainTexture = movieTexture;
// Assign clip to audio source
// Sync playback with audio
GetComponent<AudioSource>().clip = movieTexture.audioClip;
// Play both movie & sound
GUITexture gt = GetComponent<GUITexture>();
gt.texture = movieTexture;
transform.localScale = new Vector3 (0f,0f,0f);
transform.position = new Vector3 (0.5f,0.5f,0f);
Rect r = gt.pixelInset;
print(_Width);
print(movieTexture.width / 2);
r.xMin = -_Width / 2;
r.xMax = _Width / 2;
r.yMin = -_Height / 2;
r.yMax = _Height / 2;
// r.xMin = -movieTexture.width / 2;
// r.xMax = movieTexture.width / 2;
// r.yMin = -movieTexture.height / 2;
// r.yMax = movieTexture.height / 2;
gt.pixelInset=r;
movieTexture.Play();
GetComponent<AudioSource>().Play();
}
}
}
When I'am clicking on my TV set, looks good:
It looks good until I am not rotate my camera. After camera rotation movieTexture has initial position on the screen:
and it makes my TV set broken...
How to specify movieTexture, that it must have position of the GetComponent<Renderer>().material.mainTexture? I need make to look something like this
But instead of screen of the TV must be video of the movieTexture at it must rotate, like screen of the TV set...
Have you looked at projectors? They do this pretty well and just changes the texture like you want. They are normally used for shadows but any texture can be projected on any model.
http://docs.unity3d.com/Manual/class-Projector.html

Categories

Resources