• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rigidbody: Linear motion to Circular motion 0 Answers

How to shoot a laser beam in a 2.5d game 0 Answers

importing .fbx file breaks into many polymesh 1 Answer

I'm not able to make the build for unity oculus project. 1 Answer

Properly attach to a GameObject after collision? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges