- Home /
Why don't my laser shots shoot from the same position consistently?
I have a little robot guy that jumps up and down and he shoots lasers out of his eyeballs when a button is clicked. I used the unity project 'Space Shooter' to help aid me in designing the code. The problem is that there seems to be a delay sometimes and it doesn't appear that the lasers are coming out of his eyeballs but a little lower or higher depending on what position he is in his jump. I nested the shotSpawn gameObject underneath my Player gameObject and positioned the shotSpawn right on top of the eyeball, so what is the problem?
Here is my RobotController script:
using UnityEngine;
using System.Collections;
public class RobotController : MonoBehaviour
{
public float height;
public float verticalSpeed;
public float horizontalSpeed;
public GameObject shot;
public GameObject shotSpawn;
public float fireRate;
private float nextFire;
Animator animator;
void Start ()
{
animator = GetComponent<Animator>();
}
void Update()
{
if (Input.GetMouseButtonDown (0) && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate (shot, shotSpawn.transform.position, shotSpawn.transform.rotation);
audio.Play ();
}
}
void FixedUpdate ()
{
Vector3 pos = transform.position;
pos.z = -2;
transform.position = pos;
Vector3 direction = Vector3.zero;
direction.x = -Input.acceleration.x*horizontalSpeed;
if ( direction.sqrMagnitude > 1 ){
direction.Normalize();
}
direction *= Time.deltaTime;
transform.Translate( direction * verticalSpeed );
// Smoothes the rotation when touching Box collider... not sure why
rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
}
void OnCollisionEnter(Collision collision){
foreach(ContactPoint contact in collision.contacts){
rigidbody.velocity = transform.up * height;
audio.Play();
animator.SetTrigger("Jump");
}
}
}
Here is my Mover script:
public class mover : MonoBehaviour
{
public float speed;
void Start()
{
rigidbody.velocity = (transform.up + transform.right) * speed;
}
}
Answer by mquickel · May 03, 2014 at 03:03 AM
Where are you setting fireRate? As a test you can take that check out and just instantiate whenever the mouse button is clicked to narrow issues down.
I eli$$anonymous$$ated the fireRate and still no change.
As the robot moves down in his jump state the laser spawns above his eyes around and as the robot moves up in his jump state the laser spawns closer to his feet
Answer by Fattie · May 03, 2014 at 06:09 AM
you very likely HAVE THE PARENTING WRONG.
the "laser shooting position" will need to be a child of THE HAND - or something like that.
very simply, look using your eyeballs at the editor as it is playing. click on the "laser shooting position" game object, and watch if it goes up and down with the "hand" or whatever is relevant.
if not, move one object inside the other (ie -- change the parenting) until you have it working.
Right now my laser shooting position is the eyes. In the scene view I can watch the shooting position go up and down as the robot jumps, and yet the laser is not spawning from this position, consistently. I've tried parenting the laser to different parts of the robot body, but always run into the same problem. I'm done for the night. $$anonymous$$y brain is fried. I will try again tomorrow.
Your answer
![](https://koobas.hobune.stream/wayback/20220613144127im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
control animation using animation control class 1 Answer
How to destory gameObject without destroying the audio source attached to it? 2 Answers
Weird NullReferenceException 1 Answer