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 Zeepblok · Sep 28, 2015 at 06:42 AM · rotationunity 2d

Cannonball not rotating with cannon

Hi,

I made a ship with a cannon. I put an empty object on the cannon to shoot, it works but the cannonBalls wont rotate with my ship

 using UnityEngine;
 using System.Collections;
 
 public class PlayerShoot : MonoBehaviour {
 
     public Transform firePoint;
     public GameObject CannonBall;
     public float fireRate = 0;
     public float Damage = 10;
 
   //  var bullet = Instantiate(bulletPrephab, GameObject.Find("Spawn Point").transform.position, transform.rotation);
 
     public LayerMask notToHit;
 
     float timeToFire = 0;
 
     // Use this for initialization
     void Start ()
     {
     
     }
     
     // Update is called once per frame
     void Update ()
     {
         if (fireRate == 0)
         {
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 shoot();
             }
         }
         else
         {
             if (Input.GetKey(KeyCode.Space) && Time.time > timeToFire)
             {
                 timeToFire = Time.time + 1 / fireRate;
                 shoot();
             }
         }
         
     }
 
     void shoot()
 
     {
         if(GameObject.Find("firePoint"))
         {
             Debug.Log("found it");
         }
         else
         {
             Debug.Log("its not here");
         }
        Instantiate(CannonBall, GameObject.Find("firePoint").transform.position, GameObject.Find("firePoint").transform.rotation);
     }
 }
 
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 SpaceManDan · Sep 28, 2015 at 08:16 AM 0
Share

I'm not sure I understand the question. Are the cannon balls not firing the right direction? Or are they loading into the canon and not following the ship as it moves and rotates? Can you explain the problem in more detail and use a bit more context.

avatar image Zeepblok SpaceManDan · Sep 28, 2015 at 08:21 AM 0
Share

The cannonballs always shoot of to the same direction in the same angle (always horizontallyand left to right) even if my ship is turned or at an angle

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SpaceManDan · Sep 28, 2015 at 08:21 AM

Ah, you are using worldspace and not local space I think. Try doing this. change your instantiate to this and tell me what happens

 Instantiate(CannonBall, GameObject.Find("firePoint").transform.position, GameObject.Find("firePoint").transform.localRotation);
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
avatar image
0

Answer by Zeepblok · Sep 28, 2015 at 09:30 AM

They are still only going horizontally :(

Comment
Add comment · Show 2 · 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 SpaceManDan · Sep 28, 2015 at 09:36 AM 0
Share

what is propelling the cannon ball? I looks like here they are only created. Where do you send them on their way? Is their a script on the cannon ball?

avatar image Zeepblok SpaceManDan · Sep 28, 2015 at 09:38 AM 0
Share

Yea sorry for not sending it with the question :P

 using UnityEngine;
 using System.Collections;
 
 public class CannonBallController : $$anonymous$$onoBehaviour
 {
 
     public float speed;
     public Layer$$anonymous$$ask notToHit;
     // Use this for initialization
     void Start ()
     {
     
     }
     
     // Update is called once per frame
     void Update ()
     {
         GetComponent<Rigidbody2D>().velocity = new Vector2(speed, GetComponent<Rigidbody2D>().velocity.y);
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         Destroy (gameObject);
     }
 }
 
avatar image
0

Answer by Zeepblok · Sep 28, 2015 at 01:05 PM

I FOUND IT :D thnx for all the help

I changed my scripts to this:

 using UnityEngine;
 using System.Collections;
 
 public class Player1CannonBall : MonoBehaviour {
 
 
     public float maxSpeed = 8f;
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         Vector3 pos = transform.position;
         Vector3 velocity = new Vector3(0, maxSpeed * Time.deltaTime, 0);
         pos += transform.rotation * velocity;
 
         transform.position = pos;
     }
 }
 

 using UnityEngine;
 using System.Collections;
 
 public class Player1Shoot : MonoBehaviour {
 
     public GameObject CannonBall;
     public float fireRate = 0;
     public float Damage = 10;
 
 
     float timeToFire = 0;
 
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (fireRate == 0)
         {
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 shoot();
             }
         }
         else
         {
             if (Input.GetKey(KeyCode.Space) && Time.time > timeToFire)
             {
                 timeToFire = Time.time + 1 / fireRate;
                 shoot();
             }
         }
 
     }
 
     void shoot()
 
     {
         Instantiate(CannonBall, GameObject.Find("player1FirePointRight").transform.position, GameObject.Find("player1FirePointRight").transform.rotation);
         Instantiate(CannonBall, GameObject.Find("player1FirePointLeft").transform.position, GameObject.Find("player1FirePointLeft").transform.rotation);
     }
 }

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 SpaceManDan · Sep 28, 2015 at 05:38 PM 0
Share

It's all you. but changing the cannon ball to check for the rotation definitely saved the day. :) Nice 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

31 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

Related Questions

Rotate and Drag Problem 1 Answer

Rotating in Z-Axis while still moving using rigidbody2D.velocity,Rotating in z axis while still moving using rigidbody2d 2 Answers

Vector3.RotateTowards doesn't work with angles of 180 degrees 0 Answers

Unity 2D - Rotating the controls themselves 0 Answers

Rotating around z-axis with OnScreen buttons 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