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;
}
}