Thanks for reading. I tried posting this on unity answers, but it's been 24 hours and it's still not out of moderation, so maybe I'll have better luck here.
Background
I am very new to unity and know I must be missing some critical piece of knowledge to tie this together. I've created a blank game with some placeholder terrain and added a main camera to the 3d orthographic scene. I would like the user to be able to move the camera along a parallel plane to the ground.
What I've Done
I've added a starter camera codebehind I found online along with an event system, rigid body, ray caster, and a bunch of other things I saw online for this sort of thing. All of these components are in the same layer.
The Problem
My camera script's update method is hit when I put that in for testing but it doesn't receive any other events even though it implements the appropriate interfaces. I'm guessing I have something blocking the input from hitting the script or I do not have a component I need in order for those events to fire.
I'm testing on desktop for now but I want this to run on mobile (although from what I've read the events I'm subscribing to should be universal). Can anyone spot any oversights or deficiencies in my setup so far? Here are some screenshots.
Thanks!
My Setup
Edit: by request, here is the camera script I'm using. I know it doesn't do what I want yet but the issue I'd like to tackle right now is why it doesn't seem to be wired up correctly. Like I said, if I put an update method in here it is hit but none of my other logs are hit.
using UnityEngine;
using UnityEngine.EventSystems;
// The touch is in 2D but the scene is in 3D, so touch.x => scene.x, touch.y => scene.z, and nothing change (0) => scene.y since y is up
public class TouchCamera : MonoBehaviour,
IPointerDownHandler,
IDragHandler,
IPointerUpHandler
{
private Vector3 prevPointWorldSpace;
private Vector3 thisPointWorldSpace;
private Vector3 realWorldTravel;
private int drawFinger;
private bool drawFingerAlreadyDown;
public void OnPointerDown(PointerEventData data)
{
Debug.Log("Pointer down");
if (drawFingerAlreadyDown == true)
return;
drawFinger = data.pointerId;
drawFingerAlreadyDown = true;
prevPointWorldSpace = data.pointerCurrentRaycast.worldPosition;
// in this example we'll put it under finger control...
GetComponent<Rigidbody>().isKinematic = false;
}
public void OnDrag(PointerEventData data)
{
Debug.Log("Pointer drag");
if (drawFingerAlreadyDown == false)
return;
if (drawFinger != data.pointerId)
return;
thisPointWorldSpace = data.pointerCurrentRaycast.worldPosition;
realWorldTravel = thisPointWorldSpace - prevPointWorldSpace;
_processRealWorldtravel();
prevPointWorldSpace = thisPointWorldSpace;
}
public void OnPointerUp(PointerEventData data)
{
Debug.Log("Pointer up");
if (drawFinger != data.pointerId)
return;
drawFingerAlreadyDown = false;
GetComponent<Rigidbody>().isKinematic = false;
}
private void _processRealWorldtravel()
{
Debug.Log("Updating camera position");
Vector3 pot = transform.position;
pot.x += realWorldTravel.x;
pot.z += realWorldTravel.y;
transform.position = pot;
}
}
Final Edit for the Solution
Thank you very much to Juan Bayona Beriso for helping me in chat with this configuration. Here is what we ended up with that now works:
The camera object has a Rigidbody and Physics 2D Raycaster attached to it. The scene now has a new Canvas object with a Graphics Raycaster (blocking mask set to everything and blocking objects set to none) and an event system with the standalone input module enabled. That canvas also has a child UI object that covers the screen and has the camera script attached. As a curiosity, it required a text component in order for the script to start receiving the events -- this only made a difference when the Raycast Target option was selected. Screenshots below.
I think you are misusing the EventSystem, OnPointerDown, OnDrag and all those functions, they are called when you have a PhysicsRaycaster in your Camera object and you press or drag over an UI Element or and object with a collider.
In your code you don't have any of these, you have a camera so you are not actually pointer down over it or dragging on it.
You have two options:
Either put this script in a plane with a collider or GUI element that is inside the camera and moves with it, or use Input.GetMouseButtonDown, Input.GetMouseButtonUp in the Update function
Related
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 am new to C# and Unity and I'm trying to figure out how to make an animation for an object in unity play when I press a key but I can only make the animation play once, and then it is broken and doesn't work. (I am trying to make an FPS game)
The code I have right now looks like this:
void Start()
{
gameObject.GetComponent<Animator>().enabled = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
gameObject.GetComponent<Animator>().enabled = true;
}
}
When I press run and left click, the animation triggers and does as it is supposed to but when I try to do it again, the animation doesn't work. Can anybody help me change this code so that the animation will work and play every time the button is pressed?
I am assuming your animation is non-looping as if it was looping it would already play back into itself when it is over.
One quick note I would have with your code is do not use GetComponent in an Update function as it is quite costly. An easy way to get an animation state to reset is to enable and disable it, however I am assuming you want to have more animations than shooting. You would want to look into what is called an Animation Tree or a Blend Tree and add States to your animation. Examples of states would be an Idle, Walk, Run, Shoot, Crouch, etc. I would consider researching Animation Trees and Blend Trees to get a full animation cycle in.
Once you get a State machine working, I would have the enter go to an Idle state, then either set a transition Bool or directly switch the animation in code.
// when you serialize a private field, it will appear in
// the inspector so you can drag in the reference in the editor
[SerializeField] private Animator anim = null;
private void Start()
{
anim.enabled = false;
}
private void Update
{
if(Input.GetButtonDown("Fire1")
{
Shoot();
if(!anim.enabled)
anim.enabled;
else
anim.Play("AnimationStateName", -1, 0f);
}
}
I have not tested the code, but I believe this would work with your setup. I would still strongly advise to not do this and look into Trees. After implementing the tree, instead of calling using the enabled, just use the line anim.Play("AnimationStateName", -1, 0f) or you can do anim.SetBool(isAttacking, true) if you set your state to transition from Idle/Run/Walk/etc. to Attacking when the isAttacking bool is set to true.
I found a video that might help you out. I do not want to post a full explanation to animation states and blend trees, just point you in the right direction to a better approach.
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.
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.
I am a total beginner at Unity3d. I have some background in android programming, but no C# experience whatsoever. The first thing I am trying to do is to create a clone of flappy bird game, called flappy plane, according to this tutorial
http://anwell.me/articles/unity3d-flappy-bird/
The problem is, when I tried to write a script that allows player to move (player.cs) with the code
using UnityEngine;
using System.Collections;
public class player: MonoBehaviour {
public Vector2 jumpForce = new Vector2(0,300);
public Vector2 jumpForce2 = new Vector2(0,-300);
// Use this for initialization
// Update is called once per frame
void Update () {
if (Input.GetKeyUp("space")){
Rigidbody2D.velocity = Vector2.zero;
Rigidbody2D.AddForce(jumpForce);
}
}
}
I get an error "An Object reference is required to access non-static member 'UnityEngine.Rigidbody2D.velocity'". I have googled that and it is suggested to access Rigidbody2d with GetComponent().velocity,
so I changed
Rigidbody2D.velocity = Vector2.zero;
Rigidbody2D.AddForce(jumpForce);
with
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
GetComponent<Rigidbody2D>().AddForce(jumpForce);
The error is gone and I am able to add the script to the object, still I don`t get the desired action - after I hit play the object turns invisible and just falls down, does not react to spacebar button. What am I doing wrong?
Thanks for the answer.
It's possible that you're not adding enough force to have the object move upwards.
There's technically nothing wrong with your code. (Although you do have somethings mixed up in your question). The problem is in the fact that you're not adding ANY force upwards every single frame.
Essentially, at the moment, your player object is in free-fall the instant you hit the play button, and you're adding a minuscule force to the player only on the frames that the space bar is pressed.
To solve this, here's what you should be doing
Add an upward force to counter-act the force of gravity every frame. You can do this in two ways.
a. Set the rigidbody's velocity.y to 0 BEFORE detecting the space bar (this is really a hacky way, but it'll suffice and doesn't need any more code)
b. Add an upward force to the player which will nullify the effect of gravity. Just use F = mg to get the value of force you'd need to add.
You could, alternatively set the isKinematic property to true by default on the Player's rigidbody, set it to false on pressing the space bar, and back to true after a few frames (5 - 6 frames)
make sure your player object and the ground both have BoxCollider2D colliders to keep above ground.
you could keep a reference stored for the rigidBody like Rigidbody2D myRigidbody;
then in start put myRigidbody = GetComponent<Rigidbody2D>(); then you would use like myRigidbody.AddForce(jumpForce); your jumpForce2 though is shooting your player downward you should not need it in a jump as the physics and gravity will apply with the rigidbody.
incase your input is not set up in the project settings try to fire the jump with
Input.GetKeyDown(KeyCode.Space);