So I've designed a custom HP bar and aligned it where I'd like it to be as well as how I'd like it to look.
However, when I press play (Not full screen mode, haven't even tested for that)either the image background slides slightly right or the green filler image slides to the left.
I have no idea why it's doing this or how to fix it. I'm willing to offer whatever information you require such as code or screenshots of the inspector.
This is a screen shot of the bar as it is in the sceneview canvas.
As you can see when the Play isn't pressed the bar functions normally. The above pic is Half-Full. The below image is Empty.
Part 2 of the issue:
I'm also having trouble with the HP bar rotating properly with the player.
However, when I turn left or right or face up:
]9
So you can see the HP bar doesn't properly rotate with the player's movement; although it does follow perfectly, the bar doesn't rotate accordingly. I can provide some movement code and the code I use to track the position of the player. What I have done was created a sphere and attached it to the player. I then attached the script for tracking the player onto that sphere. I then removed the mesh render and box collider of the sphere.
Health Bar Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PHealth : MonoBehaviour {
[Header("HP Bar Images")]
[SerializeField]
private Image HpBarBG;
[SerializeField]
private Image HpBarFillBar;
private float imgFill = 1;
void Update () {
FillBar()
}
private void FillBar(){
HpBarFillBar.fillAmount = imgFill;
}
}
Player Movement Script
public GameObject player;
public Vector3 localscale;
// public Transform start, end;
[SerializeField]
private float speed = 5;
void Start () {
}
// Update is called once per frame
void Update ()
{
/*
******** RAYCAST TO DETECT WALLS BELOW *******
*/
// WallDetection(); //Cast ray to detect walls
/*
********* MOVEMENT CODE BELOW *********
*/
if (Input.GetKey(KeyCode.W)|| Input.GetKey(KeyCode.UpArrow)) // Move
Forward
{
player.transform.Translate(Vector2.up * speed * Time.deltaTime,
Space.World);
transform.eulerAngles = new Vector2(0, 180);
}
}//End of class
I'd recommend adding your code to your question so we have the full picture. Before you do that, I can only give you some recommendations and suggestions. I'll amend this answer, so it becomes a real answer, afterwards.
In the mean time, what type of canvas are you using? Judging from the hierarchy, I'd imagine it's in either of the screen space modes. Have you considered a world-space canvas parented to your player? I believe it'll naturally rotate in the way that you want it to. Can the players zoom in and out, and is the player character's rotation fixed to 90-degree increments?
In addition, are you sure you want the healthbar to rotate? To be upside down? Won't it be a better idea to keep it fixed in a regular position, above the character's sprite, even if that's technically "below" the character at the time?
Finally, if you don't mind me asking: why Unity 5? It's been a couple of major releases after it, and 2018 is almost at its cycle's end. Though to be fair, I don't know if the version will make any difference for this, so I'm just being curious.
Related
It has two camera angles. First person for engaging gameplay and top-down for 2D game experience. But in top down camera view I can't figure out the rotation of the camera with respect to player.
I wrote the following script for the main(top-down) camera view.
public class FollowPlayer : MonoBehaviour
{
public GameObject player;
private Vector3 offset = new Vector3(0f, 24.953f, -0f);
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void LateUpdate()
{
transform.position = player.transform.position + offset;
}
}`
I tried
transform.rotation = player.transform.rotation;
but it didn't work since camera is on top of player. If I could somehow do
transform.rotation = player.transform.rotation + X axis 90 degree;
That would be perfect but I don't know how to do that.
If I understand you correctly, you want to follow the player from above with your second camera. Leaving some flexibility, the best option I think would be Unity's built in Transform.LookAt(target) method (LookAt), which automatically rotates an object (in your case the camera) so that it faces the target.
Therefore, you could do something like this in your Update, assuming your script is attached to the Camera. Otherwise substitute transform with Camera.main.transform:
transform.LookAt(player);
Note: If you plan to have your camera fixed above your player at all times, it is sufficient if you perform the LookAt once, e.g. in Start and then attach the camera as a child to the player. If your camera does not move with your player in the world but you want it to focus the player anyway, do do it in Update. Hope I addressed your problem:)
My problem has been resolved. I was trying to rotate the wrong axis. My bad.
mouseInput = Input.GetAxis("Mouse X");
transform.Rotate(Vector3.forward * speed * -mouseInput);
I needed to get mouse input from user and change the Z-axis according to it. Game is working fine now.
I am building a 3d topdown shooter. The player controls the avatar with the keyboard and the reticule with the mouse.
I found a simple way to implement the reticule based on this article:
https://gamedevbeginner.com/how-to-convert-the-mouse-position-to-world-space-in-unity-2d-3d/
I defined an object which represents the reticule and attached this script:
public class Reticule : MonoBehaviour
{
Camera mainCamera;
Plane plane;
float distance;
Ray ray;
// Start is called before the first frame update
void Start()
{
mainCamera = Camera.main;
plane = new Plane(Vector3.up, 0);
// This would be turned off in the game. I set to on here, to allow me to see the cursor
Cursor.visible = true;
}
// Update is called once per frame
void Update()
{
ray = mainCamera.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out distance))
{
transform.position = ray.GetPoint(distance);
}
}
}
This works, but the issue is that the reticule is lagging behind the mouse cursor, and catches up when I stop moving the mouse.
Is this because this method is just slow. Is there another simple way to achieve the same result?
the issue is that the reticule is lagging behind the mouse cursor, and catches up when I stop moving the mouse
That's normal, your mouse cursor is drawn by the graphics driver (with the help of WDM) as fast as the mouse data information comes over the wire, while your game only renders at a fixed framerate (or slower). Your hardware mouse will always be ahead of where your game draws it or anything related to it.
Some things that can help with working around this:
Don't show the system cursor, instead show your own. That way the cursor you show will always be in the same place your game thinks it is (since it drew it) and you won't have this issue. Careful with this however, because if your game's frame rate starts dipping it will be VERY noticeable when your cursor movement isn't smooth anymore.
Don't tie objects to your cursor. The issue doesn't show with normal interactions, like clicking buttons. You will notice this in RTS games when drawing boxes around units, but I struggle to think of another example of this.
Like above, but less restrictive, you could lerp the objects tied to your cursor in place, so they're always and intentionally behind it. It makes it look more gamey, which isn't that bad for a, you know, game. Though I wouldn't do this for a twitchy shooter, of course.
I would like to use a characters tongue so instead of shooting a bullet, it goes toward the enemy, licks it, and comes back. I got this wording from this question: Unity shooting with Tongue 2d game (hasn't been answered and is 4+ years old). The only difference is my character moves.
I have this code from looking at a shooting tutorial so when you click the tongue prefab generates and is at the correct angle. I need it to grow on click and shrink back.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LineController : MonoBehaviour
{
public GameObject player;
private Vector3 target;
public GameObject crosshairs;
public GameObject tonguePrefab;
// Start is called before the first frame update
void Start()
{
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
target = transform.GetComponent<Camera>().ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
crosshairs.transform.position = new Vector2(target.x, target.y);
Vector3 difference = target - player.transform.position;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
if (Input.GetMouseButtonDown(0))
{
shootTongue(rotationZ);
}
}
IEnumerator shootTongue(float rotationZ)
{
GameObject t = Instantiate(tonguePrefab) as GameObject;
t.transform.position = new Vector2(target.x, target.y);
t.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);
yield return new WaitForSeconds(2000);
t.SetActive(false);
}
}
I was also trying to make it disappear after whatever time and with that it doesn't work at all?
If the prefab is just a line with a simple texture I suggest using a line renderer:
Create an empty transform that starts on the mouth and then it move untill it reaches the crosshair. The movement is done using Vector2.MoveTowards.
Your tongue will no be a line renderer component. This line renderer will have 2 points, the start which is static, and the end which you have to update on the Update() for example, to correspond to the positions of the empty transform in item 1.
If you really wish to expand a tongue gameobject instead, then you are going to need a bit of math, this answer has what you need but in 3D:
https://answers.unity.com/questions/473076/scaling-in-the-forward-direction.html
1) I suggest creating sprite/sprite sheet animations with Mecanim or relatively new Skeletal Animation with Anima2D. With these systems you can create nice animations, transitions, even animate the sphere collider to trigger collisions and actions, change active state of your objects, etc. and control the animation with very little code. This way you can get best effects in my opinion.
As tongue is not a bullet... :) You only need one. I don't think you want to Instantiate/Create prefabs every time you press the lick button. Instead you should just turn your tongue object on/off (gameObject.SetActive). You can also change your object active state within the animation. So if you don't know how to code it you can do most of it in the Animation window and use very simple code to play the animation when you press the lick button. Whenever a sphere collider touches something you can tell the Animator Controller to play a 'roll back' animation and it will transition nicely to the start position.
There are many tutorials about Mecanim, Animations, 2D Animations, Anima2D, Animator Controller out there.
2) If you need very good control over the tongue you could create a custom mesh and control it via script but this is far more difficult.
3) The reason why your object is not turning off is probably because you wrote WaitForSeconds(2000) so it will turn off after 2000 seconds - more than half an hour. You should also call it with StartCoroutine(shootTongue()) as it is a Coroutine. Again if you want to turn off the object don't create new ones every time. If you want to keep creating new objects you should Destroy the objects instead. Otherwise you will end up with a lot of deactivated tongues in your scene and I don't think you needs that many tongues.
I want to follow the camera the player along (just the y axis, but this I will look later) for now I have the following problem:
I have a background image in a seperate canvas (World Space) with my Main Camera attached.
Now if I attach a camera follow script (simply transform.position = Player.transform.position) the background image follows.
This results to the following: Everything falls down.
I tried to use another camera but I don't know if I just did it wrong. I have no idea.
as I said, just:
void update()
{
transform.position = Player.transform.position
}
but the code isn't my problem. I expect that the camera follows the player on the y axis (but not 100% just smoothly a bit) but I will look up for that later
I want that the Background doesn't move. Is there another Way to have them background images as a world space, or something I don't know?
void update()
{transform.position = Player.transform.position}
this will make your camera follow your player whatever his direction.
while u didnt mention any details, I expect the reason why everything is falling bec your mainplayer is falling due to physics . and u need to fix this by adding rigibody to your mainplayer and make sure that any thing he walk on also has colliders.
if u want your camera only to follow player on Y axis only , it should be something like this
void update()
{ float y = player.transfor.position.y;
Vector3 newPos = new Vector3(transform.position.x,y,transform.position.z);
transform.position = newPos;
}
and I suggest you cach your transform varaibles too.
lastly about your image that you dont want it to move, just dont attach it to any moving gameobject and set it to static and it should never move at all.
In my hierarchy, I have my gun as a child to my camera. As you can tell from the pictures the gun appears to float in the air upon moving the camera up. How can I make the gun relative to the camera so that it sticks to the camera however I position it in the inspector?
I don't want it to look like this
I want it to look like this, even when rotating the camera up.
You could attach to your Gun Object a script that does:
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
public Quaternion x;
// Use this for initialization
void Start () {
x = transform.rotation;
}
// Update is called once per frame
void Update () {
transform.rotation = x;
}
}
From what I understand you want the Gun to remain a child but you want to "lock" rotation. This script would retain the Rotation of the gun, and while also letting you translate the location without locking that.
Store the rotation initially as a Quaternion during Start, then when Update is called every frame, it'll set the rotation to the initial Quaternion.
In the future if you want to break the Rotation to individual x, y, z, you can lock certain axes with transform.rotation.x, y, z and store each individually as floating point variables.
After hours of search, I ended up figuring it out by myself. The parent object the camera was connected to wasn't a uniform shape thus skewing the gun's shape when rotating the camera. I simply set the scale of the parent object to 1,1,1 and it works as intended.