I have created a player move/action script based on mouse0 held down.
Currently clicking or holding the mouse 0 on the ground causes the model to move/animations to fire.
If I let go the model stops, animation returns to default.
If I click/hold on a ‘shootable’ object, model stops and plays the shooting anim.
If I let go, the anim stops.
Problem<<
If I click and hold on ground, and drag through/on a shootable object , the player remains in the ‘walking state’ and continues to play the walking anim.
Like wise if I click and hold, on a shootable object, the shooting anim fires, If I drag outside of the shootable, namely the ground, the player starts moveng as per the walking transform, but continues to play the shooting anim.
I know the raycast is somehow updating what my cursor is over, as the model starts to move but the animation is not updating to the walk from the shoot. reflect this change. like wise the walk, does not seem to switch to the shoot on mouse over of a shootable.
I suspect it may be the transitions in the animator, and the boolean parameter updates to the animator.
Currently my animations and parameters are as follows
Animations:
Default Pose (standing still)
walking (…walking)
shooting (eh…shooting)
Parameters:
IsWalking
IsShooting
Setup is as follows:
default —> walking (requires IsWalking true)
walking—>default (requires IsWalking false)
default ----> shooting (requires IsShooting true)
shooting —> default (requires IsShooting false)
I have a number of Debug lines in my code from working on the shooting detection. I’ve left them in.
Any ideas would be helpful or suggestions on what to change.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
namespace CompleteProject
{
public class PlayerMoveClick2 : MonoBehaviour
{
private Animator anim;
private NavMeshAgent navMeshAgent;
Rigidbody playerRigidbody;
int floorMask;
private bool walking;
private bool shooting;
private float shootDur;
private float shootNext;
private bool enemyClicked;
private Transform targetedEnemy;
// Use this for initialization
void Awake ()
{
anim = GetComponent<Animator> ();
navMeshAgent = GetComponent<NavMeshAgent> ();
//floorMask = LayerMask.GetMask ("Shootable");
playerRigidbody = GetComponent <Rigidbody> ();
shootDur = 0.5f;
shootNext = 0f;
shooting = false;
}
// Update is called once per frame
void Update ()
{
Debug.Log("======VOID UPDATE START=======");
if (Input.GetMouseButton(0))
{
Debug.Log("LEFT MOUSE HELD/PRESSED");
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit moveHit;
if (Physics.Raycast(ray, out moveHit, 100f)) //removed floorMask at end
{
Debug.Log("Raycast == HIT");
if (moveHit.collider.CompareTag("Enemy"))
{
targetedEnemy = moveHit.transform;
enemyClicked = true;
Debug.Log("HIT Target == Enemy ");
shootWep1();
}
else
{
Debug.Log("HIT Target == NOT Enemy");
Vector3 moveTarget = moveHit.point - transform.position;
moveTarget.y = 0f;
Quaternion newRotation = Quaternion.LookRotation(moveTarget);
playerRigidbody.MoveRotation (newRotation);
walking = true;
//shooting = false;
enemyClicked = false;
navMeshAgent.destination = moveHit.point;
anim.SetBool ("IsWalking", walking);
anim.SetBool ("IsShooting", shooting);
navMeshAgent.Resume();
}
}
}
else
{
Debug.Log("LEFT MOUSE not HELD/PRESSED");
walking = false;
shooting = false;
anim.SetBool ("IsWalking", walking);
anim.SetBool ("IsShooting", shooting);
navMeshAgent.Stop();
}
//if (enemyClicked == true)
//{
// Debug.Log("enemyClicked == true SUCCESS");
// shooting = true;
// walking = false;
// anim.SetBool ("IsWalking", walking);
// navMeshAgent.Stop();
// shootWep1();
//}
//mouse not pressed so stand still
//else
//{
// walking = false;
// //shooting = false;
// anim.SetBool ("IsWalking", walking);
// //anim.SetBool ("IsShooting", shooting);
// navMeshAgent.Stop();
//}
Debug.Log("=======VOID UPDATE END========");
}
private void shootWep1()
{
Debug.Log("shootWep1 CALLED SUCCESSFULLY");
if (enemyClicked == true)
{
Debug.Log("enemyClicked == STILL TRUE");
if (Time.time > shootNext) //ShootDur = 0.5f ShootNext= ? Time.time = current time in frame since game start
{
Debug.Log("-->>TIME TO SHOOT<<-- Time.time > shootNext");
shootNext = Time.time + shootDur;
//Call shooting anim, perform shoot actions
//navMeshAgent.Stop();
//walking = false;
//anim.SetBool ("IsWalking", walking);
shooting = true;
anim.SetBool ("IsShooting", shooting);
}
else
{
Debug.Log("-->>CAN'T SHOOT YET <<-- Time.time NOT > shootNext");
if (shootNext < Time.time + shootDur)
{
Debug.Log("Waiting for current shoot animation to finish");
shooting = true;
Debug.Log("Making shooting = true so animation continues");
}
else
{
Debug.Log("Shooting Animation not playing anymore");
//navMeshAgent.Stop();
//walking = false;
//anim.SetBool ("IsWalking", walking);
shooting = false;
anim.SetBool ("IsShooting", shooting);
Debug.Log("Stopped shooting animation");
}
}
}
Debug.Log("shootWep1 === FINISHED");
}
}
}