unity3d

Unity Input.GetKeyDown(KeyCode.Space) not detecting key Press

爷,独闯天下 提交于 2021-02-05 05:33:24
问题 I'm learning Unity but my key press isn't being detected using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { public Rigidbody myBody; private float time = 0.0f; private bool isMoving = false; private bool isJumpPressed = false; void Start(){ myBody = GetComponent<Rigidbody>(); } void Update() { isJumpPressed = Input.GetKeyDown(KeyCode.Space); Debug.Log(isJumpPressed); } void FixedUpdate(){ if(isJumpPressed){ myBody.velocity =

Unity Input.GetKeyDown(KeyCode.Space) not detecting key Press

人走茶凉 提交于 2021-02-05 05:33:00
问题 I'm learning Unity but my key press isn't being detected using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { public Rigidbody myBody; private float time = 0.0f; private bool isMoving = false; private bool isJumpPressed = false; void Start(){ myBody = GetComponent<Rigidbody>(); } void Update() { isJumpPressed = Input.GetKeyDown(KeyCode.Space); Debug.Log(isJumpPressed); } void FixedUpdate(){ if(isJumpPressed){ myBody.velocity =

Unity开发——DoTween 方法和队列动画

烂漫一生 提交于 2021-02-05 03:07:59
项目中经常用到物体移动,旋转,控制等已经自己来代码实现,代码制作动画也可以使用Dotween来实现。 Dotween提供了很多方法可以使用: using UnityEngine; using System.Collections; using DG.Tweening; using UnityEngine.UI; public class TestDoTween : MonoBehaviour { int number = 0; // Use this for initialization void Start () { //FunctionOne(); //FunctionTwo(); //FunctionSequence(); FunctionSet(); } // 创建 DOTween 实例 方法 #region 方法一 类方法 private void FunctionOne() { // 创建一个 Tweener 是自身坐标 一秒内 移动到 坐标 Vector3(5, 5, 5) 位置 Tween tween = DOTween.To(() => transform.position, r => transform.position = r, new Vector3(5, 5, 5), 1); // 创建一个 Tweener 对象, 另 number的值在 5 秒内变化到

Store References to Scene Objects in prefabs?

ⅰ亾dé卋堺 提交于 2021-02-04 20:59:19
问题 Look i have a player and enemy in my scene. i am using vector2.movetowards to move my enemy towards my player but my enemy is a prefab so i have to give it a reference of my player in the inspector as a target but when i delete enemy from scene because its a prefab it deletes the reference of the player what should i do here is my code thanks i just want that how to permanently store the reference of this target in prefab using UnityEngine; using System.Collections; public class moveTowards :

Why does ReSharper not suggest using null propagation for both of these blocks?

安稳与你 提交于 2021-02-04 18:08:14
问题 ReSharper suggests using null propagation for the "damageable" if block, but for the "forceVelocityCalculator" one there is no suggestion. void Damage(Collider hitCollider) { var damageable = hitCollider.GetComponent<IDamageable>(); if (damageable != null) { damageable.TakeDamage(_damage); } } void Push(Collider hitCollider) { var forceVelocityCalculator = hitCollider.GetComponent<ForceVelocityCalculator>(); if (forceVelocityCalculator != null) { forceVelocityCalculator.Push(_pushForce,

Why does ReSharper not suggest using null propagation for both of these blocks?

不羁岁月 提交于 2021-02-04 18:07:40
问题 ReSharper suggests using null propagation for the "damageable" if block, but for the "forceVelocityCalculator" one there is no suggestion. void Damage(Collider hitCollider) { var damageable = hitCollider.GetComponent<IDamageable>(); if (damageable != null) { damageable.TakeDamage(_damage); } } void Push(Collider hitCollider) { var forceVelocityCalculator = hitCollider.GetComponent<ForceVelocityCalculator>(); if (forceVelocityCalculator != null) { forceVelocityCalculator.Push(_pushForce,

Why does ReSharper not suggest using null propagation for both of these blocks?

China☆狼群 提交于 2021-02-04 18:05:43
问题 ReSharper suggests using null propagation for the "damageable" if block, but for the "forceVelocityCalculator" one there is no suggestion. void Damage(Collider hitCollider) { var damageable = hitCollider.GetComponent<IDamageable>(); if (damageable != null) { damageable.TakeDamage(_damage); } } void Push(Collider hitCollider) { var forceVelocityCalculator = hitCollider.GetComponent<ForceVelocityCalculator>(); if (forceVelocityCalculator != null) { forceVelocityCalculator.Push(_pushForce,

Check if an enum flag contains a certain flag value

柔情痞子 提交于 2021-02-04 16:32:07
问题 In C# (using Unity working on a game) I have an enum with the [Flag] attribute. I have instantiated it twice. I would like a way to compare the two enums. Specifically if enum A (which will have multiple flags) contains a flag from enum B (which will only ever be assigned a single flag). I am not trying to compare a single instantiated enum to a single flag (this has been answered multiple times). I suspect I could do this by dumping values with GetValue and comparing those values on a

Unity: AddExplosionForce to 2D

本秂侑毒 提交于 2021-02-04 16:31:57
问题 I want to change my script into a 2D c# script. I know that AddExplosionForce is not a member of UnityEngine.Rigidbody2D so how can I change this in to a 2D script and still have the same force applied fro all directions. (basically do the same but in 2D) Thank you! Here's my script: # pragma strict var explosionStrength : float = 100; function OnCollisionEnter(_other: Collision) { if (_other.collider.gameObject.name == "Bouncy object") _other.rigidbody.AddExplosionForce(explosionStrength,

How do I pass variable from one script to another C# Unity 2D?

你。 提交于 2021-02-04 15:30:30
问题 For example I have a variable (public static float currentLife) in script "HealthBarGUI1" and I want to use this variable in another script. How do I pass variable from one script to another C# Unity 2D? 回答1: You could do something like this, because currentLife is more related to the player than to the gui: class Player { private int currentLife = 100; public int CurrentLife { get { return currentLife; } set { currentLife = value; } } } And your HealthBar could access the currentLife in two