- Home /
how would i make this work?
Basically i wrote a small bot script and it works but i need the enemy to always point towards the soldier, how would i do this, heres my code:
using UnityEngine; using System.Collections;
public class BOT : MonoBehaviour {
public GameObject Soldier;
public GameObject Enemy;
public float Distance=100f;
public Rigidbody bulletPrefab;
public GameObject EnemyBarrelEnd;
public GameObject gun;
private Vector3 SoldierPos=new Vector3(0,0,0);
// Update is called once per frame
void Update ()
{
SoldierPos=Soldier.transform.position;
Enemy.transform.position=Vector3.Lerp(Enemy.transform.position, Soldier.transform.position,Time.deltaTime);
animation.Play("soldierRun");
Distance=Vector3.Distance(Enemy.transform.position,Soldier.transform.position);
if (Distance<=5)
{
animation.Play("soldierFiring");
Rigidbody rocketInstance;
rocketInstance = Instantiate(bulletPrefab, EnemyBarrelEnd.transform.position, bulletPrefab.transform.rotation) as Rigidbody;
rocketInstance.AddForce(Vector3.forward*10000);
}
}
}
Your question's title is way too general. Please consider changing it to something like "How do I set one object to look constantly at another gameObject"? That would be clearer and people can help you better by posing a specific problem.
First off it is a specific problem, the person just doesn't know how to do somthing it's not way to general. If your going to comment something tell him how to do what he needs help with and don't be a tool.
Answer by ikelaiah · Jan 22, 2014 at 10:15 PM
Have you look at the documentation on Transform.LookAt()? As per documentation: "It rotates the transform so the forward vector points at target's current position." It seems that this is what you're after.
Here's an example from the Unity's doc -- given in the link above;
// This complete script can be attached to a camera to make it
// continuously point at another object.
// The target variable shows up as a property in the inspector.
// Drag another object onto it to make the camera look at it.
var target : Transform;
// Rotate the camera every frame so it keeps looking at the target
function Update() {
transform.LookAt(target);
}
thanks, i didn't realise it was in the documentation, i better look there first next time :)
@tayyab43 That's okay. Life is a lifelong learning. You can help others next time too.
Answer by komodor · Jan 22, 2014 at 10:12 PM
don't you look for http://docs.unity3d.com/Documentation/ScriptReference/Transform.LookAt.html or http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.RotateTowards.html?
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Rotating camera while wallrunning 1 Answer
Can't stop camera from rotating on Z Axis 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer