How do i fix the position of the bullet sprite when character flips.
Im using this as a reference but in code he uses his mouses position for the direction and not just vertical/horizontal.
Heres the script im using, its not finished because i wouldnt know what to call to flip it.
I have FirePoint gameObject at the tip of the assault rifle, i thought by default it would flip since its a child under character…
using UnityEngine;
using System.Collections;
public class Shooting : MonoBehaviour {
public GameObject shot;
public float Damage = 10;
public float fireRate = 0;
float timeToFire = 0;
Transform firePoint;
// Use this for initialization
void Awake () {
firePoint = transform.FindChild("FirePoint");
if (firePoint == null)
{
Debug.LogError("No FirePoint located");
}
}
// 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()
{
Instantiate(shot, firePoint.position, firePoint.rotation);
Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast (firePointPosition, )
}
}
The way I’m doing it, is I have two fireTransform (same as your FirePoint) game objects that I use to spawn my bullet object. One faces left, one faces right (flipped 180 degrees on z axis)
If you can detect which way your sprite is facing, you can choose which point to fire out of. I assume you’re just flipping it’s scale on the X-axis, so you can set the script to change the firing point based on what the x-scale of the player sprite is.
yes you can use another transform var and call on it when player is facing the negative side of the local scale plane but i really do want to be efficient here and i can see bugs with that in the future.
It’s either that or stick a script on your FirePoint to move and rotate it to the correct place.
EDIT: Or, just have one FirePoint, with no bells attached, and when you turn your sprite around, use the negative X of your firepoint and whack 180 degrees on the rotation. Should have the same effect. Two seperate fireTransforms is working fine for me so far but I have to replace that code with something a little more elaborate later on anyway.
i only need the prefab to point to the direction of the gun. Im sure its an easy fix, is there any other tuts out there for it that i can cross reference?
Ah, idea. Instead of flipping localScale to turn the prefab around, turning it through 180 on the Y axis should have the same effect and in theory might just affect your FirePoint.
While using an object as a substitute for the mousePosition that is just like FirePoint game object (just positioned alittle to the right) i was able to make it shoot straight in front of the gun no matter which way its facing.
New variable (copy of firePoint just positioned to the right)
Transform endPoint;
Calling it in the this function (Script made by brackeys, modified to fit classic platformers)
My line won’t draw to the end point. It acts as if there is another fixed end point below my player (when i jump its above my player) even though there isn’t. here is my code: