Why is this audio source not playing anything? - c#

Im trying to write a script where the audio source plays an audioclip array with 4 sounds in it. The sounds are gonna be played faster and louder when my character is sprinting and quieter and slower when my character is crouching. My problem is that the audiosource doesn't play the audioclip. I've checked it multiple times and I really can't find the problem.
`public class PlayerFootsteps : MonoBehaviour
{
private AudioSource footstepSound;
[SerializeField]
private AudioClip[] footstepClip;
private CharacterController charController;
[HideInInspector]
public float VolumeMin, VolumeMax;
private float accumulatedDistance;
[HideInInspector]
public float stepDistance;
void Awake()
{
footstepSound = GetComponent<AudioSource>();
charController = GetComponentInParent<CharacterController>();
}
void Update()
{
CheckToPlayFootstepSounds();
}
void CheckToPlayFootstepSounds()
{
if (!charController.isGrounded)
{
return;
}
if (charController.velocity.magnitude > 0)
{
accumulatedDistance += Time.deltaTime;
if (accumulatedDistance > stepDistance)
{
footstepSound.volume = Random.Range(VolumeMin, VolumeMax);
footstepSound.clip = footstepClip[Random.Range(0, footstepClip.Length)];
footstepSound.Play();
accumulatedDistance = 0f;
}
else
{
accumulatedDistance = 0f;
}
}
}
}`
public class PlayerSprintAndCrouch : MonoBehaviour
{
private PlayerMovement playerMovement;
public float sprintSpeed = 10f, moveSpeed = 5f, crouchSpeed = 2f;
[SerializeField]
private Transform lookRoot;
private float standHeight = 1.6f, crouchHeight = 1f;
private bool isCrouching;
private PlayerFootsteps playerFootsteps;
private float sprintVolume = 1f;
private float crouchVolume = 0.1f;
private float walkVolumeMin = 0.2f, walkVolumeMax = 0.6f;
private float walkStepDistance = 0.4f, sprintStepDistance = 0.25f, crouchStepDistance = 0.5f;
void Awake()
{
playerMovement = GetComponent<PlayerMovement>();
playerFootsteps = GetComponentInChildren<PlayerFootsteps>();
}
void Start()
{
playerFootsteps.VolumeMin = walkVolumeMin;
playerFootsteps.VolumeMax = walkVolumeMax;
playerFootsteps.stepDistance = walkStepDistance;
}
// Update is called once per frame
void Update()
{
Sprint();
Crouch();
}
void Sprint()
{
if (Input.GetKeyDown(KeyCode.LeftShift) && !isCrouching)
{
playerMovement.speed = sprintSpeed;
playerFootsteps.stepDistance = sprintStepDistance;
playerFootsteps.VolumeMin = sprintVolume;
playerFootsteps.VolumeMax = sprintVolume;
}
if (Input.GetKeyUp(KeyCode.LeftShift) && !isCrouching)
{
playerMovement.speed = moveSpeed;
playerFootsteps.stepDistance = walkStepDistance;
playerFootsteps.VolumeMin = walkVolumeMin;
playerFootsteps.VolumeMax = walkVolumeMax;
}
}
void Crouch()
{
if (Input.GetKeyDown(KeyCode.LeftControl))
{
if (isCrouching)
{
lookRoot.localPosition = new Vector3(0f, standHeight, 0f);
playerMovement.speed = moveSpeed;
isCrouching = false;
}
else
{
lookRoot.localPosition = new Vector3(0f, crouchHeight, 0f);
playerMovement.speed = crouchSpeed;
playerFootsteps.stepDistance = crouchStepDistance;
playerFootsteps.VolumeMin = crouchVolume;
playerFootsteps.VolumeMax = crouchVolume;
isCrouching = true;
}
}
}
}

The problem is that you are setting the accumulatedDistance to 0, This should fix your problem:
void CheckToPlayFootstepSounds()
{
if (!charController.isGrounded)
{
return;
}
if (charController.velocity.magnitude > 0)
{
accumulatedDistance += Time.deltaTime;
if (accumulatedDistance > stepDistance)
{
footstepSound.volume = Random.Range(VolumeMin, VolumeMax);
footstepSound.clip = footstepClip[Random.Range(0, footstepClip.Length)];
footstepSound.Play();
accumulatedDistance = 0f;
}
else
{
// accumulatedDistance = 0f; Remove This
}
}
}

Related

problem with mirror(particle system) unity, pls help:)

I have a problem with mirror, I'm making a online strategy game and you have buildings in the game, with those buildings you can make soldiers, now the buildings have a particles sim to work while they make more solider, but only the server can see the particles sim of its own buildings and the opponents, any ideas why?
that's the code for building smoke:
#region data
[SerializeField] private ParticleSystem[] Explotion = new ParticleSystem[2];
[SerializeField] private Unit UnitPrefab;
[SerializeField] private Transform position;
[SerializeField] private Health MyHp;
[SerializeField] private Image ProgressBar = null;
[SerializeField] private TMP_Text QuaeText = null;
[SerializeField] private float QDurition = 5f;
[SerializeField] private float SpaceSpawn = 7f;
[SerializeField] private int QMax = 5;
[SerializeField] private float progressImageVelocity = 2;
bool played = false;
[SyncVar(hook = nameof(UpdateQText))] private int QuadeUnits;
[SyncVar] private float Timer = 0f;
private ParticleSystem Smoke = null;
private RTSPlayer player;
private float QProgress;
#endregion
//[ServerCallback]
private void UpdateQText(int OldQ,int NewQ)
{
QuaeText.text = NewQ.ToString();
}
private void Update()
{
if(Smoke == null)
{
Smoke = GetComponentInChildren<ParticleSystem>();
}
if(player == null)
{
player = NetworkClient.connection.identity.GetComponent<RTSPlayer>();
}
if (isServer)
{
ProduceUnits();
}
if (isClient)
{
UpdateTimer();
}
if(QuadeUnits <= 0)
{
QuadeUnits = 0;
Timer = 0;
QProgress = 0;
ProgressBar.fillAmount = 0;
}
}
[Server]
private void ProduceUnits()
{
if(QuadeUnits == 0)
{
try
{
Smoke.Stop();
}
catch
{
}
played = false;
return;
}
try
{
if (!played)
{
print("playing");
Smoke.Play();
played = true;
}
}
catch
{
}
Timer += Time.deltaTime;
if(Timer < QDurition) { return; }
GameObject instance1 = Instantiate(UnitPrefab.gameObject, position.position, position.rotation);
NetworkServer.Spawn(instance1, connectionToClient);
Vector3 SpawnPlace = gameObject.transform.position * SpaceSpawn;
SpawnPlace.y = 0;
Movment ClearSpot = instance1.gameObject.GetComponent<Movment>();
ClearSpot.ServerMoveUnit(SpawnPlace);
instance1.gameObject.GetComponent<HPDispaly>().SetEnacled(false);
QuadeUnits--;
Timer = 0f;
}
This wasn't really useful but I found other code that helped me with this problem, if someone has the same problem here's the solution:
Solution
You need to make a syncVar bool and update the animator using it and not with just regular false/true calls, here's my new code:
#region data
[SyncVar(hook = nameof(HandleBuilding))] private bool IsBuildng = false;
[SyncVar(hook = nameof(HandleExplotion))] private bool IsPlaying = false;
[SerializeField] private ParticleSystem[] Explotion = new ParticleSystem[2];
[SerializeField] private Unit UnitPrefab;
[SerializeField] private Transform position;
[SerializeField] private Health MyHp;
[SerializeField] private Image ProgressBar = null;
[SerializeField] private TMP_Text QuaeText = null;
[SerializeField] private float QDurition = 5f;
[SerializeField] private float SpaceSpawn = 7f;
[SerializeField] private int QMax = 5;
[SerializeField] private float progressImageVelocity = 2;
bool played = false;
[SyncVar(hook = nameof(UpdateQText))] private int QuadeUnits;
[SyncVar] private float Timer = 0f;
[SerializeField] private ParticleSystem Smoke = null;
private RTSPlayer player;
private float QProgress;
#endregion
//[ServerCallback]
private void UpdateQText(int OldQ,int NewQ)
{
QuaeText.text = NewQ.ToString();
}
private void HandleBuilding(bool OldBool,bool NewBool)
{
Smoke.gameObject.SetActive(IsBuildng);
}
private void Update()
{
if(Smoke == null)
{
Smoke = GetComponentInChildren<ParticleSystem>();
}
if(player == null)
{
player = NetworkClient.connection.identity.GetComponent<RTSPlayer>();
}
if (isServer)
{
ProduceUnits();
}
if (isClient)
{
UpdateTimer();
}
if(QuadeUnits <= 0)
{
QuadeUnits = 0;
Timer = 0;
QProgress = 0;
ProgressBar.fillAmount = 0;
}
}
[Server]
private void ProduceUnits()
{
if(QuadeUnits == 0)
{
IsBuildng = false;
played = false;
return;
}
else
{
IsBuildng = true;
}
IsBuildng = true;
Timer += Time.deltaTime;
if(Timer < QDurition) { return; }
GameObject instance1 = Instantiate(UnitPrefab.gameObject, position.position, position.rotation);
NetworkServer.Spawn(instance1, connectionToClient);
Vector3 SpawnPlace = gameObject.transform.position * SpaceSpawn;
SpawnPlace.y = 0;
Movment ClearSpot = instance1.gameObject.GetComponent<Movment>();
ClearSpot.ServerMoveUnit(SpawnPlace);
instance1.gameObject.GetComponent<HPDispaly>().SetEnacled(false);
QuadeUnits--;
Timer = 0f;
}

OnPointerEnter and OnPointerExit not being triggered Unity

Alright so basically the issue that I've been having is that for some reason a GameObject is interfering with the OnPointerEnter function. I'm pretty sure that OnPointerEnter detects only UI. So that's why I'm extremely confused when seeing that a specific GameObject in this case the PlayerLogic GameObject (which you can see in the screenshot) is for some reason interfering with the detection of UI elements. The reason I believe it is this specific GameObject is because once I do PlayerLogic.SetActive(false); OnPointerEnter starts to work again, and I'm also sure that it isn't any of the children of PlayerLogic because I've tried turning them off specifically and it still didn't work.
Inspector of the PlayerLogic object
Hierarchy
The code I'm using to test OnPointerEnter
After some testing I've realized that its the specific issue lies within the Player script on the PlayerLogic GameObject. Now what confuses me is that once I turn off the Player component OnPointer doesn't work, but if I were to remove the Player component completely from the PlayerLogic GameObject OnPointerEnter works.
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections;
using System.Collections.Generic;
public class Player : MonoBehaviour, TakeDamage {
[SerializeField] private Animator playerAnimator;
[SerializeField] private Transform mainCameraTransform;
private bool isRunning = false;
[SerializeField] private CharacterController controller;
public float speed = 10f;
[SerializeField] private float jumpForce = 3f;
[SerializeField] private float gravity = -10000.81f;
Vector3 velocity;
Vector3 desiredMoveDirection;
private float dashSpeed = 30f;
private float mouseX;
private float mouseY;
[SerializeField]
private Transform Target;
[SerializeField]
private Transform player;
private float turnSmoothVelocity;
private float time = 0f;
public bool playerIsAttacking = false;
[SerializeField] private Slider playerHealth, playerMana;
[SerializeField] private TextMeshProUGUI healthText, manaText;
private Vector3 originalSpawnPos;
private bool playerIsDead = false;
[SerializeField] private LayerMask enemyLayerMask;
[SerializeField] private Transform playerLook;
private ShowHPBar obj;
private bool HPBarShown = false;
private bool unshowingHPBar = false;
public bool lookingAtEnemy = false;
public RaycastHit hit;
[SerializeField] private Canvas abilityCanvas;
[SerializeField] private Slider CD1;
[SerializeField] private Slider CD2;
[SerializeField] private Slider CD3;
public List<Ability> currentlyEquippedAbilites = new List<Ability>();
public List<string> abilityTexts = new List<string>();
public float[] abilityCooldowns = new float[3];
private float manaRegenTime;
//public List<Image> abilityImages = new List<Image>();
private void Awake() {
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
private void Start() {
playerHealth.onValueChanged.AddListener(delegate {OnValueChangedHealth(); });
playerMana.onValueChanged.AddListener(delegate {OnValueChangedMana(); });
originalSpawnPos = transform.position;
}
private void Update() {
if (!playerIsDead) {
PlayerMovementAndRotation();
}
PlayerDash();
PlayerRun();
PlayerSeeEnemyHealth();
PlayerActivateAbility();
if (manaRegenTime > 0.5f) {
playerMana.value += playerMana.maxValue/100;
manaRegenTime = 0;
}
playerLook.rotation = mainCameraTransform.rotation;
time += Time.deltaTime;
manaRegenTime += Time.deltaTime;
#region Ability Cooldowns
if (currentlyEquippedAbilites.Count > 0) {
if (currentlyEquippedAbilites[0].cooldown <= abilityCooldowns[0])
{
currentlyEquippedAbilites[0].isOnCooldown = false;
abilityCooldowns[0] = 0;
CD1.value = 0;
}
else if (currentlyEquippedAbilites[0].isOnCooldown) {
abilityCooldowns[0] += Time.deltaTime;
CD1.value = currentlyEquippedAbilites[0].cooldown - abilityCooldowns[0];
}
}
if (currentlyEquippedAbilites.Count > 1) {
if (currentlyEquippedAbilites[1].cooldown <= abilityCooldowns[1])
{
currentlyEquippedAbilites[1].isOnCooldown = false;
abilityCooldowns[1] = 0;
CD2.value = 0;
}
else if (currentlyEquippedAbilites[1].isOnCooldown) {
abilityCooldowns[1] += Time.deltaTime;
CD2.value = currentlyEquippedAbilites[1].cooldown - abilityCooldowns[1];
}
}
if (currentlyEquippedAbilites.Count > 2) {
if (currentlyEquippedAbilites[2].cooldown <= abilityCooldowns[2])
{
currentlyEquippedAbilites[2].isOnCooldown = false;
abilityCooldowns[2] = 0;
CD3.value = 0;
}
else if (currentlyEquippedAbilites[2].isOnCooldown) {
abilityCooldowns[2] += Time.deltaTime;
CD3.value = currentlyEquippedAbilites[2].cooldown - abilityCooldowns[2];
}
}
#endregion
}
private void PlayerRun() {
if (Input.GetKey(KeybindsScript.RunKey) && (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)) {
playerAnimator.SetInteger("isRunning", 1);
playerAnimator.SetInteger("isIdle", 0);
playerAnimator.SetInteger("isWalking", 0);
speed = 15f;
}
else if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0) {
playerAnimator.SetInteger("isWalking", 1);
playerAnimator.SetInteger("isIdle", 0);
playerAnimator.SetInteger("isRunning", 0);
speed = 10f;
}
else {
playerAnimator.SetInteger("isRunning", 0);
playerAnimator.SetInteger("isWalking", 0);
playerAnimator.SetInteger("isIdle", 1);
speed = 10f;
}
}
private void PlayerMovementAndRotation() {
bool isGrounded = controller.isGrounded;
if (isGrounded && velocity.y < 0) {
velocity.y = -2f;
}
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 moveDir = (transform.right*horizontal+transform.forward*vertical).normalized;
controller.Move(moveDir*Time.deltaTime*speed);
transform.eulerAngles = new Vector3(0f, mainCameraTransform.eulerAngles.y, 0f);
if (Input.GetKeyDown(KeybindsScript.JumpKey) && isGrounded) {
velocity.y = Mathf.Sqrt(jumpForce * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
private void PlayerDash() {
if (Input.GetKeyDown(KeybindsScript.DashKeybind) && isRunning == false) {
if (Input.GetKey(KeybindsScript.MovementKeyBackward)) {
StartCoroutine(PlayerDashTiming(2));
}
else if (Input.GetKey(KeybindsScript.MovementKeyRight)) {
StartCoroutine(PlayerDashTiming(3));
}
else if (Input.GetKey(KeybindsScript.MovementKeyLeft)) {
StartCoroutine(PlayerDashTiming(4));
}
else {
StartCoroutine(PlayerDashTiming(1));
}
}
}
private void PlayerSeeEnemyHealth() {
if (Physics.Raycast(playerLook.position, playerLook.forward, out hit, 1000f, enemyLayerMask)) {
obj = hit.transform.gameObject.GetComponent<ShowHPBar>();
if (obj != null && !HPBarShown && !unshowingHPBar) {obj.ShowHPBarFunction(); HPBarShown = true;}
lookingAtEnemy = true;
}
else {
if (obj != null && HPBarShown) {StartCoroutine(UnShowHPBar(obj)); HPBarShown = false;}
}
}
public void PlayerEquipAbility(Ability ability, int place) {
if (currentlyEquippedAbilites.Count < place) {currentlyEquippedAbilites.Add(ability);}
else {currentlyEquippedAbilites[place-1] = ability;}
//if (abilityImages.Count < place) {abilityImages.Add(ability.icon);}
//else {abilityImages.Add(ability.icon);}
if (abilityTexts.Count < place) {abilityTexts.Add(ability.name);}
else {abilityTexts[place-1] = ability.name;}
for (int i=0;i < abilityTexts.Count;++i) {
abilityCanvas.transform.GetChild(i).GetChild(0).GetComponent<TextMeshProUGUI>().text = abilityTexts[i];
abilityCanvas.transform.GetChild(i).GetChild(1).GetComponent<Slider>().maxValue = ability.cooldown;
}
}
private void PlayerActivateAbility() {
if (Input.GetKeyDown(KeyCode.Alpha1)) {
if (currentlyEquippedAbilites[0] != null) {
if (currentlyEquippedAbilites[0].manaCost <= playerMana.value && !currentlyEquippedAbilites[0].isOnCooldown) {
ParticleEffect pe = currentlyEquippedAbilites[0].script.gameObject.GetComponent<ParticleEffect>();
playerMana.value -= currentlyEquippedAbilites[0].manaCost;
currentlyEquippedAbilites[0].isOnCooldown = true;
if (pe != null) {pe.PlayAnimation();}
}
}
}
}
public void OnValueChangedHealth() {
healthText.text = playerHealth.value + "/" + PlayerStats.HealthPoints;
}
public void OnValueChangedMana() {
manaText.text = playerMana.value + "/" + PlayerStats.ManaAmount;
}
public void TakeDamageFunction(float damage) {
playerHealth.value -= damage;
if (playerHealth.value <= 0) {
StartCoroutine(PlayerDeath());
}
}
IEnumerator PlayerDashTiming(int x) {
isRunning = true;
float time = 0f;
Vector3 savedVector = Vector3.zero;
switch (x) {
case 1:
savedVector = transform.forward;
break;
case 2:
savedVector = -transform.forward;
break;
case 3:
savedVector = transform.right;
break;
case 4:
savedVector = -transform.right;
break;
}
while(time < .3f)
{
time += Time.deltaTime;
controller.Move(savedVector * dashSpeed * Time.deltaTime);
yield return null;
}
yield return new WaitForSeconds(1.5f);
isRunning = false;
}
IEnumerator PlayerDeath() {
//Respawn
playerIsDead = true;
playerAnimator.enabled = false;
yield return new WaitForSeconds(1f);
playerHealth.value = 100;
transform.position = originalSpawnPos;
yield return new WaitForSeconds(0.1f);
playerIsDead = false;
playerAnimator.enabled = true;
}
IEnumerator UnShowHPBar(ShowHPBar obj) {
unshowingHPBar = true;
yield return new WaitForSeconds(1.5f);
obj.ShowHPBarFunction();
unshowingHPBar = false;
}
}
This is the Player.cs script.
I'm pretty sure that OnPointerEnter detects only UI.
No.
It works "out of the box" on UI because by default every Canvas contains a GraphicsRaycaster component which is then used and handled by the EventSystem.
For non-UI 3D objects you have to make sure
your objects have 3D Colliders
the Colliders are on the same object as the IPointerXY interfaces
on your Camera there is a PhysicsRayster component
For non-UI 2D objects quite similar
your objects have a 2D Collider
the colliders are on the same object as the IPointerXY interfaces
the Camera has a Physics2DRaycaster component
Note that it is possible that any other collider or in general raycast blocking object is between your input and the target object which would also prevent the OnPointerXY to be triggered on your objects.
The CharacterController
is simply a capsule shaped Collider
which can be told to move in some direction from a script.
which is probably blocking the input.
Now with your Player code:
You do
Cursor.lockState = CursorLockMode.Locked;
in Awake so even if you turn it off afterwards this already took effect.

Collision2D is not called

I know that this is a fairly regular question however having tried the solutions provided I have not been having much luck. Trying to get some object to collide in unity which worked, however the OnCollision2d call is not being made after they collide. Hoping to get some help on why this is happening.
The two classes provided here
[RequireComponent(typeof(SpriteRenderer))]
[RequireComponent(typeof(Rigidbody2D))]
public class Missile: MonoBehaviour
{
public bool dead = false, hostile = true;
public float speed = 30, rotatingSpeed= 200;
public GameObject target;
private BoxCollider2D bc;
private Rigidbody2D rb;
private SpriteRenderer sr;
private GameObject go;
public Missile() { Debug.Log("Constructed"); }
private void OnEnable()
{
Debug.Log("Missile Launched");
go = new GameObject("Missile");
go.tag = "Missile";
target = GameObject.FindGameObjectWithTag("Ship");
rb = go.AddComponent<Rigidbody2D> ();
sr = go.AddComponent<SpriteRenderer> ();
bc = go.AddComponent<BoxCollider2D>();
bc.size = new Vector2(2f, 2f);
//bc.isTrigger = true;
go.transform.position = new Vector3(0,0, 11);
sr.sprite = SpriteManager.current.GetSprite("Ships", "missile1");
}
private void FixedUpdate()
{
//sr.sprite = SpriteManager.current.GetSprite("Missiles", "missile1");
//Debug.Log(target);
if (!target) return;
var point2Target = (Vector2) go.transform.position - (Vector2) target.transform.position;
point2Target.Normalize();
float value = Vector3.Cross(point2Target, go.transform.right).z;
rb.angularVelocity = rotatingSpeed * value;
//Vector2 lastVel = rb.velocity;
rb.velocity = go.transform.right * speed;
//rb.AddForce(go.transform.right * speed);
}
public void setPosition(int x, int y)
{
go.transform.position = new Vector3(x,y,11);
}
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("hit");
Destroy (go, 0.02f);
}
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("hit");
Destroy (go, 0.02f);
}
}
And the second class
[RequireComponent(typeof(SpriteRenderer))]
[RequireComponent(typeof(Rigidbody2D))]
public class Ship: MonoBehaviour
{
public int hull = 100, type = 1;
public bool dead = false;
public float speed = 5, rotatingSpeed= 200;
public GameObject target;
private BoxCollider2D bc;
private Rigidbody2D rb;
private SpriteRenderer sr;
private GameObject go;
public Ship() { Debug.Log("Constructor"); }
private void OnEnable()
{
Debug.Log("Ship Built");
go = new GameObject("Ship");
go.tag = "Ship";
//target = GameObject.FindGameObjectWithTag("Player");
rb = go.AddComponent<Rigidbody2D> ();
sr = go.AddComponent<SpriteRenderer> ();
bc = go.AddComponent<BoxCollider2D>();
bc.size = new Vector2(2f, 2f);;
bc.isTrigger = true;
go.transform.position = new Vector3(0,0,11);
sr.sprite = SpriteManager.current.GetSprite("Ships", "ship2");
//sr.sprite.transform.localScale = anyWidth;
//sr.sprite = SpriteManager.current.GetSprite("Missiles", "missile1");
}
void FixedUpdate ()
{
//Debug.Log(target);
GameObject[] missiles = GameObject.FindGameObjectsWithTag("Missile");
/*foreach (GameObject missile in missiles)
{
Vector2 point2Target = ((Vector2)go.transform.position - (Vector2) missile.transform.position);
if(point2Target.magnitude < 50)
{
point2Target.Normalize();
Flak flak = gameObject.AddComponent<Flak>();
flak.setPosition(go.transform.position);
point2Target.x = point2Target.x * -1;
point2Target.y = point2Target.y * -1;
flak.setVelocity(point2Target * 50);
break;
}
}*/
if (target != null)
{
var point2Target = (Vector2) go.transform.position - (Vector2) target.transform.position;
point2Target.Normalize();
float value = Vector3.Cross(point2Target, go.transform.right).z;
rb.angularVelocity = rotatingSpeed * value;
rb.velocity = go.transform.right * speed;
//Vector2 lastVel = rb.velocity;
//rb.AddForce(go.transform.right * speed;);
}
}
public void setPosition(int x, int y) { go.transform.position = new Vector3(x,y,11); }
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("hit");
Destroy (go, 0.02f);
}
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("hit");
Destroy (go, 0.02f);
}
}
and finally the invoking
if (Input.GetKeyUp("i"))
{
var ship = gameObject.AddComponent<Ship>();
ship.setPosition((int)currFramePosition.x, (int)currFramePosition.y);
}
if (Input.GetKeyUp("v"))
{
var missile = gameObject.AddComponent<Missile>();
missile.setPosition((int)currFramePosition.x, (int)currFramePosition.y);
Debug.Log(missile);
}
Some help would be appreciated, thanks.

NPC Follower - how to implement script

my goal is to have an NPC follow ThePlayer. I had a functioning NPCFollower script attached to a character with two animations, Idle and Walk.
I'm wondering if anyone knows how I can implement the NPCFollower script with the'SimpleCharacterControl'script which is attached to a character with an animator with multiple animations. I would like to call on two of the animations, walk and idle. I highly appreciate the help!
NPCFollow' script
public class NPCFollow : MonoBehaviour {
public GameObject ThePlayer;
public float TargetDistance;
public float AllowedDistance = 5;
public GameObject TheNPC;
public float FollowSpeed;
public RaycastHit Shot;
void Update () {
transform.LookAt(ThePlayer.transform);
if (Physics.Raycast(transform.position,transform.TransformDirection(Vector3.forward),out Shot))
{
TargetDistance = Shot.distance;
if(TargetDistance >= AllowedDistance)
{
FollowSpeed = 0.02f;
TheNPC.GetComponent<Animation>().Play("Walk");
}
else
{
FollowSpeed = 0;
TheNPC.GetComponent<Animation>().Play("Idle");
}
}
}
}
'SimpleCharacterControl'script
public class SimpleCharacterControl : MonoBehaviour {
private enum ControlMode
{
Tank,
Direct
}
[SerializeField] private float m_moveSpeed = 2;
[SerializeField] private float m_turnSpeed = 200;
[SerializeField] private float m_jumpForce = 4;
[SerializeField] private Animator m_animator;
[SerializeField] private Rigidbody m_rigidBody;
[SerializeField] private ControlMode m_controlMode = ControlMode.Direct;
private float m_currentV = 0;
private float m_currentH = 0;
private readonly float m_interpolation = 10;
private readonly float m_walkScale = 0.33f;
private readonly float m_backwardsWalkScale = 0.16f;
private readonly float m_backwardRunScale = 0.66f;
private bool m_wasGrounded;
private Vector3 m_currentDirection = Vector3.zero;
private float m_jumpTimeStamp = 0;
private float m_minJumpInterval = 0.25f;
private bool m_isGrounded;
private List<Collider> m_collisions = new List<Collider>();
private void OnCollisionEnter(Collision collision)
{
ContactPoint[] contactPoints = collision.contacts;
for(int i = 0; i < contactPoints.Length; i++)
{
if (Vector3.Dot(contactPoints[i].normal, Vector3.up) > 0.5f)
{
if (!m_collisions.Contains(collision.collider)) {
m_collisions.Add(collision.collider);
}
m_isGrounded = true;
}
}
}
private void OnCollisionStay(Collision collision)
{
ContactPoint[] contactPoints = collision.contacts;
bool validSurfaceNormal = false;
for (int i = 0; i < contactPoints.Length; i++)
{
if (Vector3.Dot(contactPoints[i].normal, Vector3.up) > 0.5f)
{
validSurfaceNormal = true; break;
}
}
if(validSurfaceNormal)
{
m_isGrounded = true;
if (!m_collisions.Contains(collision.collider))
{
m_collisions.Add(collision.collider);
}
} else
{
if (m_collisions.Contains(collision.collider))
{
m_collisions.Remove(collision.collider);
}
if (m_collisions.Count == 0) { m_isGrounded = false; }
}
}
private void OnCollisionExit(Collision collision)
{
if(m_collisions.Contains(collision.collider))
{
m_collisions.Remove(collision.collider);
}
if (m_collisions.Count == 0) { m_isGrounded = false; }
}
void Update () {
m_animator.SetBool("Grounded", m_isGrounded);
switch(m_controlMode)
{
case ControlMode.Direct:
DirectUpdate();
break;
case ControlMode.Tank:
TankUpdate();
break;
default:
Debug.LogError("Unsupported state");
break;
}
m_wasGrounded = m_isGrounded;
}
private void TankUpdate()
{
float v = Input.GetAxis("Vertical");
float h = Input.GetAxis("Horizontal");
bool walk = Input.GetKey(KeyCode.LeftShift);
if (v < 0) {
if (walk) { v *= m_backwardsWalkScale; }
else { v *= m_backwardRunScale; }
} else if(walk)
{
v *= m_walkScale;
}
m_currentV = Mathf.Lerp(m_currentV, v, Time.deltaTime * m_interpolation);
m_currentH = Mathf.Lerp(m_currentH, h, Time.deltaTime * m_interpolation);
transform.position += transform.forward * m_currentV * m_moveSpeed * Time.deltaTime;
transform.Rotate(0, m_currentH * m_turnSpeed * Time.deltaTime, 0);
m_animator.SetFloat("MoveSpeed", m_currentV);
JumpingAndLanding();
}
private void DirectUpdate()
{
float v = Input.GetAxis("Vertical");
float h = Input.GetAxis("Horizontal");
Transform camera = Camera.main.transform;
if (Input.GetKey(KeyCode.LeftShift))
{
v *= m_walkScale;
h *= m_walkScale;
}
m_currentV = Mathf.Lerp(m_currentV, v, Time.deltaTime * m_interpolation);
m_currentH = Mathf.Lerp(m_currentH, h, Time.deltaTime * m_interpolation);
Vector3 direction = camera.forward * m_currentV + camera.right * m_currentH;
float directionLength = direction.magnitude;
direction.y = 0;
direction = direction.normalized * directionLength;
if(direction != Vector3.zero)
{
m_currentDirection = Vector3.Slerp(m_currentDirection, direction, Time.deltaTime * m_interpolation);
transform.rotation = Quaternion.LookRotation(m_currentDirection);
transform.position += m_currentDirection * m_moveSpeed * Time.deltaTime;
m_animator.SetFloat("MoveSpeed", direction.magnitude);
}
JumpingAndLanding();
}
private void JumpingAndLanding()
{
bool jumpCooldownOver = (Time.time - m_jumpTimeStamp) >= m_minJumpInterval;
if (jumpCooldownOver && m_isGrounded && Input.GetKey(KeyCode.Space))
{
m_jumpTimeStamp = Time.time;
m_rigidBody.AddForce(Vector3.up * m_jumpForce, ForceMode.Impulse);
}
if (!m_wasGrounded && m_isGrounded)
{
m_animator.SetTrigger("Land");
}
if (!m_isGrounded && m_wasGrounded)
{
m_animator.SetTrigger("Jump");
}
}
}
1) Get a reference to ThePlayer(Any object you want other objects to follow).
2) Find direction of ThePlayer.
3) In update check for DistanceFromTarget
if (DistanceFromTarget <= distanceUwant)
{
setMovingSpeed=0;
playIdelAnimation();
}
else
{
setMovingSpeed= asmuchyouwant;
playWalkAnimation();
}

I have error in collision on bullet shoot

I have a problem on the bullet collision the enemy
I am using the OnCollisionEnter() method in the Bullet Script
Bullet Script :
public class BulletScript : MonoBehaviour
{
public float TimeForDestory = 10f;
public float Speed = 0.5f;
// Use this for initialization
void Start () {
}
public void OnCollisionEnter(Collision col)
{
Debug.Log("Collision");
if (col.gameObject.tag == "Enemy")
{
Debug.Log("Collision");
}
}
// Update is called once per frame
void Update () {
transform.Translate(0, -Speed, 0);
if (TimeForDestory < 0f)
{
Destroy(gameObject);
}else
{
TimeForDestory -= 0.1f;
}
}
}
The Bullet Not Collision Anything
the bullet is out of the objects because I am using Method Instantiate() in the Player Script
Player Script :
public class Player : MonoBehaviour
{
//Player
//Move
public float defualtSpdPlayer = 1f;
public float plsSpd = 0.1f;
public float maxPlayer = 2f;
private float speed = 0;
//Bullet
public Transform bulletSpawn;
public GameObject bullet;
public Texture ImgBackground;
public Texture ImgWhiteGround;
public Texture ImgReloading;
public float bulletDamge = 1f;
public float bulletSpeed = 0.1f;
public float ReloadTime = 0.5f;
public float ReloadFillAmontX = 2f;
private float reload = 0;
private float reloadFillAmont = 0;
private bool readyToShoot = true;
private string status = "Ready";
//GUI
[HideInInspector]
public bool guiShow = true;
void Start () {
MoveStart();
BulletStart();
}
private void MoveStart()
{
speed = defualtSpdPlayer;
}
private void BulletStart()
{
if(ReloadTime > 1)
{
Debug.LogError("The Reload Time Is Bigger 1");
}
}
void OnGUI()
{
//Verables
float cvReloadingWidth = 150;
float cvReloadingHeight = 150;
float cvReloadngY = Screen.height - cvReloadingHeight;
float cvReloadngX = Screen.width / 2 - 70;
//Rects
Rect cvReloadingImages = new Rect(cvReloadngX, cvReloadngY, cvReloadingWidth, cvReloadingHeight);
Rect cvReloadinglalReloadTime = new Rect(cvReloadngX + 65, cvReloadngY + 75, cvReloadingWidth, cvReloadingHeight);
Rect cvReloadinglalStatus = new Rect(cvReloadngX + 40, cvReloadngY + 50, cvReloadingWidth, cvReloadingHeight);
//Texts
//Values
//Texture
GUI.DrawTexture(cvReloadingImages, ImgBackground);
GUI.DrawTexture(cvReloadingImages, ImgWhiteGround);
//GUI.DrawTexture(cvReloadingImages, ImgReloading);
if (reloadFillAmont <= 0)
{
GUI.skin.label.normal.textColor = Color.green;
GUI.skin.label.fontSize = 25;
GUI.skin.label.fontStyle = FontStyle.Bold;
GUI.Label(cvReloadinglalStatus, status);
GUI.skin.label.fontSize = 15;
GUI.skin.label.fontStyle = FontStyle.Bold;
GUI.Label(cvReloadinglalReloadTime, ReloadTime.ToString());
}
else
{
GUI.skin.label.normal.textColor = Color.red;
GUI.Label(cvReloadinglalStatus, status);
GUI.Label(cvReloadinglalReloadTime, reloadFillAmont.ToString());
}
//GUI
}
void Update () {
Move();
Shoot();
}
private void Move()
{
//Move
if (transform.rotation.y < 180)
{
plsSpeed();
transform.Translate(Input.GetAxis("BW") * speed * Time.deltaTime, 0, Input.GetAxis("FW") * speed * Time.deltaTime);
}
if (transform.rotation.y >= 180)
{
plsSpeed();
transform.Translate(-Input.GetAxis("BW") * speed * Time.deltaTime, 0, -Input.GetAxis("FW") * speed * Time.deltaTime);
}
}
private void plsSpeed()
{
if (speed > maxPlayer)
{
speed = defualtSpdPlayer;
}
else
speed += plsSpd;
}
//Gun Shoot
private void Shoot()
{
if (readyToShoot)
{
if (Input.GetMouseButton(0))
{
Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
readyToShoot = false;
reload = ReloadTime;
status = "Reloading";
}
status = "Ready";
}
else
{
reloadFillAmont = reload * ReloadFillAmontX;
if (reload < 0)
{
readyToShoot = true;
}else
{
reload -= Time.deltaTime;
status = "Reloading";
}
}
}
}
What's the problem in the collision ?
You are using transform.Translate(0, -Speed, 0); to move the bullet, when you use this the bullet is going to move regardless any obstacle/force outside of it. To fix this add a Rigidbody to the bullet and change that line to GetComponent<Rigidbody>().MovePosition(transform.position - Vector3.up * Speed);
Because its a bullet it might be a fast moving object so put the Collision Detection to Continuous on the Rigidbody.

Categories