I'm making a small endless runner game about a fish swimming to school, and I want to include a particle system that replicates bubbles and indicates when it is speeding up. In order to make the fish swim through obstacles, he continually moves forward. However, when I try to move the particle generator forward through script, it doesn't work. Can someone tell me why?
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class motion : MonoBehaviour {
public float motionSpeed = 1f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(motionSpeed / 10, 0, 0);
motionSpeed += motionSpeed / 7200;
}
}
I recommend attaching your particle system onto a separate gameobject which is a child of your fish that is swimming.
Related
So I'm using this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class wands : MonoBehaviour
{
public Transform WandPos;
void Update()
{
WandPos.rotation = Quaternion.Lerp(new Quaternion(0,0,1,0), new Quaternion(0,0,0.866025448f,0.49999994f),Time.time * .5f);
}
}
For some reason it is preforming the rotation before the GameObject even rotates, so when it instantiates, the GameObject snaps to where it would be if it instantiated when the scene started. I'm probably just missing something obvious, but I can't seem to find my solution anywhere online either.
Also, if there is an easier way to do this than with the code I've written, please tell me.
Well Time.time is the time in seconds since you started the application!
Sounds like you rather wanted to track the time since you instantiated the object so e.g. like
private float time;
void Update ()
{
time += Time.deltaTime * 0.5f;
WandPos.rotation = Quaternion.Lerp(new Quaternion(0,0,1,0), new Quaternion(0,0,0.866025448f,0.49999994f), time);
}
I am in process to make a 3D fps game with unity. But when i wrote the code to destroy the bullet after a particular time say - 5 seconds, till 5 seconds it spawnes the bullet and after 5 seconds the bullets spawned bullets get destroyed. But after that when i try to again spawn bullets, they dont get spawned and it shows the error GameObject has been destroyed but you are still trying to access it.
here is the destroy bullets code
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float speed = 8f;
public Camera playerCamera;
public float lifeDuration = 2f;
private float lifeTimer;
void Start()
{
lifeTimer = lifeDuration;
}
// Update is called once per frame
void Update()
{
transform.position += playerCamera.transform.forward * speed * Time.deltaTime;
lifeTimer -= Time.deltaTime;
if (lifeTimer <= 0f)
{
Destroy (gameObject);
}
}
}
I believe that when you destroy the GO, the reference to that object may be lost too. In your case, in which you're using bullets, I recommend you to instead of using Destroy(gameObject), use gameObject.SetActive(false) and recycle the bullets with a pool of bullets that you instantiate at the start of the game. That way is easier to have a reference to the GO and optimize the cost of instantiating x bullets.
This error occurs when you try to access a gameobject which has been destroyed.
how do you spawn the bullet?
if it's a prefab,i suggest you do like this,and DO NOT call this gameobject in any other script after its "lifeTime":
var go = GameObject.Instantiate( Resources.Load( "bullet prefab path" ) ) as GameObject;
go.AddComponent<Bullet>();
if not, i think you're Instantiate a gameobject already exist in Hierarchy.
//if the "prefab" is already exist in Hierarchy with script "Bullet" and is active
//this "prefab" will be destroyed by your bullet script after "lifeTime"
//when the next time you Instantiate() with this "prefab"
//you'll get the error:"GameObject has been destroyed but you are still trying to access it"
GameObject go = GameObject.Instantiate(prefab) as GameObject;
I'm trying to get a camera to move whenever you press either WASD or the arrow keys, but it's throwing
Error CS0428 Cannot convert method group 'GetComponent' to non-delegate type 'Transform'. Did you intend to invoke the method?
On this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameramover : MonoBehaviour
{
public Camera controlled;
Vector3 movement;
void Start()
{
Transform transform1 = controlled.GetComponent<Transform>;
}
void Update()
{
movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);
}
}
I tried invoking the method, but that threw CS0201, so I tried doing
new Transform(controlled.GetComponent<Transform>;)
and that didn't work either, so I went here to ask for help.
Figured it out with the help of the wonderful people at the Game Dev Network discord server! i just had to add a pair of parentheses on the end of the controlled.GetComponen;
I followed this tutorial and done everything that was mentioned, everything looked and worked fine except the reticle point expands and contracts when I gaze upon the cube but the click event does not trigger. Any help would be greatly appreciated.
Code for random teleport is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ReticleTest : MonoBehaviour
{
public void RandomlyTeleport()
{
Debug.Log("reached here");
gameObject.transform.position = new Vector3(
GetRandomCoordinate(), Random.Range(0.5f, 2), GetRandomCoordinate());
}
private float GetRandomCoordinate()
{
var coordinate = Random.Range(-7,7);
while( coordinate > -1.5 && coordinate < 1.5)
{
coordinate = Random.Range(-5,5);
}
return coordinate;
}
}
These are screenshots
Update :
It turns out that the same thing is happening when I bring up the scene that comes premade with SDK HelloVR, although the hexagon(the only interactive thing in the scene) changes color when I gaze at it, nothing else happens when I click it. So, this is not a problem with what I made but the inherent problem of unity or the SDK
I'm trying to make the system work so that when a game starts and for each wall, it will either be in the game or not, randomly. Somehow, when I run it, it either keeps or destroys all the walls. How do I fix this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallGen : MonoBehaviour{
public GameObject wallObject;
System.Random rnd = new System.Random();
// Use this for initialization
void Start () {
generate();
}
void generate()
{
int number = rnd.Next(1, 5);
if (number == 2)
{
OnDestroy();
}
}
private void OnDestroy(){
Destroy(wallObject);
}
// Update is called once per frame
void Update () {
}
}
Every time you do new System.Random() it is initialised using the clock. Now as the Start function is called at the same time, you get the same value for all the gameobjects. You should keep a single Random instance for all gameobjects and keep using Next on the same instance.
Or the easy solution
just Use Random.Range (1,5) instead of rnd.Next(1, 5);
As a side note, don't use the function name OnDestroy, as it is one of the MonoBehaviour functions in Unity and is called automatically when the attached gameobject is destroyed.