Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 Dre0 · Feb 09, 2017 at 03:44 PM · enemymovement scriptplatformer

Make circular moving object stay within a specific area

Hello, Currently I have successfully implemented an enemy that can moving in a circular motion, however, when I begin the game the enemy goes off to a completely random area of the level. How would I ensure that the enemy stays within a specific area while moving circular? Thank you.

 using UnityEngine;
 using System.Collections;
 
 public class CircularMovement : MonoBehaviour {
 
     //timer
     float timeCounter = 0;
 
     //speed
     public float speed;
     public float width;
     public float height;
 
     // Use this for initialization
     private void Start () {
         speed = 2;
         width = 2;
         height = 3;
 
         //start in positon 
         transform.position = new Vector3(53.939f, 1.201f);
         transform.position = transform.position;
 
     }
     
     // Update is called once per frame
     void Update () {
         timeCounter += Time.deltaTime*speed;
 
         //directional path
         float x = Mathf.Cos (timeCounter)*width;
         float y = Mathf.Sin (timeCounter)*height;
 
 
         transform.position = new Vector2(x, y);
     
     }
 }
 
Comment
Add comment · Show 1
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
avatar image Saad_Khan · Feb 09, 2017 at 03:31 PM 0
Share

If you want the enemy to move around in a fixed area , why not use unity's API function Transform.RotateAround..

https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dre0 · Feb 09, 2017 at 08:45 PM

Hi I tried using the following function and ensuring that I placed the game object correctly but for some reason it still will not work. Any suggestions?

  void OrbitAround()
     {
         transform.RotateAround(Boundary.transform.position, Vector2.right, 3 * Time.deltaTime);
     }
Comment
Add comment · Show 1 · Share
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
avatar image Saad_Khan · Feb 10, 2017 at 12:32 PM 0
Share

Change your orbitAround to something like the following , and make sure the object to which this script is attached is at a distance (away) from the 'point' around which it rotates :

     public Transform point;
 
     // Update is called once per frame
     void Update () {
     
         OrbitAround();
     }
 
     void OrbitAround()
     {
         transform.RotateAround(point.position, Vector3.up, 10 * Time.deltaTime);
     }

Just as in this screenshot the point(sphere) is at the centre and the Cube is the object to which this script is applied.alt text

screen-shot-2017-02-10-at-52817-pm.png (87.7 kB)

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with a health bar 0 Answers

platform ai following player direction when close, flips normal movement ,cant get my enemy to keep his sprite position when moving back to move after following during attack 1 Answer

Enemy AI ignores the distance from the player 0 Answers

How to tidy up my code for Unity 5 Basic Platformer 1 Answer

Hi guys, I am trying to make my enemy to chess the player when he enter in to danger zone. But the enemy is not chasing the player when he enter in to dangerzone. Please help me ..Thank you.... 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