hi all
when i start the game near npc start moving to my character but it first rotate around my character then forward to my character.why
thankful.
The question is impossible to answer as stated. In order to help, we’ll need more information (source code, example images, and/or a more detailed description of the problem would be a good start).
I Agree with Anders, Just tell us the script you’re using atleast, so we can determine the problem.
this is my script:
void moveToEnemy()
{
RaycastHit hit;
Vector3 direction=transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position,direction,out hit,100f) ||
hit.rigidbody.gameObject.tag!=“Player”)
{
transform.rotation=Quaternion.Slerp(transform.rotation
,Quaternion.LookRotation(playerTransform.position-transform.position),Time.deltaTime*5f);
}
myAnimation.CrossFade(runAnimation.name);
transform.Translate(gameObject.transform.forwardTime.deltaTimerunSpeed);
}
I see a few potential problems with your code (see comments below):
RaycastHit hit;
// What you have will work, but you don't need to use TransformDirection()
// here. Instead of this:
//Vector3 direction=transform.TransformDirection(Vector3.forward);
// You can just write:
Vector3 direction = transform.forward;
// (Or just use transform.forward directly, of course.)
// Here, you access the variable 'hit' even if the raycast returns
// false, which I doubt is what you want to do. Think about what
// logical operator would make the most sense here: would it be
// 'or' (||) or 'and' ()?
// Also, keep in mind that hit.rigidbody will be null if the target
// object doesn't have a rigid body component attached. I'd recommend
// instead using .collider or .transform, as they'll always be valid.
if (
Physics.Raycast(transform.position, direction, out hit, 100f) ||
hit.rigidbody.gameObject.tag != "Player")
{
transform.rotation = Quaternion.Slerp(
transform.rotation,
Quaternion.LookRotation(
playerTransform.position - transform.position),
Time.deltaTime * 5f
);
}
myAnimation.CrossFade(runAnimation.name);
// You can just write 'transform.forward' here (rather than
// gameObject.transform.forward).
transform.Translate(
gameObject.transform.forward * Time.deltaTime * runSpeed);
There may be other problems as well, but those are the things I noticed right off.
thanks.but my problem not solved.
this is complete myEnemy Script:
using UnityEngine;
using System.Collections;
public class myEnemy : MonoBehaviour {
public int range=0;
public int health=100;
int weaponDamage=10;
public AnimationClip idleAnimation;
public AnimationClip walkAnimation;
public AnimationClip runAnimation;
public AnimationClip attackAnimation;
bool trig=false;
public Animation myAnimation;
GameObject enemyGameObject;
public float runSpeed=5f;
// Use this for initialization
void Start () {
myAnimation=(Animation)GetComponent(typeof(Animation));
playerTransform=GameObject.FindWithTag("Player").transform;
}
Transform playerTransform;
// Update is called once per frame
void Update () {
//detect and direct to enemy
float objectDistance=
Vector3.Distance(transform.position,playerTransform.position);
if (objectDistance<range objectDistance>1f)
{
transform.rotation=Quaternion.Slerp(transform.rotation
,Quaternion.LookRotation(playerTransform.position-transform.position),Time.deltaTime*5f);
moveToEnemy();
}
else if(objectDistance<=1f)
{
myAnimation.CrossFade(idleAnimation.name);
combat();
}
}
void applyDamage(int damage)
{
health-=damage;
print(health+"enemy");
}
void moveToEnemy()
{
RaycastHit hit;
Vector3 direction=transform.forward;
if((Physics.Raycast(transform.position,direction,out hit,100f)))
{
transform.rotation=Quaternion.Slerp(transform.rotation
,Quaternion.LookRotation(playerTransform.position-transform.position),Time.deltaTime*5f);
}
myAnimation.CrossFade(runAnimation.name);
transform.Translate(transform.forward*Time.deltaTime*runSpeed);
}
void combat()
{
myAnimation.CrossFade(attackAnimation.name);
if (trig)
enemyGameObject.SendMessage("applyDamage",weaponDamage,SendMessageOptions.DontRequireReceiver);
trig=false;
}
void oncoll(GameObject cal)
{
if(cal==null)
{
trig=false;
}
else
{
trig=true;
print(cal.name);
enemyGameObject=cal.transform.root.gameObject;
}
}
void weaponDamageFunc(int weaponDmg)
{
weaponDamage=weaponDmg;
}
}
Thankful.
Please repost your code using ‘code’ tags rather than ‘quote’ tags. Also, make sure that the indentation is preserved when you post the code.
