- Home /
Can anyone tell me whats wrong with my MoveTowardsEnemy part of my script?
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);
}
}
}
Answer by iwaldrop · Jan 22, 2013 at 07:18 AM
One, because you're Lerping from points a > b, but your value is never 1; hence, your player will never arrive at his destination.
http://en.wikipedia.org/wiki/Lerp_(computing)
And two; it's probably because you're using LookAt...so your player is looking at the enemy's actual location (the ground). You're going to want to maintain your x/z rotation and only rotate about the y axis.
Once you get the horizontal rotation sorted, try experimenting with transform.Translate() to move your character instead of Lerping him around.
How would you recommend going about the horizontal rotation?
I don't have time right now to give you a great answer, but you can do something like this (and hopefully someone who does have time will improve upon it)...
public Transform lookTarget;
public float moveAmount;
void TurnAnd$$anonymous$$ove()
{
Vector3 angles = transform.eulerAngles;
transform.LookAt(lookTarget);
angles.y = transform.eulerAngles.y;
transform.eulerAngles = angles;
transform.Translate(Vector3.forward * moveAmount);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613091356im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Getting enemy to move towards player untill they collide problem... 2 Answers
rebuild target list when gameobject destroyed 1 Answer
Enemy Death and Killing Script 3 Answers
Creating Flowing Water 1 Answer