- Home /
Why does my code shoot up but not forward?
I dont get why this is doing what it does my code is using UnityEngine; using System.Collections;
public class ProjectileController : MonoBehaviour {
public float fireSpeed;
public Rigidbody2D myRigidbody2D;
public PlayerController player;
public float fireLife;
public float fireLifeStore;
// Use this for initialization
void Start () {
myRigidbody2D = GetComponent<Rigidbody2D> ();
player = FindObjectOfType<PlayerController> ();
}
// Update is called once per frame
void Update () {
GetComponent<Rigidbody2D>().AddForce(transform.forward * fireSpeed);
//myRigidbody2D.velocity = new Vector2 (fireSpeed, myRigidbody2D.velocity.y);
fireLife -= Time.deltaTime;
if (fireLife <= 0)
{
Destroy(gameObject);
}
}
}
Answer by D3Duck · Sep 01, 2015 at 08:45 PM
Is the script attached to the projectile or a child?
Select the thing that it is attached to in the inspector. Make sure you use local for the gizmos and look where it is pointing at. Is it up or forward? (probably up because your script looks fine ;) )
Its attached to the projectile
and how do i check where it is point towards
aand found a wierd thing where it works when i use GetComponent().AddForce(transform.up * fireSpeed); but i dont want that direction.
So you fixed it?
Select your projectile in the scene view and see where the blue arrow points at. That's forward.
Also, make sure that when you instantiate you do so with Quaternion.Identity (no rotation).
nope its not fixed nd im not seeing a blue arrow only red right and green up
I figured it out i didnt need transform.forward i need transform.right which is the red line thanks all for the help.
$$anonymous$$y bad I didn't realize you were making a 2D game. Anyway, glad you found it.
Answer by Jessespike · Sep 01, 2015 at 08:36 PM
transform.forward is referring to the ProjectileController. I'm guessing ProjectileController is on an object that doesn't move, I don't know how you set this up. What if you use the player's transform?
GetComponent<Rigidbody2D>().AddForce( player.transform.forward * fireSpeed);
Answer by Orc_Horn_Productions · Sep 02, 2015 at 07:14 AM
Hi @Treven,
Might I make the suggestion that you attach your projectile controller to your player character and then use scripting to instantiate/move your projectile from it's desired location.
This would be a more common way of creating projectiles in game and probably easier to transfer onto new models/projects if you plan on future proofing your code.
You can get a good introduction to this concept in either of the following tutorials:
https://unity3d.com/learn/tutorials/projects/space-shooter/shooting-shots?playlist=17147 https://unity3d.com/learn/tutorials/projects/survival-shooter/harming-enemies?playlist=17144
I'd be more than happy to look over your script in more detail if there's anything your still not sure about and help anyway I can.
Ross
okay the tutorials didnt help and attaching the script to the player character did not help. is there a way to do transform.forward accross the x axis cause is only goes across the Z axis which dosent work in unity 2D.