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();
}
Related
if dc is not null i'm using a radius from another script and it's working fine when i'm changing the radius value in the DrawCircle script the transform in RotateAroundTarget is moving smooth to the new radius.
but if dc variable is null i want to use the local radius variable for setting the radius in runtime but when changing the radius the transform is not moving to the new radius.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateAroundTarget : MonoBehaviour
{
public Transform target;
public float rotatingSpeed;
public float movingSpeed;
public Vector3 axis;
public bool randomHeight;
public float setRandomHeight = 1;
public float radius;
public DrawCircle dc;
private float lastRadius;
private bool move = false;
private float t = 0.0f;
public float upperLimit, lowerLimit, delay;
private float prevHeight;
private Vector3 radiusPosition;
private void Start()
{
if (dc != null)
{
lastRadius = dc.xRadius;
}
else
{
lastRadius = radius;
}
move = true;
}
private void Update()
{
if (dc != null)
{
radiusPosition = new Vector3(target.position.x, target.position.y, dc.xRadius);
}
else
{
radiusPosition = new Vector3(target.position.x, target.position.y, radius);
}
if (move == false)
{
transform.RotateAround(target.position, axis, rotatingSpeed * Time.deltaTime);
t += Time.deltaTime;
if (t > delay)
{
prevHeight = setRandomHeight;
setRandomHeight = Random.Range(lowerLimit, upperLimit);
t = 0;
}
var tt = transform.position;
tt.y = Mathf.Lerp(prevHeight, setRandomHeight, t);
transform.position = tt;
}
if (dc != null)
{
if (lastRadius != dc.xRadius)
{
move = true;
lastRadius = dc.xRadius;
}
}
else
{
if (lastRadius != radius)
{
move = true;
lastRadius = radius;
}
}
if (move)
{
if (transform.position != radiusPosition)
{
float step = movingSpeed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position,
radiusPosition, step);
}
else
{
move = false;
}
}
}
}
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.
This code works for like 2min and stops taking swipe input, i need to relaunch the editor then again it works for 2min. if i did same with keyboard input it works just fine. there is a problem in swipemanager ill put both code................................................................................................................................................................................................
using UnityEngine;
public class SwipeManager : MonoBehaviour
{
public static bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
private bool isDraging = false;
private Vector2 startTouch, swipeDelta;
private void Update()
{
tap = swipeDown = swipeUp = swipeLeft = swipeRight = false;
#region Standalone Inputs
if (Input.GetMouseButtonDown(0))
{
tap = true;
isDraging = true;
startTouch = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0))
{
isDraging = false;
Reset();
}
#endregion
#region Mobile Input
if (Input.touches.Length > 0)
{
if (Input.touches[0].phase == TouchPhase.Began)
{
tap = true;
isDraging = true;
startTouch = Input.touches[0].position;
}
else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
{
isDraging = false;
Reset();
}
}
#endregion
//Calculate the distance
swipeDelta = Vector2.zero;
if (isDraging)
{
if (Input.touches.Length < 0)
swipeDelta = Input.touches[0].position - startTouch;
else if (Input.GetMouseButton(0))
swipeDelta = (Vector2)Input.mousePosition - startTouch;
}
//Did we cross the distance?
if (swipeDelta.magnitude > 100)
{
//Which direction?
float x = swipeDelta.x;
float y = swipeDelta.y;
if (Mathf.Abs(x) > Mathf.Abs(y))
{
//Left or Right
if (x < 0)
swipeLeft = true;
else
swipeRight = true;
}
else
{
//Up or Down
if (y < 0)
swipeDown = true;
else
swipeUp = true;
}
Reset();
}
}
private void Reset()
{
startTouch = swipeDelta = Vector2.zero;
isDraging = false;
}}
This is code of playermanager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public static bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
private bool isDraging = false;
private Vector2 startTouch, swipeDelta;
// Start is called before the first frame update
private CharacterController controller;
private Vector3 direction;
public float forwardSpeed;
private int desiredLane = 1;//1=left 2=middle 3=right
public float laneDistance;
public float Jumpforce;
public float Gravity = -20;
public GameObject GameOver;
public float MaxSpeed;
public Animator animator;
public SwipeManager sm;
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if(!PlayerManager.isGameStarted)
return;
animator.SetBool("isGameStarted", true);
if(forwardSpeed<MaxSpeed)
forwardSpeed += 0.05f* Time.deltaTime;
direction.z = forwardSpeed;
animator.SetBool("IsGrounded", controller.isGrounded);
if (controller.isGrounded)
{
direction.y = -1;
if (SwipeManager.swipeUp)
{
Jump();
}
}
else { direction.y += Gravity * Time.deltaTime; }
if (SwipeManager.swipeDown)
{
StartCoroutine(slide());
}
if (SwipeManager.swipeRight)
{
desiredLane++;
if (desiredLane == 3)
desiredLane = 2;
}
if (SwipeManager.swipeLeft)
{
desiredLane--;
if (desiredLane == -1)
desiredLane = 0;
}
Vector3 targetPosition = transform.position.z * transform.forward + transform.position.y * transform.up;
if (desiredLane == 0)
{
targetPosition += Vector3.left * laneDistance;
}
else if (desiredLane == 2)
{
targetPosition += Vector3.right * laneDistance;
}
transform.position = targetPosition;
}
private void FixedUpdate()
{
if(!PlayerManager.isGameStarted)
return;
controller.Move(direction * Time.fixedDeltaTime);
}
public void Jump()
{
direction.y = Jumpforce;
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.transform.tag == "Obstacle")
{
GameOver.SetActive(true);
Time.timeScale = 0;
}
}
private IEnumerator slide()
{
controller.center = new Vector3(0,-0.5f,0);
controller.height = 1;
animator.SetBool("isCrouching", true);
yield return new WaitForSeconds(1.3f);
controller.center = new Vector3(0,0,0);
animator.SetBool("isCrouching", false);
controller.height = 2;
}}
I think your swipeDelta = Vector2.zero; should be called in the Start() function and your Reset() function only, as in the Update() it is getting zero every frame.
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
}
}
}
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.