So I’m making a “top down” game, not fully top but more from the side.
I’m having 2 issues with my laser gun.
first issue
(my laser is a prefab) when it Instantiates in, it does not hold the “rotation” of the prefab. so its shooting sideways lol.
also, I can only shoot in 1 direction with “forward” only along the x axis. How can I shoot “outwards” from the spawn point? I’m looking for a different command but can find one in the Vector3 api.
Also I’m a newbie, but I have taken a few courses so I’m not that Fresh haha.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Laser : MonoBehaviour
{
[SerializeField] int speed;
[SerializeField] float distance;
// Update is called once per frame
void Update ()
{
transform.Translate(Vector3.forward * Time.deltaTime * speed);
distance += 1 * Time.deltaTime;
if (distance >= 2.5f)
Destroy(this.gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LaserRound : MonoBehaviour
{
public GameObject laserSpawnPoint;
public GameObject laserPrefab;
public float waitTime;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
// if q is pressed
//spawn laserprefab at player position
if (Input.GetKeyDown(KeyCode.Q))
{
Instantiate(laserPrefab.transform, laserSpawnPoint.transform.position, Quaternion.identity);
}
}
}
Ok so this is the new code I have, but now it seems even more messed up lol.
so the projectile is now spawning in the correct direction, just not spawning in the correct x,y. and still not doing the correct rotation?
here is the code for the player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LaserRound : MonoBehaviour
{
public Rigidbody projectile;
public GameObject laserSpawnPoint;
public float waitTime;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
// if q is pressed
//spawn laserprefab at player position
if (Input.GetKeyDown(KeyCode.Q))
{
Rigidbody clone;
Instantiate(projectile.transform, laserSpawnPoint.transform.position, Quaternion.identity);
clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection(Vector3.forward * 10);
}
}
here is the code that is on the projectile (laser)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Laser : MonoBehaviour
{
[SerializeField] int speed;
[SerializeField] float distance;
// This is the scrip that is on the projectile prefab
void Update ()
{
transform.Translate(Vector3.forward * Time.deltaTime * speed);
distance += 1 * Time.deltaTime;
if (distance >= 2.5f)
Destroy(this.gameObject);
}
}
and here are pictures so because I know typing out what I mean, doesn’t usually happen for me lol.
so there are the pictures of my lasers ( see how they are sideways but the prefab laser is at a 90* angle)
Setting the rotation at instantiation overrides any prefab value, and Quaternion.identity is 0,0,0 not “no value” or something like that. If you want to inherit the prefab’s rotation, you need to not explicitly set it in the clone.
You seem to also be mixing rigidbody physics and transform movement – you set the velocity when you create the projectile but then in the Laser script, you move the transform manually? Pick one or the other (usually the rigidbody velocity so you get accurate physics).
Finally, you have two Instantiate calls. Why’s that?
Adding more: If your laser is rotated the wrong way because your mesh points straight up but you want it to face forward, add the actual laser mesh as a child to an empty GameObject and rotate it there. Then you can orient the parent down the muzzle of the gun and the child (the laser mesh) will keep its rotation.
I have to say Grozzlers very good at explaining quite technical stuff and doesn’t waste time doing it.
Edit: Sorry, that was a different person, there are lots of helpful people on here and I get them confused that one was leftyrighty though I swear I’ve seen Grozzler do some similar posts before too.