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 /
This question was closed Mar 17, 2016 at 02:53 AM by sunderplugs11 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by sunderplugs11 · Mar 16, 2016 at 06:44 AM · 2dphysicsrigidbody2dprojectilevector2

2d cannon projectile not following expected path

Trying to: take rotation.z from parent, Cannon3. take this angle, convert to radians use this to create Vector2(Cos(rad), Sin(rad)) access slider value for power apply scalar power to vector2. use this vector2 in addForce.impulse to propel in arc... like a cannon

http://imgur.com/QnS9xnO

I swear Im just copy pasting at this point and not working right

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class CannonBallShooting : MonoBehaviour {
     public GameObject cannonBall;
     GameObject clone;
     public Slider powerSlider;
     public float force;
     float radAngle;
     Vector2 shootingVector;
     // Use this for initialization
     void Start () {
         radAngle = Mathf.Deg2Rad * transform.rotation.z;
         shootingVector = new Vector2(Mathf.Cos(radAngle), Mathf.Sin(radAngle));
 
 
         
         }
     
     // Update is called once per frame
     void Update () {
         
         Debug.Log(powerSlider.value);
 
     }
     public void Shoot()
     {
         clone = Instantiate(cannonBall, transform.position, transform.localRotation) as GameObject;
         clone.transform.parent = this.transform;
         clone.GetComponent<Rigidbody2D>().velocity =(shootingVector*powerSlider.value);
 
 
 
 
         
     }
 }
 
Comment
Add comment · Show 2
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 sunderplugs11 · Mar 17, 2016 at 01:19 AM 0
Share

I' ve found my problem but cant figure out how to fix it.

I believe I am trying to apply a quarternion into a float, in the line angle = transform.rotation.z and then I am applying that into the vector as a float?

avatar image sunderplugs11 sunderplugs11 · Mar 17, 2016 at 02:03 AM 0
Share

well, no. both transform.rotation.z and transform.rotation.eulerangles.z are floats.

so I dont understand why they dont work?

Im even just trying to use the following and its not printing the angle to the console. Ive also tried just accessing it with transform.rotation.z, then by getcomponent, then by a public transform. none of them went above 0 in the inspector.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class CannonBallShooting : $$anonymous$$onoBehaviour {
     public GameObject cannonBall;
     GameObject clone;
     public Slider powerSlider;
     public float force;
     public Transform angleSource;
     public float angle;
     Rigidbody2D rg;
     Vector2 shootingVector;
     // Use this for initialization
     void Start () {
         rg = GetComponent<Rigidbody2D>();
         angle = rg.transform.rotation.eulerAngles.y;
         shootingVector = new Vector2($$anonymous$$athf.Cos(angle), $$anonymous$$athf.Sin(angle));
 
 
         
         }    
     
     // Update is called once per frame
     void Update () {
         force = powerSlider.value;
         Debug.Log(powerSlider.value);
         Debug.Log(angle);
 
     }
     public void Shoot()
     {
         clone = Instantiate(cannonBall, transform.position, transform.localRotation) as GameObject;
         clone.transform.parent = this.transform;
         clone.GetComponent<Rigidbody2D>().AddForce(shootingVector*powerSlider.value, Force$$anonymous$$ode2D.Impulse);
 

1 Reply

  • Sort: 
avatar image
0

Answer by sunderplugs11 · Mar 17, 2016 at 02:53 AM

I had it in Start, not in Update.

dont code while sleepy, kids.

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

Follow this Question

Answers Answers and Comments

84 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

Related Questions

Rigidbody projectile misses collider 1 Answer

Adding velocity to an object in a brick breaker game 0 Answers

AddForce90 Jump while moving Rigidbody with MovePosition 1 Answer

Why/How 2d tower of blocks collapse? 0 Answers

Addforce for 2d game is not doing anything. 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