- Home /
Bullet follows player mouvement when It instintiates.
I want it to just translate to the direction i give it not follow my mouvement. Thanks for your help! bullet script using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Bullet : PhysicsObject {
public float Speed;
public float LifeTime;
public GameObject destroyEffect;
public Rigidbody2D rg2D;
void Start()
{
Invoke("DestroyEffect", LifeTime);
}
private void Update()
{
if ( GameObject.Find("ak").GetComponent<SpriteRenderer>().flipX )
{
transform.Translate(-transform.right * Speed * Time.deltaTime);
GetComponent<SpriteRenderer>().flipX = true;
}
else
{
transform.Translate(transform.right * Speed * Time.deltaTime );
GetComponent<SpriteRenderer>().flipX = false;
}
}
void DestroyProjectile()
{
Instantiate(destroyEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
Weapon script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class weapon : MonoBehaviour { public GameObject Projectile; public Transform ShootPoint;
private float TimeBtwShots;
public float StartTimeBtwShots;
public float MaxSpeed = 7f;
public float jumpTakeOffSpeed = 7;
private SpriteRenderer SpriteRenderer;
private float MouvementSpeed;
// Update is called once per frame
private void Update()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
if (TimeBtwShots <= 0) ;
{
if (Input.GetButtonDown("Fire1"))
{
Instantiate(Projectile, ShootPoint.position, transform.rotation);
TimeBtwShots = StartTimeBtwShots;
}
else
{
TimeBtwShots -= Time.deltaTime;
}
}
}
void FixedUpdate()
{
if (GetComponent<Rigidbody2D>().velocity.x < 0)
{
GetComponent<SpriteRenderer>().flipX = true;
}
if (GetComponent<Rigidbody2D>().velocity.x > 0)
{
GetComponent<SpriteRenderer>().flipX = false;
}
}
}
Answer by Tsaras · Mar 30, 2019 at 08:57 AM
If I understand correctly, you are basing the projectile's movement based on the facing of your ak in the Update method (which means in every frame). This is not a good solution because obviously your facing can change. What you can easily do is have the weapon script communicate to the bullet script the direction with which to move.
In bullet script:
int direction;
public void SetDirection(int _direction)
{
direction = _direction;
}
void Start()
{
Invoke("DestroyEffect", LifeTime);
if (direction < 0)
{
GetComponent<SpriteRenderer>().flipX = true;
}
else
{
GetComponent<SpriteRenderer>().flipX = false;
}
}
private void Update()
{
transform.Translate(transform.right * direction * Speed * Time.deltaTime);
}
In weapon script:
int direction;
SpriteRenderer rend; //cache this, don't look for it every fixed update
Rigidbody2D rb2d; //same
void Start()
{
rend = GetComponent<SpriteRenderer>();
rb2d= GetComponent<Rigidbody2D >();
}
void FixedUpdate()
{
if (rb2d.velocity.x < 0)
{
rend.flipX = true;
direction = -1;
}
if (rb2d.velocity.x > 0)
{
rend.flipX = false;
direction = 1;
}
}
private void Update()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
if (TimeBtwShots <= 0) ;
{
if (Input.GetButtonDown("Fire1"))
{
GameObject bullet = Instantiate(Projectile, ShootPoint.position, transform.rotation);
Bullet bulletScript = bullet.transform.GetComponent<Bullet>(); //get Bullet script
bulletScript.SetDirection(direction); //set direction same as the direction of weapon
TimeBtwShots = StartTimeBtwShots;
}
else
{
TimeBtwShots -= Time.deltaTime;
}
}
}
I apparently have a problem the bullet is not translating it just stands still. I forgot to tell that I have made the bullet a prefab is that a problem?
Check if the bulletScript is indeed getting cached and SetDirectoin is called. Use a breakpoint to pause execution and move line by line to see what the variables hold. If the bullet is not moving then something in this line is probably 0. transform.Translate(transform.right * direction * Speed * Time.deltaTime);
Your answer
Follow this Question
Related Questions
My player is not rotating upwards when it moves upwards. Any advice? 1 Answer
Shooter 2D - Problem with rotation of bullet to face direction (velocity) 2 Answers
2D RPG boomerang instantiation help. 2 Answers
How do I flip a sprite based on parent's rotation? 1 Answer
Slow Sprite Rotation Toward Movement Direction - Top-Down 2D Game 0 Answers