Question by
zikyoubi · Mar 14, 2016 at 01:58 PM ·
rotationgameobjectinstantiatetransformdestroy
Canon Ball shooting with Instiate
Hello ,
Well I created a mini games when I direct my Canon ball and when I click on the Space key , Red Ball must be projected to the direction of my Canon ! everything works (time down , rotating the barrel, instantiation of prefab .. ) but the problem is that when I click on space, my cannon projectile force does not apply , I just set it on 1800F as a Force! well here's my images and scripts that I use :
I put a script on the Canon to control the rotation, and a script on the gameobject or I add my prefab of the red ball :
Here is the script of my CanonController :
using UnityEngine;
using System.Collections;
public class CanonController : MonoBehaviour {
private float horizontal;
public float maxAngle = -30;
public float minAngle = -145;
public float rotateSpeed = 1f;
float currentAngle = -90;
Vector3 initialAngle;
Quaternion rotationTarget;
// Use this for initialization
void Start () {
initialAngle = transform.eulerAngles;
}
// Update is called once per frame
void Update () {
horizontal = Input.GetAxisRaw("Horizontal");
if (horizontal > 0)
{
if(currentAngle < maxAngle)
{
currentAngle += rotateSpeed;
}
}
if (horizontal < 0)
{
if (currentAngle > minAngle)
{
currentAngle -= rotateSpeed;
}
}
rotationTarget = Quaternion.AngleAxis(currentAngle, Vector3.back);
transform.localRotation = rotationTarget;
}
}
And the script of my prefab :
using UnityEngine;
using System.Collections;
public class shootingGun : MonoBehaviour {
public GameObject gunPrefab;
GameObject gunPrefabClone;
public float forForce = 300.0f;
private float CurrentTime = 0;
public float TimeToShoot = 1.5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
CurrentTime += Time.deltaTime;
}
void FixedUpdate()
{
if (Input.GetButtonDown("Jump") && CurrentTime >= TimeToShoot)
{
ShootBallMethod();
CurrentTime = 0;
}
}
void ShootBallMethod()
{
gunPrefabClone = Instantiate(gunPrefab, transform.position, Quaternion.identity) as GameObject;
Rigidbody2D rb2dForClone = gunPrefabClone.GetComponent<Rigidbody2D>();
rb2dForClone.AddForce(transform.forward * forForce,ForceMode2D.Force);
Destroy(gunPrefabClone, 1.5f);
}
}
I wish to have the same result as this tutorial :
https://youtu.be/Mwr3oh7wlRc?t=10m58s
Thank you !
Comment