- Home /
Question by
assanuma1 · Mar 16, 2018 at 04:38 PM ·
scripting problemvector3aimodify
Modify player controller script to chase player instead
I downloaded a character pack from the unity store. The script included is a player controller and I want to modify it to be an AI script so it chase the player. I wanted to minimize any changes to the script due to the way the animations are scripted.
The modifications I've made are not doing what I thought it would do. Instead of chasing the player the chaser runs into a wall.
I'd appreciate any help.
This is part of the code I was working on.
public GameObject targetPlayer; //Added
private Transform targetTransform; //Added
Vector3 inputVec;
Vector3 targetDirection; //Added
Vector3 toTargetVector; //Added
void Update()
{
targetTransform = targetPlayer.transform; //Added
toTargetVector = targetTransform.position - transform.position; //Added
//Get input from controls
float z = toTargetVector.z; //Input.GetAxisRaw("Horizontal"); //Commented out original code
float x = -toTargetVector.x; //-(Input.GetAxisRaw("Vertical")); //Commented out original code
toTargetVector.y = 0;
inputVec = toTargetVector; //new Vector3(x, 0, z); //Commented out original code
//Apply inputs to animator
animator.SetFloat("Input X", z);
animator.SetFloat("Input Z", -(x));
Comment
Your answer
Follow this Question
Related Questions
How would i go about comparing two transform scales 1 Answer
OnTriggerEnter called only one time?!! 5 Answers
More Efficient way? 1 Answer
Wandering AI "ignores" speed 2 Answers
I need help for an animation to be played when my funciton is called. 0 Answers