Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 PythonMaster18 · Jul 12, 2017 at 09:27 PM · shootingcamera rotatebetay-axisnegative

Won't Shoot if Y Rotation Value Is Negative

My program allows a player (which the main camera) to shoot balls in the direction the camera is angled/facing. This works except when the y rotation value of the camera is a negative number.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraController : MonoBehaviour {
 
     public float speed;
     public GameObject projectile;
     public GameObject hit_object;
 
     private Vector3 moveDirection = Vector3.zero;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 pos = transform.position;
         pos.y = 3;
         transform.position = pos;
 
         if (Input.GetKey(KeyCode.RightArrow))
         {
             transform.localPosition += transform.right * Time.deltaTime * speed;
         }
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             transform.position += -transform.right * Time.deltaTime * speed;
         }
         if (Input.GetKey(KeyCode.DownArrow))
         {
             transform.position += -transform.forward * Time.deltaTime * speed;
         }
         if (Input.GetKey(KeyCode.UpArrow))
         {
             transform.position += transform.forward * Time.deltaTime * speed;
         }
 
         if (Input.GetKeyDown(KeyCode.F))
         {
             GameObject bullet = Instantiate(projectile, transform.position, transform.rotation);
             bullet.GetComponent<Rigidbody>().AddRelativeForce(new Vector3(0, 0, 4000));
         }
 
         if (Input.GetKeyDown(KeyCode.E))
         {
             var pos_x = transform.position.x + Random.Range(-500F, 500F);
             var pos_z = transform.position.z + Random.Range(-500F, 500F);
             Instantiate(hit_object, new Vector3(pos_x, transform.position.y, pos_z), transform.rotation);
         }
 
         transform.Rotate(0f, Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0f, Space.World);
         transform.Rotate(-Input.GetAxis("Vertical") * speed * Time.deltaTime, 0f, 0f, Space.Self);
     }
 }

This happens regardless of the size of the sphere (has Rigidbody component) or the current position of the camera.

alt text

As you can see, the balls that were fired went towards the top of the field based on the snippet view which is a direct result of the balls fired at a y-rotation value from 0 to 180 and rolling away due to physics. There are no balls at the bottom of the screen since if the rotational y value is negative, the balls will be shot at 0 or 180 degrees, whichever is closer.

How do I allow balls to be fired even if the rotational value of y is negative? The y-value mentioned:

alt text

capture.png (372.6 kB)
capture.png (3.6 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 aldonaletto · Jul 13, 2017 at 01:01 AM

There's nothing wrong with your script - I've tested it and the projectiles were shot in the camera's forward direction independently of its rotation, as expected. I suspect that you may have something behind the camera, some forgotten object or collider that is blocking the projectiles when the camera turns to its direction.

Comment
Add comment · Show 1 · 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 PythonMaster18 · Jul 13, 2017 at 01:36 AM 1
Share

You were right...ish. The unnecessary CharacterController component disallowed me to shoot when y was negative. Thank you so much!

avatar image
0

Answer by Jwizard93 · Jul 12, 2017 at 11:34 PM

 bullet.GetComponent<Rigidbody>().AddForce(transform.forward * 4000);

Try swapping out line 44 with this

Comment
Add comment · Show 1 · 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 PythonMaster18 · Jul 13, 2017 at 12:23 AM 0
Share

Unfortunately, that doesn't work.

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

71 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

Related Questions

Y rotation of the Camera doesn't clamp 1 Answer

Unity character dissapearing when I hit play 0 Answers

is it possible to have your camerarotation be *-1 of you player rotation. 1 Answer

How to Rotate the camera up and down using mouse input. 1 Answer

how to rotate camera on x and y axis with mouse? 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