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);
}
}
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.
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
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);
Answer by Zeepblok · Sep 28, 2015 at 09:30 AM
They are still only going horizontally :(
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?
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);
}
}
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);
}
}
It's all you. but changing the cannon ball to check for the rotation definitely saved the day. :) Nice work.