Game Development Asked by jenkatan29 on January 5, 2022
I’m making a simple shooter game where the player shoots a projectile at objects that spawn in random locations. I’d like to change the speed of the spawned objects and am having two issues
Move Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveForward : MonoBehaviour
{
public float speed = 20f;
public GameObject Trump;
public GameObject Player;
private Rigidbody enemyRb;
// Start is called before the first frame update
void Start()
{
enemyRb = GetComponent<Rigidbody>();
Player = GameObject.Find("Player");
this.transform.LookAt(Player.transform);
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.forward * Time.deltaTime * speed);
}
}
Spawn Manager Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnManager : MonoBehaviour
{
public GameObject Trump;
private float spawnRange = 60;
private float startDelay = 2;
private float spawnInterval = 1.5f;
// Start is called before the first frame update
void Start()
{
InvokeRepeating("SpawnTrump", startDelay, spawnInterval);
}
// Update is called once per frame
void Update()
{
// MoveForward.speed += speedIncreasePerSpawn * spawnCount;
//spawnCount++;
}
void SpawnTrump()
{
Instantiate(Trump, GenerateSpawnPosition(), Trump.transform.rotation);
}
private Vector3 GenerateSpawnPosition()
{
float spawnPosX = Random.Range(-spawnRange, spawnRange);
float spawnPosZ = Random.Range(-spawnRange, spawnRange);
Vector3 randomPos = new Vector3(spawnPosX, 0, spawnPosZ);
return randomPos;
}
}
Did you mean to write something like this?
public MoveForward prefabToSpawn;
public float minSpeed = 5f;
public float maxSpeed = 20f;
void Spawn()
{
MoveForward spawned = Instantiate(
prefabToSpawn,
GenerateSpawnPosition(),
prefabToSpawn.transform.rotation);
spawned.speed = Random.Range(minSpeed, maxSpeed);
}
This is a fairly trivial application of methods you can find in the Unity documentation and in beginner guides, so you may want to work through some more tutorials to make sure you've got a good grasp of the basics.
Answered by DMGregory on January 5, 2022
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP