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 wongchris · Feb 02, 2020 at 08:58 AM · unity 2drotate objectshootlookattarget

How to make a boss who can shot toward an object, while the boss can do motion like the Earth

Now I have two 2D Objects. One is the boss, the other one is the rotate center point. And I also have two balls, which are red_ball and blue_bell. Here is what I want to make:

  1. The boss will move like the Earth (boss) to the Sun (rotate center point)

  2. The boss alway toward the rotate center point

  3. The boss will shot red_ball in the first rotate circle. And shot blue_ball in the second circle.

Here is the script I have(all are under the boss)

 [Header("Projectile Settings")]         
 public int numberOfProjectiles;             // Number of projectiles to shoot.
 public float projectileSpeed;               // Speed of the projectile.
 public GameObject ProjectilePrefab;         // Prefab to spawn.
 public Transform rotationCenter;
 public float rotationSpeed;
 public bool clockwise = true;

 [Header("Private Variables")]
 private Vector3 startPoint;                 // Starting position of the bullet.
 private const float radius = 1F;            // Help us find the move direction.
 public static bool shot_bool = false;
 
 [SerializeField]
 float angelarSpeed = 100f;
 float rotation_angle = 0f;



 // Update is called once per frame
 void Update()
 {
     if (shot_bool == true)
     {
         startPoint = transform.position;
         shot_to_center();
         shot_bool = false;
     }
     rotation_angle = rotation_angle + Time.deltaTime * angelarSpeed;

     Debug.Log("rotation_angle " + rotation_angle);
 }
 public static void shot()
 {
     shot_bool = true;
 }
 public void shoot()     //for the animation event
 {
     shot_bool = true;
 }
 // Spawns x number of projectiles.


 //-----------------------------------working------------------------------------------------

 private void shot_to_center()   
 {
     float angleStep = 360f;
     float angle = 0f;

     // Direction calculations.
     float projectileDirXPosition = startPoint.x + Mathf.Sin((rotation_angle * Mathf.PI) / 180) * radius;
     float projectileDirYPosition = startPoint.y + Mathf.Cos((rotation_angle * Mathf.PI) / 180) * radius;

     // Create vectors.
     Vector3 projectileVector = new Vector3(projectileDirXPosition, projectileDirYPosition, 0);
     Vector3 projectileMoveDirection = (projectileVector - startPoint).normalized * projectileSpeed;

     // Create game objects.
     GameObject tmpObj = Instantiate(ProjectilePrefab, startPoint, Quaternion.identity);
     tmpObj.GetComponent<Rigidbody2D>().velocity = new Vector3(projectileMoveDirection.x, projectileMoveDirection.y, 0);

     // Destory the gameobject after 10 seconds.
     Destroy(tmpObj, 10F);

     angle += angleStep;
 }




 //-----------------------------------no use now------------------------------------------------
 private void SpawnProjectile(int _numberOfProjectiles)      
 {
     float angleStep = 360f / _numberOfProjectiles;
     float angle = 0f;

     for (int i = 0; i <= _numberOfProjectiles - 1; i++)
     {
         // Direction calculations.
         float projectileDirXPosition = startPoint.x + Mathf.Sin((angle * Mathf.PI) / 180) * radius;
         float projectileDirYPosition = startPoint.y + Mathf.Cos((angle * Mathf.PI) / 180) * radius;

         // Create vectors.
         Vector3 projectileVector = new Vector3(projectileDirXPosition, projectileDirYPosition, 0);
         Vector3 projectileMoveDirection = (projectileVector - startPoint).normalized * projectileSpeed;

         // Create game objects.
         GameObject tmpObj = Instantiate(ProjectilePrefab, startPoint, Quaternion.identity);
         tmpObj.GetComponent<Rigidbody2D>().velocity = new Vector3(projectileMoveDirection.x, projectileMoveDirection.y,0);

         // Destory the gameobject after 10 seconds.
         Destroy(tmpObj, 10F);

         angle += angleStep;
     }
 }

It is the RotateMove.

 [Header("RotateMove")]
 [SerializeField]
 Transform rotationCenter;

 [SerializeField]
 float rotationRadius = 2f, angelarSpeed = 10f;
 float posX, posY, rotation_angle = 0f;
 float angle = 0f;
 public float rotationSpeed;
 public bool clockwise = true;
 Transform myTrans;
 Vector3 myPos;
 Vector3 myRot;
 void Awake()
 {
     myTrans = transform;
     myPos = myTrans.position;
     myRot = myTrans.rotation.eulerAngles;
     StartCoroutine(fight());
     
 }
 //-----------------------------------working------------------------------------------------
 public IEnumerator fight()
 {
     while(true)
     {
     FireBullet.shot();
     yield return new WaitForSeconds(1f);
     }

 }
 private void Update()
 {
     posX = rotationCenter.position.x + Mathf.Cos(rotation_angle) * rotationRadius;
     posY = rotationCenter.position.y + Mathf.Sin(rotation_angle) * rotationRadius;
     transform.position = new Vector2(posX, posY);
     rotation_angle = rotation_angle + Time.deltaTime * angelarSpeed;

     if (rotation_angle >= 360f)
         rotation_angle = 0f;
     angle = myTrans.eulerAngles.magnitude * Mathf.Deg2Rad;


     //rotate object Right & Left
     if (clockwise == true)
     {
         myRot.z -= rotationSpeed;
     }
     if (clockwise == false)
     {
         myRot.z += rotationSpeed;
     }
     myTrans.rotation = Quaternion.Euler(myRot);
 }

It is look to center

 [Header("Look_to_the_center")]
 public Transform Target;
 public float TurnSpeed = 50.00F;
 private Transform myTransform;

 public void Start()
 {
     myTransform = GetComponent<Transform>(); // Cache own Transform component
 }

 public void Update()
 {
     var targetDirection = Target.position - myTransform.position;
     targetDirection.z = 0.00F; // Lock global y-axis

     var targetRotation = Quaternion.LookRotation(targetDirection);
     var deltaAngle = Quaternion.Angle(myTransform.rotation, targetRotation);

     if (deltaAngle == 0.00F)
     { // Exit early if no update required
         return;
     }

     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,targetRotation,TurnSpeed * Time.deltaTime / deltaAngle);
 }

Some above script I just copy from the internet as I am very new in unity. But I will try to learn it.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by wongchris · Feb 02, 2020 at 05:32 PM

I solved the problem of these code.

 public class Look_to : MonoBehaviour
 {
 [Header("Look_to_the_center")]
 public Transform Target;
 Quaternion XLookRotation2D(Vector3 right)
 {
     Quaternion rightToUp = Quaternion.Euler(0f, 0f, 90f);
     Quaternion upToTarget = Quaternion.LookRotation(Vector3.forward, right);

     return upToTarget * rightToUp;
 }
 private void Update()
 {
     Vector3 relativePos = Target.position - transform.position;
     Quaternion angle = XLookRotation2D(relativePos);
     transform.rotation = Quaternion.Lerp(transform.localRotation, angle, Time.deltaTime);
 }

 private IEnumerator shot_to_center()   
 {
     yield return new WaitForSeconds(0.01f);
     GameObject tmpObj = Instantiate(ProjectilePrefab, startPoint, Quaternion.identity);
     foreach (GameObject f in backgroundObjects1)
     {
         Vector3 shoot = (rotationCenter.position - transform.position).normalized;
     yield return new WaitForSeconds(7.5f);
     tmpObj.GetComponent<Rigidbody2D>().AddForce(shoot * 100f);
     Destroy(tmpObj, 10F);
     }
         
 }
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

198 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to stop the Rotation at eulerAngle.z(-40) 0 Answers

3D First Person Game, 2D Enemy Face Player/Camera 1 Answer

Hi, Im trying to make a OnTriggerStay2D but with some delay, and I dont know how is, someone can help me please? I share mi code below. 0 Answers

Shoot until i release the button, but with a fire ratio 2 Answers

MouseLook Script "Pops" back to the last value when the script is enabled after being disabled or destoryed 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