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 ragefordragons · Nov 02, 2019 at 04:28 PM · rotationinstantiatetransformtransform.position

Bullet Not Firing in Correct Direction

Im making a 2D bullet hell, and Ive added a feature on the weapons for it to fire three bullets at a time in a shotgun style spread. My code instatiates the bullet from another script and then isdie the script on the bullet below, it should first set its rotation using the faceMouse() function. Since the function sets the transform.up to face the mouse, I assumed that using transform.tralsate and moving towards it would work. And while if you aim straight up it works fine, as you move your mouse to the left and right of the play (in a top down game) the bullets start firing in drastically wrong angles, but it seems to be consistantly wrong. I already made sure the bullets I instatiate are at 0,0,0 on their rotation to begin with, so I honeslty have no idea why this isn't working. Heres the bullet script, any ideas would be much appricated.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Projectile : MonoBehaviour {

 public float bulletLife;

 public float turnSpeed;

 Vector3 moveDirection;

 public enum bulletDirection{straight, left, right};

 [HideInInspector]
 public bulletDirection dir;


 void Start()
 {
     StartCoroutine(destroyAfterTime());

     if (dir == bulletDirection.straight) {
         moveDirection = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position);
         faceMouse();

     } else if (dir == bulletDirection.left){
         moveDirection = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position);
         faceMouse();
         transform.Rotate(0,0,10);
         //moveDirection = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x - 2, Camera.main.ScreenToWorldPoint(Input.mousePosition).y - 2) - transform.position;
     } else if (dir == bulletDirection.right){
         moveDirection = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position);
         faceMouse();
         transform.Rotate(0,0,-10);
         //moveDirection = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x + 2, Camera.main.ScreenToWorldPoint(Input.mousePosition).y + 2) - transform.position;
     }

     moveDirection.z = 0;
     moveDirection.Normalize();


 }

 private void Update()   
 {
         //transform.position = transform.position + moveDirection * WeaponManager.instance.currentWeapon.shootSpeed * Time.deltaTime;
         transform.Translate(transform.up * WeaponManager.instance.currentWeapon.shootSpeed  * Time.deltaTime);
 }

 public void faceMouse(){

     Vector3 mousePosition = Input.mousePosition;
     mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
     Vector2 direction = new Vector2(mousePosition.x - transform.position.x, mousePosition.y - transform.position.y);
     transform.up = direction;
 }


 void OnTriggerEnter2D(Collider2D other)
 { 

     if (other.tag == "wall")
     {

         VisualEffects.spawnHitEffect(transform);
         Destroy(gameObject);
      
     } else if (other.tag == "enemy")
     {

         VisualEffects.spawnHitEffect(transform);
         Destroy(gameObject);
     }
 }



 IEnumerator destroyAfterTime()
 {
     yield return new WaitForSeconds(bulletLife);
     VisualEffects.spawnHitEffect(transform);
     Destroy(gameObject);
 }

}

Comment
Add comment · Show 3
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 DCordoba · Nov 03, 2019 at 06:02 AM 0
Share

I think the problem is when you get the world position of mousePosition on line 47

 mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);

as say here, The z position is in world units from the camera, if you use mouse position directly z is 0 ( Input.mousePosition converted from vector2 to vector3), this probably is creating some weird values (on z = 0 all the points have the same value... is the start clipping plane) to get a clumsy approximation you can try the distance between the camera and the player

  Vector3 mousePosition = Input.mousePosition;
  mousePosition.z = (Camera.main.transform.position- transform.position).magnitude;
  mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);

of course, if this is a fixed value, as usual on 2d games, the camera always follow the player at the same distance from scene, you can store it on starting scene and evade the magnitude cost of cpu each frame

avatar image ragefordragons DCordoba · Nov 03, 2019 at 05:58 PM 0
Share

This didn't seem to change anything... although I think your right in saying that the problem is when it gets the position of the mouse. Ive noticed that the bullets point towards the mouse when facing straigh up, but as you move left and right with the mouse the angle is more skewed. It seems like the problem occurs when I set transform.up to the position of the mouse, is there some other way I should have the bullets face the mouse? All I want to do is set that transform.up angle to to face the mouse, then rotate other bullets based off of that. Lets go from there.

avatar image DCordoba ragefordragons · Nov 03, 2019 at 06:41 PM 0
Share

maybe setting the direction it on screen space?

 Vector3 playerPosition =  Camera.main.WorldToScreenPoint(transform.position);
  Vector2 direction =  Input.mousePosition - playerPosition;
  transform.up = direction.normalized;


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

192 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

Related Questions

How to get a proper ramming effect? 1 Answer

Dropping an object behind player in respect to rotation,How to drop an object behind the player 1 Answer

How to instantiate on custom rotation? 1 Answer

Why can't I get the transform from an instantiated object? 1 Answer

Planes not instantiating properly on gameobject location 2 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