Question by
ySkilly · Sep 25, 2017 at 08:07 PM ·
instantiatebulletmoving
Moving a spawned shuriken
Hello guys,
So i'm struggling with my shuriken script, it basically is a bullet script and even though i searched on the documentation i can't get my script to work..
It should simply move forward depending on the direction my player is facing.
Here are my scripts :
public class Shuriken : MonoBehaviour {
private Rigidbody2D myRigidBody;
[SerializeField]
private float speed;
private Vector2 direction;
public GameObject player;
// Use this for initialization
void Start () {
myRigidBody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate () {
myRigidBody.velocity = direction.normalized * speed;
direction = player.transform.eulerAngles;
}
private void OnBecameInvisible()
{
Destroy(gameObject);
}
}
Thanks for any help provided : )
Comment
Answer by goutham12 · Sep 26, 2017 at 06:57 AM
try like
direction = player.transform.forword; myrigidbody.velocity = direction*normalized*speed;
Answer by ySkilly · Sep 26, 2017 at 02:49 PM
I did try, nothing happens
But i found the problem, it is because in my player script everytime i don't press any movement button my direction goes back to Vector2.zero
protected override void Update()
{
GetInput();
base.Update();
}
private void GetInput()
{
direction = Vector2.zero;
if (Input.GetKey(KeyCode.Z))
{
exitIndex = 1;
direction += Vector2.up;
}
if (Input.GetKey(KeyCode.Q))
{
exitIndex = 3;
direction += Vector2.left;
}
if (Input.GetKey(KeyCode.S))
{
exitIndex = 0;
direction += Vector2.down;
}
if (Input.GetKey(KeyCode.D))
{
exitIndex = 2;
direction += Vector2.right;
}
if (Input.GetKeyDown(KeyCode.E))
{
if (!isThrowing && !IsMoving)
{
ThrowRoutine = StartCoroutine(Throwing());
}
}
}
But if i don't write direction = Vector2.zero; my character won't stop moving, any ideas ?