Question by
Hunte060708 · Jun 06, 2018 at 08:31 PM ·
camerascripting problemraycastscript.animator controller
Animation not working when Camera is attached to script
Hello, maybe someone of you has an idea where my problem is. I have an character with an animationcontroller that is triggering the animation "shooting" when i hit the button "fire". This is working. But now i added a new function to the "playershooting"-Script that it creates an raycast and if it hits an enemy than the enemy gets damage. Also this works fine, but since i added this new function, the animations does not trigger anymore. i can reproduce this: if i comment out the function where i get the camera in Start() then the animation works. This is my playershooting-Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShootController : MonoBehaviour {
public Camera playerCam;
public GameObject bulletHolePrefab;
private Animator animator;
void Start () {
animator = GetComponent<Animator> ();
playerCam = transform.Find("PlayerCamera").GetComponent<Camera>();
}
// Update is called once per frame
void Update () {
if (Input.GetAxis ("Fire1") != 0) {
Debug.Log ("Fire1 pressed");
animator.SetBool ("isShooting", true);
shoot();
}
animator.SetBool ("isShooting", false);
}
void shoot () {
Ray ray = playerCam.ViewportPointToRay (new Vector3 (0.5F, 0.5F, 0));
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
Debug.Log ("Hit: " + hit.transform.gameObject.name);
if (hit.transform.tag == "Enemy") {
var newBulletHole = Instantiate (bulletHolePrefab, hit.point, Quaternion.identity);
newBulletHole.transform.parent = hit.transform;
hit.transform.gameObject.SendMessage ("getDamage", 10);
}
}
}
}
Comment