I am making a simple script to where when the player attacks, he moves toward the enemy. Now I got it to where the player will face the enemy, and move to him. But when the player moves towards the enemy he shrinks? and the closer he gets the slow he moves… and he just repeatedly runs into the enemy’s feet… and if I try to move I fall through the floor.
Any Ideas on whats wrong here?
This is the script I am using,
using UnityEngine;
using System.Collections;
public class PlayerAttack : MonoBehaviour {
//----------Variables Start----------
public int moveSpeed;
public int rotationSpeed;
public Transform target;
private Transform myTransform;
public float smooth = 5.0F;
public float smoothTime = 0.3F;
public bool iAmAttacking = false;
public bool iAmTargeting = false;
private Vector3 velocity = Vector3.zero;
//----------Variables End------------
//A quick reference of our transform.
void Awake()
{
myTransform = transform;
}
// Update is called once per frame
void Update ()
{
//If we are targeting something, Draw a line between the player, and the target.
if(iAmTargeting)
{
GameObject go = GameObject.FindGameObjectWithTag("Enemy");
target = go.transform;
Debug.DrawLine(target.transform.position, myTransform.position, Color.red);
}
//Since when we are targeting/Fighting, our ThirdPersonController is disabled.
//If we use any Input, enable our ThirdPersonController.
if(Input.anyKey)
{
GameObject player = GameObject.Find("Player");
PlayerAttack playerAttack = player.GetComponent<PlayerAttack>();
playerAttack.iAmTargeting = false;
playerAttack.iAmAttacking = false;
ThirdPersonController thirdPersonController = player.GetComponent<ThirdPersonController>();
thirdPersonController.enabled = true;
}
//If we are atttacking, the played will constantly face the enemy, and move towards them.
if(iAmAttacking)
{
transform.LookAt(target.transform);
transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime * smooth);
}
}
}