Im trying to make a sprite rotate onscreen to show the player how their Jet is rotated while they fly.
Here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AircraftScript : MonoBehaviour
{
public float Altitude;
public Text AltitudeText;
public GameObject AircraftImage;
public GameObject Aircraft;
void Update()
{
Altitude = Aircraft.GetComponent<Rigidbody>().position.y;
AltitudeText.text = Altitude + "ft";
AircraftImage.Rotate.z = Aircraft.Rotate.z;
}
}
It changed all the values of the Sprite (x,y,z) instead of just the z.
I did also try
AircraftImage.Rotate = new Vector3(0 , 0 , Aircraft.Rotate);
and
AircraftImage.Rotate = new Vector3(0 , 0 , Aircraft.Rotate.z);
neither worked, what have I done wrong?
I don't understand your code: Aircraft and AircraftImage are GameObjects, and GameObjects don't have the Rotate method, so that code should throw an exception.
Anyway I try to help you anyway.
Try using this code:
AircraftImage.GetComponent<RectTransform>().eulerAngles = new Vector3(0, 0, Aircraft.transform.eulerAngles.z);
Related
I have a gameObject that I would like to move between X=-2 and X=+2. It follows the position of a canvas Image that also moves in X axis as X=12 and X=150. How do I make sure my gameObject follows the right axis of the image without parenting it? It should be clamped between -2 & +2. So how do I do this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowImagePosition : MonoBehaviour
{
public GameObject followImage;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void LateUpdate()
{
this.transform.position = new Vector3(followImage.transform.position.x, this.transform.position.z, this.transform.position.z);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Well : MonoBehaviour
{
public int currentLiter = 5;
public int maxWellCapacity = 10;
public float refillTime = 8f;
public Text wellDurText;
public Image refillTimeText;
public GameObject player;
public void Update()
{
if (currentLiter < maxWellCapacity)
{
RefillWell();
}
wellDurText.GetComponent<Text>().text = currentLiter.ToString();
wellDurText.transform.LookAt(player.transform);
refillTimeText.GetComponent<Image>().fillAmount = refillTime/8;
refillTimeText.transform.LookAt(player.transform);
}
void RefillWell()
{
refillTime -= Time.deltaTime;
if (refillTime <= 0.0f)
{
currentLiter += 1;
refillTime = 8f;
}
}
}
what I'm trying to do:
wellDurText to face the player wherever the player is but for some reason, it's flipped.
what I tried to do:
I tried to flipped the UI manually from the editor but when I tried it in the game it just flipped back over.
UI elements forward vector has to point away from the spectator. This is because usually the default forward direction used for e.g. 2D UI points into the display but you want to gave the UI the user sitting in front (or from Unity perspective behind ;) ) of the display.
When you are using
wellDurText.transform.LookAt(player.transform);
You tell it to point with its forward vector towards the player so it points in the exact wrong direction.
It rather should be e.g.
var direction = wellDurText.transform.position - player.transform.position;
var lookRotation = Quaternion.LookDirection(direction);
wellDurText.transform.rotation = lookRotation;
Same also for the RefillTimeText
I'm tryinng to change the camera position by a GameManager script, but having an error like this:
< MissingComponentException: There is no 'Camera' attached to the "Game Manager" game object, but a script is trying to access it.
You probably need to add a Camera to the game object "Game Manager". Or your script needs to check if the component is attached before using it. >
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager: MonoBehaviour
{
public GameObject maincamera;
public void Awake ()
{
maincamera = GameObject.Find("Camera");
maincamera.transform.position = new Vector3(1, 1, 1);
}
}
Can someone explain what i get wrong and how can i fix this?
I have this code to set random x and z coordinates for several game objects:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class SetPositions : MonoBehaviour
{
// Use this for initialization
void Start ()
{
System.Random rnd = new System.Random();
int a = rnd.Next (-9,9);
int b = rnd.Next(-9,9);
transform.position = new Vector3(a, transform.position.y, b);
}
}
When I use this script with multiple game objects, mot of them end up in the exact same location. I have gathered that this has something to do with the time being used, but how to I make sure all of the objects are in different positions? Is there a workaround to generate the random numbers at different times, or should the code be moved around/changed?
You may need Random.insideUnitCircle
var rndPoint = (Random.insideUnitCircle * 9);
transform.position = new Vector3(rndPoint.x, transform.position.y, rndPoint.y);
Or Random.insideUnitSphere
var rndPoint = (Random.insideUnitSphere * 9);
rndPoint.y = transform.position.y;
transform.position = rndPoint;
Thanks to Scott Chamberlain's comment, I was able to find the Unity Documentation for random numbers and solve my problem. UnityEngine.Random evidently does not use the time and so random positions are different from each other. Here is the correct script:
using UnityEngine;
using System.Collections;
public class SetPositions : MonoBehaviour
{
public GameObject PickUp;
void Start()
{
transform.position = new Vector3(Random.Range(-9.5f, 9.5f), 0.5f, Random.Range(-9.5f, 9.5f));
}
}
PickUp is a prefab that I use for all of the pickup objects.
I want to know how to get the position of all available player in the network.
So how can I update my script ?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Pathfinding : MonoBehaviour {
Transform[] Players;
int TotalPlayerCount=0;
void Update() {
foreach (PhotonPlayer pl in PhotonNetwork.playerList) {
if (GetComponent<PhotonView> ().isMine) {
Players[TotalPlayerCount].position= //position of my player
TotalPlayerCount++;
}
else
{
Players[TotalPlayerCount].position=//position of all other player available in the room
TotalPlayerCount++;
}
}
}
Each client can set it's player's custom properties with SetCustomProperties, even before being in a room. They are synced when joining a room.
Hashtable xyPos = new Hashtable();
xyPos.Add(1, "10");
xyPos.Add(2, "-20");
PhotonPlayer.SetCustomProperties(xyPos, null, true) ;
For further reading, you should try following tutorial(It will give you a stronger idea how to get and sync positions):
http://doc.photonengine.com/en/pun/current/tutorials/tutorial-marco-polo#_Toc317517599