- Home /
Question by
elisonic12 · Jun 12, 2018 at 07:16 AM ·
c#2dunity 2d
How to make Comanion AI pass through the player??,How to make your Companion AI pass through the player??
So I'm working on a simple 2D platform and my player has a NPC that follows him around as a companion. It works but now if I try to walk pass the NPC all I end up doing is just pushing them. Any idea on how to stop this from happening??
I want to be able to just pass through the Companion without pushing them
I'll add the code I'm working with, no clue if it'll help.
I'm also trying to make the AI to be able to jump after I jump and re spawn near me should I lose it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Companion : MonoBehaviour {
public float speed;
public float stoppingDistance;
private Transform target;
bool facingRight;
// Use this for initialization
void Start () {
target = GameObject.FindGameObjectWithTag ("Player").GetComponent<Transform> ();
facingRight = true;
}
// Update is called once per frame
void Update () {
if (Vector2.Distance(transform.position, target.position) > stoppingDistance) {
transform.position = Vector2.MoveTowards (transform.position, target.position, speed * Time.deltaTime);
transform.LookAt (transform.position);
}
}
void FixedUpdate () {
float move = Input.GetAxis ("Horizontal");
if (move > 0 && !facingRight) {
flip ();
}
else if (move < 0 && facingRight)
{
flip ();
}
}
void flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Comment
Your answer
Follow this Question
Related Questions
i have a problem with reloading with Time.time 2d game c# Unity 2020.2 1 Answer
Teleport Not Working in Unity 2D 1 Answer
Unable to make 2D enemy zigzag : Top Down View 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers