- Home /
Question by
Actnshj200 · Dec 05, 2017 at 10:35 AM ·
unity 5physicsraycastingray
Why wont my Raycast update...?
Here is my Ai Script. The problem probably lies within my update function or my RayHit() function. The character does not move as the character rotates. It will hit my direction at first but its position wont update. Here is some images of my setup.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class soldierAI : MonoBehaviour {
Animator move;
public CharacterController controller;
public NavMeshAgent agent;
public RectTransform ViewTopHealth;
public RectTransform ViewTopShield;
public bool InSight = false;
public Transform RaySpawn;
public float speed = 300;
// Use this for initialization
void Start () {
move = GetComponent<Animator>();
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update ()
{
if (InSight == true)
{
RayHit();
}
if (InSight==false)
{
move.SetBool("soldierWalk", true);
}
}
void SoldierAnim()
{
}
void RayHit()
{
//Ray myRay;
RaycastHit hit; // Hit input
Physics.Linecast(RaySpawn.position, RaySpawn.position*speed, out hit); //Collision Data
Debug.DrawLine(RaySpawn.position, RaySpawn.position * speed, Color.red, .001f); // Renders Line Output
if (hit.collider.tag == "Player")
{
if (hit.transform.gameObject.GetComponent<healthBar>())
{
hit.transform.gameObject.SendMessage("HandleBarUpdateHealthbar");
}
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
//RayHit();
agent.isStopped = true;
InSight = true;
move.SetBool("soldierWalk", false);
move.SetBool("soldierShooting", true);
}
if (other.tag == "Helper")
{
//RayHit();
agent.isStopped = true;
InSight = true;
move.SetBool("soldierWalk", false);
move.SetBool("soldierShooting", true);
}
}
void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
// RayHit();
agent.isStopped = true;
move.SetBool("soldierWalk", false);
move.SetBool("soldierShooting", true);
}
if (other.tag == "Helper")
{
//RayHit();
agent.isStopped = true;
move.SetBool("soldierWalk", false);
move.SetBool("soldierShooting", true);
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
InSight = false;
move.SetBool("soldierWalk", true);
move.SetBool("soldierShooting", false);
}
if (other.tag == "Helper")
{
InSight = false;
move.SetBool("soldierWalk", true);
move.SetBool("soldierShooting", false);
}
}
}
ray.png
(399.5 kB)
component.png
(13.7 kB)
Comment
I dont get what you are trying to do with the Endpoint in your statement
Physics.Linecast(RaySpawn.position, RaySpawn.position*speed, out hit);
RaySpawn.position*speed doesnt make sense to me as the endpoint for your ray.
Also I see that speed is set to 300 and never changes?
To project a ray forward with respect to the object, you need to use the local z axis of the object's transform.