Is there a way to make a follow script without NavMesh?
Is there a way to make a character move towards the player, exactly like Nav but not since Nav breaks my game? Can you post the script please?
I need an answer TODAY!
Clearly there are games that do not use Unity's navmesh and still have AI pathfinding and whatnot, so it becomes a question of how much work you are willing to do to that end.
It's important to ask the right questions. The answer to your current question posed here is "yes, of course", but it sounds like the actual question you want to ask is "what happened to my project to break the navmesh system?"
It sounds like this is something for work or school (by the deadline), so the urgency is noted.
What are you looking for as far as "follow script" is concerned? Just something to slowly approach some other object? You presumably care about obstacles, or you likely wouldn't bother with setting up a navmesh agent system.
I need a script that will work with the currently existing Third Person Character (Ethan) standard asset animation. Just a basic "approach" script. Having an obstacle avoidance would be helpful but not necessary. You guessed correctly about the school project, I won't get more than an hour to work on it after tomorrow.
There is a bit here, but this will be 90% comments as this seems to be for class / education.
Create an empty scene, make two cubes, attach this to one of them. Then set the other cube as its follow target.
It will look like this: **http://i.imgur.com/FwX4wxU.gifv**
using UnityEngine;
using System.Collections;
public class SimpleFollow : $$anonymous$$onoBehaviour
{
// Pick a target to see how this works
public float followSpeed = 0.1f;
public Transform followTarget;
// Update is called once per frame
void Update ()
{
// Call the function we wrote below. It can get a bit ugly
// if you put a bunch of code in a function like Update().
//
this.Follow (this.followTarget, fSpeed: this.followSpeed);
}
void Follow (Transform target, float fSpeed = 1)
{
// There are more elegant and modular ways to do this, but it sounded like
// you needed a very simple example to get started.
//
// First, we'll deter$$anonymous$$e how much we should
// be moving. Vector3.$$anonymous$$oveTowards() will give
// you a vector in the direction we want and
// then set that vector's magnitude to whatever
// we supply for the 3rd parameter. In this case,
// we're calling this every frame, so we want
// to move a certain amount of units each second.
// We accomplish this by multiplying that distance
// by the change in time.
//
Vector3 newPosition = Vector3.$$anonymous$$oveTowards(this.transform.position, target.position, fSpeed * Time.deltaTime);
// If you wanted to update the animations, you might do so here.
// Animation control schemes vary from project to project,
// so your own code will differ from someone else's.
//
/* Animation stuff here */
// Lastly, assign the values we calculated above
this.transform.position = newPosition;
this.transform.LookAt (target.position, this.transform.up);
}
}
I'm using the existing Ethan prefab animator, can you edit the script to take that into account? I tried myself, but I can't get it to work.
I'm not familiar with that specific controller, but this is your assignment. I was hesitant to even post that much, but the desperation egged me beyond typical scaffolding territory.
These forums are not a place where you should look for homework problems to be magically solved. You have nearly all of the work done for you, with the remaining problem being syncing an animation to this.
Hint: magnitude comes into play.
Answer by Jimmy_Jonno · Jun 19, 2017 at 10:44 AM
Hey there. First of all sorry for my bad english. If you are still asking how to do that,here is a code that I made some weeks ago. Unfortunatley this code let the character move through walls and collider obstacle.
float moveSpeed=3.0f;
private GameObject character;
public GameObject player;
void Start(){
character = GetComponent<GameObject>();
player=GameObject.Find("Player");
}
void Update(){
//code for looking to player
character.transform.rotation = Quaternion.Slerp(character.transform.rotation,
Quaternion.LookRotation(player.transform.position - character.transform.position), 3 * Time.deltaTime);
//code for following the player
character.transform.position += character.transform.forward * moveSpeed * Time.deltaTime;
}
Your answer
Follow this Question
Related Questions
Navmesh agent enemy moving to players last seen position 0 Answers
How do I make enemies that avoid player? 0 Answers
Navmesh dont work! 0 Answers
Having some problem with AI finding a vector3... 0 Answers
Is there anyway to avoid a StackOverflow 0 Answers