Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by oliveracooper06 · Feb 15, 2021 at 10:35 AM · topdowntop down shooter

need help with top down shooter,need help with topdown shooting

my shooting is doing wierd stuff

video of whats wrong: https://youtu.be/Nw1M-OdTF_A

player script: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class playercontrol : MonoBehaviour
 {
     public Camera cam;
     public float speed = .25f;
     public GameObject bulletobj;
     public float bulletspeed = 10;
     public Transform shootpos;
 
     // Update is called once per frame
     void Update()
     {
 
         float movex = Input.GetAxisRaw("Horizontal");
         float movey = Input.GetAxisRaw("Vertical");
         transform.position += new Vector3(movex, movey).normalized * Time.deltaTime *speed;
 
         Vector3 mousepos = cam.ScreenToWorldPoint(Input.mousePosition);
 
         float angle = Mathf.Atan2(mousepos.y - transform.position.y, mousepos.x - transform.position.x);
         angle *= Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(0, 0, angle-90);
         if (Input.GetButtonDown("Fire1"))
         {
             GameObject bullet;
             //add shooty code
             bullet = Instantiate(bulletobj, shootpos.transform.position, shootpos.transform.rotation);
 
         }
 
 
     }
 
 
 }
 

bullet prefab script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class bulletmove : MonoBehaviour
 {
     public float bulletspeed = 100f;
 
   
     public void Update()
     {
         // Move the object forward at 1 unit/second.
         transform.Translate(transform.up * Time.deltaTime * bulletspeed);
 
     }
 
 }
 


,so im having a problem with the angle of shooting on a topdown shooter script

video of whats wrong: https://youtu.be/Nw1M-OdTF_A

player script: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class playercontrol : MonoBehaviour
 {
     public Camera cam;
     public float speed = .25f;
     public GameObject bulletobj;
     public float bulletspeed = 10;
     public Transform shootpos;
 
     // Update is called once per frame
     void Update()
     {
 
         float movex = Input.GetAxisRaw("Horizontal");
         float movey = Input.GetAxisRaw("Vertical");
         transform.position += new Vector3(movex, movey).normalized * Time.deltaTime *speed;
 
         Vector3 mousepos = cam.ScreenToWorldPoint(Input.mousePosition);
 
         float angle = Mathf.Atan2(mousepos.y - transform.position.y, mousepos.x - transform.position.x);
         angle *= Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(0, 0, angle-90);
         if (Input.GetButtonDown("Fire1"))
         {
             GameObject bullet;
             //add shooty code
             bullet = Instantiate(bulletobj, shootpos.transform.position, shootpos.transform.rotation);
 
         }
 
 
     }
 
 
 }

bullet prefab script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class bulletmove : MonoBehaviour
 {
     public float bulletspeed = 100f;
 
   
     public void Update()
     {
         // Move the object forward at 1 unit/second.
         transform.Translate(transform.up * Time.deltaTime * bulletspeed);
 
     }
 
 }
 

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 MUG806 · Feb 15, 2021 at 05:15 PM

Took me a while but I found the mistake! Luckily its a simple one, just hard to spot at a distance. The problem lies here:

 // Move the object forward at 1 unit/second.
 transform.Translate(transform.up * Time.deltaTime * bulletspeed);

By default, transform.Translate moves the object in its own space, not world space. So you don't want to pass in directions relative to its own direction like "transform.up". To fix this you can do either of the following:

 // Pass in Vector3.up, which is an absolute up, rather than a relative up
 transform.Translate(Vector3.up * Time.deltaTime * bulletspeed);
 
 //use the optional parameter to tell unity you are giving a world space direction, not local.
 transform.Translate(transform.up * Time.deltaTime * bulletspeed, Space.World);

Both are equivalent so use whichever makes the most sense to you!

EDIT: Bonus tip, you might wanna try out this more readable (and trig free!) "look at mouse" solution:

 //make sure the mouse pos is on the same depth as the transform
 mousepos.z = transform.position.z;
 //Look into the screen, but rotate the "up" of the transform to face mousepos
 transform.rotation = Quaternion.LookRotation(Vector3.forward,mousepos-transform.position);
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

113 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

Related Questions

3D Top Down Rotation (PLZ HELP) 1 Answer

Top-down character running animation based on character direction 0 Answers

Need some help calculating Euler Angles 1 Answer

Joystick character rotation wrong movement 1 Answer

2D Top Down Shooting Problem 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