- Home /
Bullet Follows Player Orientation.
I have a bullet script that needs to travel forward relative to the player's orientation on the X axis, but the bullet prefab changes direction whenever the player does. Please help, because my brain hurts :L
void FlipVelocity(bool isRight) {
if (isRight)
{
sr.flipX = false;
transform.Translate(velocity * Time.deltaTime, 0, 0);
}
else if (!isRight) {
sr.flipX = true;
transform.Translate(-velocity * Time.deltaTime, 0, 0);
}
}
private void Update()
{
if (pc.isFacingRight)
{
FlipVelocity(true);
}
else if (!pc.isFacingRight){
FlipVelocity(false);
}
}
Answer by SpeakBeaver · Aug 08, 2018 at 01:00 AM
You can use Vector3.Lerp ( and set Z to 0 for 2D) to give a Direction to your Bullet.
And your bullet is changing of orientation maybe because she is the children of the player, have you check this ?
Hope it's help !
Answer by brok0de · Aug 08, 2018 at 02:59 AM
Um I'm not sure if it's a child tbh. It is instantiated in the PlayerController. Would that make it a child? Also thanks for the advice! I'll see if it works :)
to check if your bullet is not a child just Debug.Log the transform parent. It can be a child if your prefab is related to your player
Nope :L The bullet has no parent. I figured as much tbh, my logic in the script isn't exactly explicit. But I have no clue how to write it. $$anonymous$$aybe I could check for a previous state on the player and work on serialization for the bullets? But that sounds really complicated for the kind of work I need the script to do. Oh well.
If I had to do something similar, I think I would do a Coroutine with a given axis to follow or with a director vector if you have a trigger system. Something like that (I didn't try but juste to give you the idee)
GameObject bulletPrefab;
public void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0)) //left clic
{
Vector3 playerPos = new Vector3(2, 2, 2); //Get position of your player
GameObject newBullet = Instantiate(bulletPrefab);
newBullet.transform.position = playerPos;
float Xplayer = 1; //Get direction of your player x or -x
StartCoroutine($$anonymous$$yBulletCoroutine(newBullet, new Vector3(Xplayer, 0, 0)));
}
}
private IEnumerator $$anonymous$$yBulletCoroutine(GameObject prefab, Vector3 direction)
{
//if you have no end juste make it infinit but not sure it's a good way to do ^^
while (true) {
float coefTranslate = 1; // to adjuste your speed
prefab.transform.Translate(direction * coefTranslate);
}
//if you have a target position given in argument use insted Lerp and give a time too
float time = 2;
float actual = 0;
Vector3 targetPos = new Vector3(1, 1, 1);
while (actual < time)
{
prefab.transform.position = Vector3.Lerp(prefab.transform.position, targetPos, actual / time);
actual += Time.deltaTime;
yield return null;
}
}
With this technic you can have multiple bullet independant of the player If you don't know [Couroutine][2] it's really really usefull and I can only advice you to watch how it works because somehow you gonna need this one day.
Your answer
Follow this Question
Related Questions
Help Me! I want to make a pixel fairy! 1 Answer
Two Polygon2D Collider do not Collide with Each Other (Solved) 2 Answers
[2D] Sprite flickers/is choppy when moving 1 Answer
Farseer Physics and Character Control 0 Answers
Weird physics issue after disabling and re-enabling player gameobject. 3 Answers