Question by
gothman89 · Feb 09, 2016 at 02:31 PM ·
unity5motionrandomizing
How to make an enemy move randomly over the terrain while always looking at me/the first person character??
SO I have this droid hovering on my scene and I have a first person character. I can make my droid always look at me whenever I move. That part is complete. I can make my droid move towards me to attack me. That is also checked. Now, I need my droid to move randomly at some distance and just shoot laser beams at me. I don't want help with shooting laser beams but I need help to make it move randomly near the FP Character.
Here is my code till now:
using UnityEngine;
using System.Collections;
public class MotionControl : MonoBehaviour {
public Transform target;
// public Transform myTransform;
public float movespeed = 0.35f;
public int rotationSpeed = 1;
Vector3 startPosition;
// float roamRadius = 20.0f;
// NavMeshAgent nav;
public float width = 5;
private Transform myTransform;
// Use this for initialization
void Start()
{
// player = GameObject.FindGameObjectWithTag("Player");
myTransform = transform;
startPosition = transform.position;
// GameObject go = GameObject.FindGameObjectWithTag("Player");
// target = go.transform;
}
// Update is called once per frame
void Update ()
{
if(target != null) {
transform.LookAt(target);
}
startPosition = transform.position;
// Vector3 randomPt = (Vector3)Random.Range(-1,1);
float distance = Vector3.Distance(target.transform.position, transform.position);
Debug.Log(distance);
float rx = Random.Range(220, 280);
float ry = Random.Range(25, 50);
float rz = Random.Range(220, 280);
Vector3 targetPos = new Vector3(transform.position.x + (rx * width), transform.position.y + ry, transform.position.z + (rx * width));
// Vector3 randomMov = Vector3.MoveTowards(startPosition, targetPos, 1.0f);
//look at target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(targetPos - myTransform.position), rotationSpeed * Time.deltaTime);
//move towards target
myTransform.position = targetPos * movespeed * Time.deltaTime;
// to make enemy move towards player
// myTransform.position += myTransform.forward * movespeed * Time.deltaTime;
}
}
I thought this would work but it doesn't and that's why I am here. I hope have made my question clear. Thanks in advance. This is for an assignment due this week, so please, if you can, help me out as soon as possible. Thank you all for the all the help again, in advance!!
Comment