- Home /
my GameObject keebs rotating if i told him to move to another GameObject
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--;
}
}
and i have tried to make the gameObject move only if the distance between it and the target is greater then 2 , but it didn't work , the gameObject keebs moving up, down, left, and right
Answer by Peter G · Aug 20, 2011 at 04:37 PM
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.
its working great, but the object does not look at the target and if i added a line to ur code that makes the object look at the target , the same problem happen again..... i'm trying to make the object go to the target that the player selected and destroy him and then go back to his first position but i cant get that to work, i'm sry if i'm stupid but i realy don't know.
Hmmmm, your code doesn't seem like it should be spinning: Try changing the rotation method too. $$anonymous$$aybe (unlikely) its a bug in transform.LookAt().
Try doing something like this:
var moveRotation = Quaternion.LookRotation(moveDirection);
transform.Rotation = Quaternion.Slerp(transform.Rotation , moveRotation, Time.deltaTime);
O$$anonymous$$G, i don't know how to thank u , u are one of the best guyz here :)
Your answer
Follow this Question
Related Questions
Unity: C#: Eyes look at mouse: HELP!! 3 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Decrease distance between 2 objects 3 Answers