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 Mystic_Quest · Feb 04, 2017 at 07:43 PM · axisprojectiletopdownlookattarget

2D Top down projectile help (not moving)

I got a top down rpg, zelda style, a boss and a child on the boss which is the source that throws fireball projectiles. This is the script on the child.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BossAttack2 : MonoBehaviour {
 
     public GameObject fireball;
     public GameObject genericfireball;
     public GameObject player;
     public GameObject fire;
 
     public Rigidbody2D body;
     public Transform target;
     public Transform location;
     public Transform source;
 
     public float speed = 0.5f;
 
     // Use this for initialization
     void Start ()
     {
         player = GameObject.FindWithTag("Player");
         fire = GameObject.Find("fire");
         source = GetComponent<Transform>();
         location = source.transform;
     }
     
     // Update is called once per frame
     void Update () {
         target = player.transform;
         location.LookAt(target);
     }
 
 
     public void Fireball()
     {
         {
             genericfireball = Instantiate(fireball, location.position, location.rotation) as GameObject;
             genericfireball.GetComponent<Rigidbody>();
             body = genericfireball.GetComponent<Rigidbody2D>();
             body.velocity = transform.forward * speed;
         }
     }
 }

With this, the fireball comes out of the child object and goes towards player as intended. But the projectile rotates around the y axis, instead of z (which would make the fireball look at the player).

If I understand correctly, with "location.position and location.rotation" it inherits the position of the child and its rotation (that is supposed to look at the player at all times). If I use instantiate without these the instances spawn at a random location and obviously don't turn around z.

Is there any way to make it look at the player with the z axis and leave y untouched?

Update: I used this code in the update function and managed to get the fireball to look at the player. Now the problem is that it's not moving for some reason.

         float AngleRad = Mathf.Atan2(target.transform.position.y - source.transform.position.y, target.transform.position.x - source.transform.position.x);
         float AngleDeg = (180 / Mathf.PI) * AngleRad;
         this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg + 90);

Update: Managed to make it move but it only moves towards a specific direction that is not affected by the player at all... Added body.velocity = new Vector2(target.position.x, target.position.y);

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.

Follow this Question

Answers Answers and Comments

90 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

Related Questions

Why are my projectiles spawning in batches? 1 Answer

Rotate around own vertical axis to face mouse 0 Answers

Make an object follow and face an object while maintaining distance from a different object. 0 Answers

Snap to grid 2 Answers

How to find current vertical axis 1 Answer


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