- Home /
Help with moving object towards player using Raycasting
I made two scripts. 1 for the player and 1 for the AI. The player is supposed to look at the AI and that would cause the AI to move towards the player. When I test it out, it doesn't work. Player Script {
Ray thugRay;
RaycastHit rayHit;
public int dist = 20;
void Update()
{
thugRay = new Ray(transform.position, transform.forward * dist);
Debug.DrawRay(transform.position, transform.forward * dist, Color.red);
if (Physics.Raycast(transform.position, transform.forward, out rayHit, dist))
{
{
if (rayHit.transform.gameObject.tag == ("It"))
{
Debug.Log("He looked at you");
GameObject.Find("Control").GetComponent("Move").enabled = false;
}
}
}
}
}
AI Script {
public Transform player;
public float walkingDistance = 10.0f;
public float smoothTime = 10.0f;
private Vector3 smoothVelocity = Vector3.zero;
RaycastHit rayHit;
private void Start()
{
}
void HitByRay()
{
Debug.Log("Rip");
transform.LookAt(player);
float distance = Vector3.Distance(transform.position, player.position);
if (distance < walkingDistance)
{
transform.position = Vector3.SmoothDamp(transform.position, player.position, ref smoothVelocity, smoothTime);
}
rayHit.transform.SendMessage("HitByRay");
}
}
Objects must have a collider to be detected by Physics.Raycast. Can you confirm your objects have that component?
Both the AI and the player have a trigger collider.
Answer by Legend_Bacon · Nov 24, 2017 at 07:08 PM
Hello there,
could you give us more information on what works and what doesn't please? Which Debug logs get triggered? Does your Debug Ray point towards the right direction?
Also, you say that you want the AI to move towards the player when looked at. But if the ray hits, you seem to be disabling the move component. Unless you are disabling the move component on the player, in which case it's fine.
And finally, when you do see the AI, nothing tells it to do anything. You probably want to have your scripts like that instead:
Ray thugRay;
RaycastHit rayHit;
public int dist = 20;
private bool iAmDead = false;
void Update()
{
if (iAmDead == false)
{
thugRay = new Ray(transform.position, transform.forward * dist);
Debug.DrawRay(transform.position, transform.forward * dist, Color.red);
if (Physics.Raycast(transform.position, transform.forward, out rayHit, dist))
{
if (rayHit.transform.gameObject.tag == ("It"))
{
Debug.Log("He looked at you");
GameObject.Find("Control").GetComponent<Move>().enabled = false;
rayHit.collider.gameObject.GetComponent<AI>().HitByRay();
iAmDead = true;
}
}
}
}
And now for the AI:
public Transform player;
public float walkingDistance = 10.0f;
public float smoothTime = 10.0f;
private Vector3 smoothVelocity = Vector3.zero;
RaycastHit rayHit;
private void Start()
{
}
public void HitByRay()
{
Debug.Log("Rip");
transform.LookAt(player);
float distance = Vector3.Distance(transform.position, player.position);
if (distance < walkingDistance)
{
transform.position = Vector3.SmoothDamp(transform.position, player.position, ref smoothVelocity, smoothTime);
}
//rayHit.transform.SendMessage("HitByRay");
}
I hope that helps!
~LegendBacon
I added the scripts and I tested them out however nothing happens to the AI. No debugs popped up, the debug ray was pointing in the right direction, and the disable move script I added for the player didn't work.
On the player script, you are creating a ray "thugRay" that you never use. Try replacing your if (Physics.Raycast(transform.position, transform.forward, out rayHit, dist))
with (if(Physics.Raycast(thugRay, out rayHit, 100)))
.
Let me know if that changes anything.
~LegendBacon
Still does not work. I get these two warnings though.
Field `RaycastingHit.rayHit' is never assigned to, and will always have its default value
NullReferenceException: Object reference not set to an instance of an object Raycasting.Update ()
Your answer
Follow this Question
Related Questions
Problem with Unity Ray Trace AI Detect Player 0 Answers
Another Raycast Issue. 0 Answers
Getting a point on ray 1 Answer
Can you figure out raycast origin position from RacyastHit? 2 Answers
Layer Mask Detection 2 Answers