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 /
avatar image
0
Question by GamerC4 · Apr 08, 2016 at 06:40 AM · 2d game2d-platformer

Make An Enemy Rotate Around A Player

alt text

I am trying to have the flying eyeball enemy rotate around the player using this code for the rotation

   v_diff = (target.position - transform.position);
         atan2 = Mathf.Atan2(v_diff.y, v_diff.x);
         transform.rotation = Quaternion.Euler(0f, 0f, atan2 * Mathf.Rad2Deg);

and the pivot of the sprite is where the pupil is though I have tried having it be the end of the tail to no avail. Please help me have the eye face the player.

issues.png (186.1 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Gruffy · Apr 08, 2016 at 11:44 AM

Hey GamerC4,

Although this will not just slot into your code as such , you can use this script to see the action of what you want and adapt it accordingly

Place script on the required rotating gameobject, nominate your target gameobject in the script inspector as the object to rotate around Use the arrow keys to test the rotation around your target object ...anyway below is the script, try this first and get back to here after and discuss how you want it developed into your existing scripts.

 using UnityEngine;
 using System.Collections;
 
 public class RotateObjectAroundAPoint : MonoBehaviour
 {
     public GameObject goToRotate;
     private Transform mTrans;
     public GameObject target;
     private Vector3 targetPosition;
     public float speed;
     private RotateObjectAroundAPoint inst;
     void Start()
     {
         inst = this;
         inst.mTrans = goToRotate.transform;
         inst.targetPosition = target.transform.position;
         inst.mTrans.LookAt(target.transform);
     }
 
     void Update()
     {
     //use arrow keys to mvoe object around clockwise or anticlockwise (additive speeps by holding one side or another for testing only)
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             speed++;
         }
         if (Input.GetKey(KeyCode.RightArrow))
         {
             speed--;            
         }
     //this rotates this object around the nominated gameobject in the scene
         inst.mTrans.RotateAround(targetPosition, Vector3.up, speed * Time.deltaTime);
     }
 }
 

Cheers for now, hope this start you on your journey :)

Comment
Add comment · 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
0

Answer by Ali-hatem · Apr 08, 2016 at 11:30 AM

i have a simple way eyeball script :

 public float diameter;
 public float RotateSpeed;
 public Transform target;
 void Update(){
  transform.position = target.position; //assuming the eyeball should fallow the player.
  transform.Translate(diameter,0,0);
  transform.Rotate(0,0,RotateSpeed);
 }
Comment
Add comment · 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

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

PLayer not landing afteer jumping,Player jumping by clicking button 1 Answer

Player 2D getting stuck while moving 2 Answers

2d shooting problem 1 Answer

2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers

How to set axes for mobile controlling? 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