Hey everyone, I hate to make another post asking for help so soon, but I’ve spent about six hours staring at this code trying everything I possibly can. I’ve even been broadcasting my coding and for hours and numerous (much more experienced programmers) have hit a wall with this as well.
Every time my Player shoots the projectile simply spawns in front of him and falls to the ground. Below I’ve included my script, images of components, etc…
using UnityEngine;
using System.Collections;
public class Dice : MonoBehaviour {
public float fireRate = 0;
public float damage = 10;
public LayerMask whatToHit;
public GameObject d6;
float timeToSpawnEffect = 0;
public float effectSpawnRate = 1;
float timeToFire = 0;
Transform throwPoint;
// Use this for initialization
void Awake () {
throwPoint = transform.FindChild ("throwPoint");
if (throwPoint == null) {
Debug.LogError ("No ThrowPoint?");
}
}
// Update is called once per frame
void Update () {
if (fireRate == 0) {
if (Input.GetButtonDown ("Fire1")) {
Shoot ();
}
}
else {
if (Input.GetButton ("Fire1") && Time.time > timeToFire) {
timeToFire = Time.time + 1/fireRate;
Shoot ();
}
}
}
void Shoot () {
Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector2 firePointPosition = new Vector2 (throwPoint.position.x, throwPoint.position.y);
RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, whatToHit);
if (Time.time >= timeToSpawnEffect) {
Effect ();
timeToSpawnEffect = Time.time + 1/effectSpawnRate;
}
Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
if (hit.collider != null) {
Debug.DrawLine (firePointPosition, hit.point, Color.red);
Debug.Log ("We hit " + hit.collider.name + " and did " + damage + " damage.");
}
}
void Effect () {
GameObject d6Clone = Instantiate (d6, throwPoint.position, throwPoint.rotation) as GameObject;
d6Clone.GetComponent<Rigidbody2D>().AddForce(transform.forward,ForceMode2D.Impulse);
}
}
And this is what it looks like after he shoots the projectile and it just falls straight down. It does have physics though so I can push it and it falls and whatnot.
I think I misread the problem.
Correct me if I am wrong, but the projectile, when created, does not move forward, it just falls yes?
Well, from what I can tell, you do not seem to be applying much, if any force to the object after instantiating it.
Unless I am just missing it somewhere.
The only line I see where you add force is line 60, but you are not adding much force, as you are only passing in its forward vector.
You might try creating a public float speed variable, and multiply transform.forward by that variable, of course do not forget to set that speed value in the inspector for your dice.
It does move but only after the player pushed it. On Spawn it would just go slightly forward and then fall and slide forward.
However a kind programmer entered my broadcast and found what I was doing wrong. Seems I was using the forward varible which if for the Z axis. Changed the code and can be found below.
Now my only problem is shooting only goes forward from whichever direction my character is facing and not shooting in the direction my mouse is clicking. Physics are pretty okay now.
Bump… and for anyone wanting to use a code that will have this work properly, I’ve got the following
Place this script on your player
using UnityEngine;
using System.Collections;
public class DiceSpawn : MonoBehaviour {
public GameObject d6; // this is where you'll put your prefab bullet
public Transform throwPoint; // create a firePoint attached to your player
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
Instantiate (d6,throwPoint.position,Quaternion.identity);
}
}
}
and this one on your projectile prefab
using UnityEngine;
using System.Collections;
public class DiceV2 : MonoBehaviour {
private Vector2 mousePosition;
private float step;
private Vector2 target;
private Vector2 myPos;
private Vector2 direction;
private float bulletSpeed;
void Start()
{
bulletSpeed = 15;
myPos = new Vector2(transform.position.x,transform.position.y);
target = Camera.main.ScreenToWorldPoint( new Vector2(Input.mousePosition.x, Input.mousePosition.y) );
direction = target - myPos;
direction.Normalize();
GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
}
void Update ()
{
}
}