Ziad
1
sorry for that kind of stupid questions… but i get a headache every time i try to fix this problem… the problem is … i made a GameObject and i told him to move to the target that the player have choosed… but when it moves to the target it keeps rotating crazy, this is the script that i used
using UnityEngine;
using System.Collections;
public class PickedLocust : MonoBehaviour
{
public static PickedLocust Instance;
public CharacterController characterController;
public float time;
public Transform Player;
public Transform startPos;
public Transform target;
private int moveSpeed = 15;
Vector3 moveVector { get; set; }
void Awake()
{
Instance = this;
characterController = GetComponent("CharacterController") as CharacterController;
}
public void Update()
{
characterController.Move(moveVector * Time.deltaTime * moveSpeed);
if (Leader.Instance.target != null)
{
target = Leader.Instance.target;
}
if (target)
{
transform.LookAt(target);
moveVector = transform.TransformDirection(Vector3.forward);
}
else if (!target)
{
moveVector = new Vector3(0, 0, 0);
target = startPos;
}
}
void OnTriggerStay(Collider OtherObjects)
{
Food FoodScript = OtherObjects.GetComponent("Food") as Food;
float health = FoodScript.Health; ;
FoodScript.Health--;
}
}
You don’t describe how fast, or how tight the loops are so its hard to guess what’s going on. Your code doesn’t look wrong, There are a few things I would check:
- Make sure Leader.Instance.target doesn’t change when you don’t expect it to.
- If it only spins when you get to the object, then you want to have a conditional clamping so you don’t face it when your within ~.5units.
Besides that I’m not to sure what’s wrong. The only other thing I would try is to move the character independent of their forward vector, and move the character at the end of the function.
void Update () {
//rotate towards target
var moveDirection = Vector3.zero;
moveDirection = target.position - transform.position;
moveDirection = moveDirection.normalized;
moveDirection *= moveSpeed
var controller = GetComponent<CharacterController>();
controller.Move(moveDirection * Time.deltaTime);
}
This might not work, but its a quick change and its worth a shot.