Unity: Smooth camera movement on UI button click - c#

I'm trying to make the camera move to a certain position after clicking a UI button.
For the positioning, I use empty game objects' (CameraPositionStart and CameraPositionFinish) coordinates.
For some reason, the camera just teleports to the position needed instead of smoothly moving to it.
Here's my code:
using UnityEngine;
using UnityEngine.UI;
public class CameraMover : MonoBehaviour
{
public float speed;
public Button ButtonStartNew;
public GameObject CameraPositionStart;
public GameObject CameraPositionFinish;
private void Update()
{
ButtonStartNew.onClick.AddListener(moveCamera);
}
public void moveCamera()
{
transform.position = Vector3.Lerp(transform.position, CameraPositionFinish.transform.position, Time.deltaTime * speed);
}
}

This behavior is likely caused because of two reasons.
The method Update is executed once every frame. Here every frame you are adding an additional call that should be made when you click the button on the UI. The result of this, I suspect, is that when the several hundred calls to moveCamera all happen at the same time, and despite moveCamera only moving the transform by Lerp(Time.DeltaTime * Speed) -- the object moves closer(since you're lerping) instantly because you're calling it a bunch.
private void Update()
{
ButtonStartNew.onClick.AddListener(moveCamera);
}
Consider only adding one call to moveCamera to the on click event. We can do this in Start or Awake since they're only called once.
private void Start()
{
ButtonStartNew.onClick.AddListener(moveCamera);
}
However this along does not solve the problem. Because if we do that moveCamera will only be called once and the result would be the camera would move very slightly toward the target and stop.
Consider implementing a Coroutine that will move the transform over time(every frame) until it has reached the target.
private void moveCamera()
{
// A coroutine runs every frame until it stops returning values
StartCoroutine(MoveCamera);
}
private IEnumerator MoveCamera()
{
// check the distance and see if we still need to move towards the destination ​
while(Vector3.Distance(transform.position, CameraPositionFinish.transform.position) > 1.0f)
​{
transform.position = Vector3.Lerp(transform.position, CameraPositionFinish.transform.position, Time.deltaTime * speed);
// Return nothing meaningful and wait until next frame​
yield return null;
}
}

Related

How to make the cubes to get close to each other and then back to opposite direction and then back close nonstop?

Like a gate that opens and closes all the time.
I'm using two cubes as gateParts and moving them each one towards the other but even if I'm checking the distance they never stop at 0.1f distance or less they get inside each other and then stop.
Then I want when they are at distance 0.1 or when the "Gate" is close to moving the cubes back so the "Gate" will get open.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gate : MonoBehaviour
{
public Transform[] gateParts;
public float speed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
var dist = Vector3.Distance(gateParts[0].position, gateParts[1].position);
if (dist > 0.1f)
{
gateParts[0].position = Vector3.MoveTowards(gateParts[0].position, gateParts[1].position
, Time.deltaTime * speed);
gateParts[1].position = Vector3.MoveTowards(gateParts[1].position, gateParts[0].position
, Time.deltaTime * speed);
}
}
}
Instead of doing this by script, I would suggest making an animation of this behavior. Then, check off looping for the animation, and then it'll do this non-stop motion you want.

Unity 2d game movement script issue - unable to jump

So basically, after hours of torment trying to create basic movement script for simple platformer game I succeeded, but not quite. Square character is able to move around and jump just ok, but sometimes it won't jump, usually while moving on short distances or, rarely, standing in place and trying to jump. I can't figure out how to fix that. Here is entire script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
private Rigidbody2D rgdb2;
public float movementSpeed;
public float jumpHeight;
private bool isJumping = false;
// Use this for initialization
void Start ()
{
rgdb2 = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
HandleMovement(moveHorizontal);
if (Input.GetKeyDown(KeyCode.Space) && isJumping == false)//by typing Space player jumps, cant double-jump
{
rgdb2.AddForce(new Vector2(rgdb2.velocity.x, 1 * jumpHeight), ForceMode2D.Impulse);
isJumping = true;
Debug.Log("jumped");
}
}
private void HandleMovement(float moveHorizontal)//applying player horizontal controls and customing player's speed by movementSpeed variable
{
rgdb2.velocity = new Vector2(moveHorizontal * movementSpeed, rgdb2.velocity.y);
}
private void OnCollisionEnter2D(Collision2D coll)
{
if (coll.transform.tag == "Platform") //if player is touching object with Platform tag, he can jump
{
Debug.Log("on ground bitch");
isJumping = false;
}
}
}
It may not be that important, but I want to polish this game as much as possilble, even if I don't need to, since it's basically my first game made in Unity3d with C#.
An important thing to keep in mind: Unity3D Engine's inputs are only updated during the time the engine calls Update() methods for your GameObjects.
What this means is that you should not read any type of input in the FixedUpdate() method. Methods like GetKeyDown() and other methods from the Input class which read keyboard/mouse/axis buttons/values should not be called during FixedUpdate(), as their returned values are unreliable.
Due to this, what is probably causing your jump implementation to fail is that the GetKeyDown() method you're calling in FixedUpdate() is returning inconsistent/invalid (false) results, when the user presses the jump key.
Fixing this can be quite simple. I suggest you keeping a boolean variable which keeps track of whether the jump key has been pressed, and gets its value updated during Update(). This should fix your problem.
bool jumpKeyPressed;
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
jumpKeyPressed = true;
else if (Input.GetKeyUp(KeyCode.Space))
jumpKeyPressed = false;
}
private void FixedUpdate()
{
/* Update "moveHorizontal", call HandleMovement(...) here, as you've already done. */
if (jumpKeyPressed && isJumping == false)
{
// IMPORTANT: this prevents the "jump force" from being applied multiple times, while the user holds the Space key
jumpKeyPressed = false;
/* Remaining jumping logic goes here (AddForce, set "isJumping", etc) */
}
}
It's because your logic for jumping is inside FixedUpdate()
When you use GetKeyDown to register input make sure to use Update instead because if you press the key using FixedUpdate it may or may not run during that frame, test it with Update instead.
You already have in comments how Update works, it is called every frame but FixedUpdate according to Unity documentation: This function is called every fixed framerate frame
void Update ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
HandleMovement(moveHorizontal);
if (Input.GetKeyDown(KeyCode.Space) && isJumping == false)//by typing Space player jumps, cant double-jump
{
rgdb2.AddForce(new Vector2(rgdb2.velocity.x, 1 * jumpHeight), ForceMode2D.Impulse);
isJumping = true;
Debug.Log("jumped");
}
}

Input.GetMouseButtonUp is not reliable. (Unity)

I am developing a 2D game in unity. One of the features in the game is to shoot projectiles using the left click of the mouse. Upon releasing the left click the projectile gets fired with certain amount of force that depends on how long the player held the left click.
The problem is that sometimes when I release the left click the game doesnt seem to detect it and the release portion of the code doesnt get executed until I click and release again. I may not sound like a big problem but input reliability will play a fundamental role in this game.
So, is there any way to make mouse input more realiable? I have already tried using Input.GetMouseButtonDown and different kinds of conditionals in order to make it more reliable, but it hasnt worked out. Thank you in advance!
Here is the code I am using:
using UnityEngine;
using System.Collections;
public class ShootPlasma : MonoBehaviour {
//prefab
public Transform bulletPrefab;
//Reloading variables:
public int numberOfBullets;
private int numberOfBulletsRecord;
private bool canShoot=true;
public float timeToReload;
private float timeToReloadRecord;
//direction and bullet Speed variables:
Transform sightPosition;
public Vector3 SpawnRiseVector;
private Vector2 direction;
public float bulletBoost;
private float bulletBoostRecord;
public float MAX_BOOST;
//Arrow Guide
public Transform aimingArrow;
//Helper variables;
private CharacterController2D charControllerScript;
void Start () {
timeToReloadRecord = timeToReload;
numberOfBulletsRecord = numberOfBullets;
charControllerScript = transform.parent.GetComponent<CharacterController2D> ();
bulletBoostRecord = bulletBoost;
sightPosition = GetComponent<Transform> ();
aimingArrow.GetComponentInChildren<Renderer>().enabled=false;
}
// Update is called once per frame
void FixedUpdate () {
if(numberOfBullets<=0){
canShoot=false;
if(!canShoot){
timeToReload-=Time.deltaTime;
//if the waiting time has ended restart variables.
if(timeToReload<=0.0f){
canShoot=true;
timeToReload=timeToReloadRecord;
numberOfBullets=numberOfBulletsRecord;
}
}
}
////////////////////////////////// SHOOTING CODE ////////////////////////////////////////////
///
/// MOUSE DOWN
/////////////////////////////////////////////////////////////////////////////////////////
else if(Input.GetMouseButton(0)&& canShoot && !Input.GetMouseButtonUp(0)){
//show the Arrow Guide:
if(aimingArrow.GetComponentInChildren<Renderer>().enabled!=true){
aimingArrow.GetComponentInChildren<Renderer>().enabled=true;
}
//calculate the distance between the mouse and the sight;
Vector3 mousePositionRelative=Camera.main.ScreenToWorldPoint(Input.mousePosition);
direction= new Vector2(mousePositionRelative.x- sightPosition.position.x,
mousePositionRelative.y- sightPosition.position.y).normalized;
//If Y is less or equal 0:
if(direction.y<=0.0f && direction.x>=0.0f){
direction=new Vector2(1.0f,0.0f);
}
else if(direction.y<=0.0f && direction.x<0.0f){
direction=new Vector2(-1.0f,0.0f);
}
//Rotate the aiming arrow
if(charControllerScript.facingFront){
if(direction.x>=0.0f){
aimingArrow.rotation=Quaternion.Euler(new Vector3(0.0f,0.0f,(Mathf.Asin(direction.y/direction.magnitude))*Mathf.Rad2Deg));
}
else if(direction.x<0.0f){
aimingArrow.rotation=Quaternion.Euler(new Vector3(0.0f,180.0f,(Mathf.Asin(direction.y/direction.magnitude))*Mathf.Rad2Deg));
}
}
else if(!charControllerScript.facingFront){
if(direction.x>=0.0f){
aimingArrow.rotation=Quaternion.Euler(new Vector3(0.0f,180.0f,(Mathf.Asin(direction.y/direction.magnitude))*Mathf.Rad2Deg));
}
else if(direction.x<0.0f){
aimingArrow.rotation=Quaternion.Euler(new Vector3(0.0f,0.0f,(Mathf.Asin(direction.y/direction.magnitude))*Mathf.Rad2Deg));
}
}
Debug.Log(direction);
//Charge
bulletBoost+=Time.deltaTime*bulletBoost;
if(bulletBoost>=MAX_BOOST){
bulletBoost=MAX_BOOST;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// MOUSE UP
/// ///////////////////////////////////////////////////////////////////////////////////////////////
else if(Input.GetMouseButtonUp(0)&& canShoot && !Input.GetMouseButton(0)){
//Hide the Arrow Guide:
if(aimingArrow.GetComponentInChildren<Renderer>().enabled!=false){
aimingArrow.GetComponentInChildren<Renderer>().enabled=false;
}
//Fire
var shootPrefab= Instantiate(bulletPrefab,sightPosition.position+SpawnRiseVector,Quaternion.identity) as Transform;
shootPrefab.GetComponent<Rigidbody2D>().AddForce(direction*bulletBoost);
bulletBoost=bulletBoostRecord;
//Reduce the Ammo by one:
numberOfBullets-=1;
}
}
}
The problem is that you are using FixedUpdate() instead of Update(). FixedUpdate() is called in constant intervals(not necessary each frame). So, the Input.GetMouseButtonUp() may be missed some time between 2 calls of FixedUpdate(). You should better use Update() when you are handling input.

Unity Jump function issue

I've been working on this script for the past day. For some reason my character will not jump as long as it's animator is active. I've got into the animation (there is only one) and removed all references to the animation placing a position anywhere and still the issue presides.
I have discovered that I can make my player jump if I use Co-routine which I'm using. However, I'm still new to using them and I can't work out why my player won't fall to the ground once a force has been added to it. And my player only moves up when the button is clicked. Could someone please take a look at my script and tell me what I'm doing wrong?
public float jumpSpeed = 100.0f;
public float jumpHeight = 2.0f;
public AudioClip jumpSound;
private GameObject pos;
private bool moving;
private bool isJumping;
void Start()
{
}
// Update is called once per frame
void Update ()
{
if(Input.GetMouseButtonDown(0))// && !moving)
{
isJumping = true;
StartCoroutine(JumpPlayer(gameObject.transform.localPosition));
}
else
{
isJumping = false;
}
}
IEnumerator JumpPlayer(Vector3 startPos)
{
Vector3 jump = new Vector3(transform.localPosition.x, jumpHeight, transform.localPosition.z);
float t = 0f;
t += Time.deltaTime / jumpSpeed;
rigidbody.AddForce(Vector3.up * jumpSpeed);
//gameObject.transform.localPosition = Vector3.Lerp(startPos, jump, 0.5f);
//isJumping = false;
yield return null;
}
Firstly, your use of coroutine isn't doing anything in particular - because it only does yield return null at the end, it'll run in a single frame and then exit. You could make it a regular void function and you shouldn't see any change in behaviour.
Removing other redundant code and you have just this:
if(Input.GetMouseButtonDown(0))
{
rigidbody.AddForce(Vector3.up * jumpSpeed);
}
This force is added for only a single frame: the frame where the mouse button is pressed down (if you used Input.GetMouseButton instead, you'd see the force applied for multiple frames).
You say "my player only moves up when the button is clicked" but I'm not clear why that's a problem - perhaps you mean that the player should continue to move up for as long as the button is held, in which case you should refer to my previous paragraph.
The most obvious reasons for the player not falling again are related to the RigidBody component: do you have weight & drag set to suitable values? An easy way to test this would be to position your player some distance from the ground at the start of the scene, and ensure that they fall to the ground when you start the scene.
Another reason might be that you're using the default override of .AddForce in an Update cycle. The default behaviour of this method applies force during the FixedUpdate calls, and you might find that using ForceMode.Impulse or ForceMode.VelocityChange gives you the result you're looking for.

Adding velocity to 2d sprite in c# unity

hi i was wondering if someone could help me fix practice code (i make practice codes before i make the real thing because its just how i roll) it is basically an object that requires the user to click on the screen in order for it to not touch the ground much like flappy bird however although i have applied gravity correctly to the sprite i cannot fix the velocity section (i coded is to every time the user clicks with his mouse or taps the space key the object will move up like flappy bird)
using UnityEngine;
using System.Collections;
public class BirdMovment : MonoBehaviour {
Vector3 Velocity = Vector3.zero;
public Vector3 gravity;
public Vector3 flapVelocity;
public float maxSpeed = 5f;
bool didFlap = false;
// Use this for initialization
void Start () {
}
void update (){
if (Input.GetKeyDown (KeyCode.Mouse0))
{
didFlap = true;
}
}
// Update is called once per frame
void FixedUpdate () {
Velocity += gravity* Time.deltaTime;
if (didFlap) {
didFlap = false;
Velocity += flapVelocity;
}
Velocity = Vector3.ClampMagnitude (Velocity, maxSpeed);
transform.position += Velocity * Time.deltaTime;
}
}
can you please fix the error as every time i set the velocity in unity for the sprite ad run the program the sprite just keeps on falling and no matter how much i click or tap the space key the sprite does not stop falling even if i increase the velocity
First of all, the correct Update function is with a capital U, so Update() instead of update(). Then, since you're not doing anything with physics, you can do everything in Update and not use FixedUpdate at all. So you can remove the didFlap variable and add to Velocity directly inside the if (Input.GetKeyDown ...) block. Furthermore, regarding gravity, you're multiplying it twice with Time.deltaTimethere, so remove the first one. That should get you started.

Categories

Resources