So I have this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreateTrail : MonoBehaviour {
Vector3 lastLocation;
int traveled;
public GameObject obj;
Quaternion lastQuaternion;
bool on;
// Use this for initialization
void Start () {
lastLocation = transform.position;
lastQuaternion = transform.rotation;
}
// Update is called once per frame
void Update () {
if (on)
{
if (Vector3.Distance(lastLocation, transform.position) > .1)
{
Instantiate(obj, lastLocation, lastQuaternion);
lastLocation = transform.position;
lastQuaternion = transform.rotation;
}
}
if (Input.GetKeyDown(KeyCode.Space))
{
if (on)
{
on = false;
}
else
{
lastLocation = transform.position;
lastQuaternion = transform.rotation;
on = true;
}
}
}
}
It creates a trail of objects behind the object it is attached to, but often has the object with the script and the object created inside each other. Is there a way to delay or queue up the object instantiation to when the objects are not inside each other, without interrupting the rest of the script? Thanks!
You can try Coroutine, Invoke or InvokeRepeating to create a delay. (Avoid using Timer and Thread as much as you can.)
Then add a collider as trigger as a bounding box with its OnTriggerExit or OnTriggerExit2D methods implemented in order to find out when another collider is moved outside the bounding box.
However, there may be another issue:
Vector3.Distance returns a float, and when compared to a double (.1) one of them should be cast to the type of the other.
It is the float which will be implicitly converted to double and will have its accuracy changed.
You can avoid this by comparing two floats
if (Vector3.Distance(lastLocation, transform.position) > .1f)
You can find out more about implicit castings here
Related
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyControler : MonoBehaviour
{
private Transform player;
// Start is called before the first frame update
void Start()
{
//Note in this case we're saving the transform in a variable to work with later.
//We could save the player's game object (and just remove .transform from the below) but this is cleaner
var player = GameObject.FindWithTag("Player").transform;
}
// Update is called once per frame
void Update()
{
//**** The code below you need to modify and move into your own method and call that method from here.
//**** Don't overthink it. Look at how Start() is defined. Your own Move method would look the exact same.
//**** Experiment making it private void instead of public void. It should work the same.
//How much will we move by after 1 second? .5 meter
var step = .5f * Time.deltaTime; // 5 m/s
var newPosition = Vector3.MoveTowards(transform.position, player, step);//This is one way to move a game object by updating the transform. Watch the videos in this module and it should become apparent what goes here.
}
}
The transform does not map to the tagged player. I was trying to get an enemy in unity to move towards a moving player for a class, but I'm completely lost.
You had three issues in your code. The first was that you were defining a new player variable in the Start method. This hid the player member field you defined earlier.
The second issue was that you were getting a value to move towards, but you weren't assigning that value to the current objects position.
The third issue was that you were feeding in a Transform into the MoveTowards method, when you should have been feeding in a Vector2.
The code below should rectify these issues.
public class EnemyControler : MonoBehaviour
{
private Transform _player;
void Start()
{
_player = GameObject.FindWithTag("Player").transform;
}
void Update()
{
var step = .5f * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, _player.position, step);
}
}
Try doing something like this ive used this in the past to do something similar once you understand the code change it to what you desire
public int minRange;
private Transform target = null;
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
target = other.transform;
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
target = null;
}
void update()
{
transform.LookAt(target);
float distance = Vector3.Distance(transform.position,
target.position);
bool tooClose = distance < minRange;
Vector3 direction = tooClose ? Vector3.back : Vector3.forward;
if (direction == Vector3.forward)
{
transform.Translate(direction * 6 * Time.deltaTime);
}
else
{
transform.Translate(direction * 3 * Time.deltaTime);
}
}
im kind of newbie to unity and object oriented programming. Recently im trying to clone Cube Surfer mobile game. Basic idea from my view is this ;
-When we triggered to collactable cubes which consist script will be duplicated and it will be belong the main cube parent as child object then triggered cube will be destroyed.(After positioning)
-Later this duplicate child objects(cubes) will do the same when they enter trigger area of other collectable cubes(those will be the same prefab but did not yet create a prefab)
Im trying to collect(create a clone of it position and destroy the object) cubes. For first cube, I added some code to my movement script which is below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public GameObject addup;
Rigidbody rb;
float controlSpeed = 1.25f;
float forwardMovementSpeed = 10f;
private Vector3 axisGet;
float deathTime;
public int collected;
// Start is called before the first frame update
void Start()
{
rb = gameObject.GetComponent<Rigidbody>();
collected = 0;
}
// Update is called once per frame
void FixedUpdate()
{
axisGet = new Vector3(0, 0, Input.GetAxis("Horizontal"));
rb.MovePosition(transform.position + Vector3.left * forwardMovementSpeed * Time.deltaTime + axisGet * controlSpeed * Time.deltaTime);
}
private void OnTriggerEnter(Collider other)
{
if(other.tag=="add up")
{
gameObject.transform.position += Vector3.up;
var newObject = Instantiate(addup.gameObject, Vector3.zero, Quaternion.identity);
newObject.transform.parent = transform;
newObject.transform.position = gameObject.transform.position + Vector3.down;
Destroy(other.gameObject);
newObject.GetComponent<BoxCollider>().isTrigger = false;
collected++;
}
}
}
WORKED WITHOUT ERROR BUT THEN, I applied the same method to collectable cubes scripts.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UpNdown : MonoBehaviour
{
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "add up")
{
GameObject parentTransform;//?????????
parentTransform = gameObject.GetComponentInParent<GameObject>(); //get first cube component
parentTransform.transform.position += Vector3.up; //first cube one unit up
GameObject newObject; // ?????????
newObject = Instantiate(other.gameObject, Vector3.zero, Quaternion.identity) as GameObject; //???????????
Debug.Log(newObject);
var collect = parentTransform.GetComponent<Movement>().collected;
if (other != null)
{
Destroy(other.gameObject); //destroy triggered collactable
}
newObject.transform.parent = parentTransform.transform; //setting parent to new cube
newObject.transform.position = parentTransform.transform.position + Vector3.down * (collect + 1); //setting position of new cube
newObject.GetComponent<BoxCollider>().isTrigger = false; //prepare the below cubes(new cubes) for trigger with other collactable cubes
collect++;
}
}
}
And, I had nullexception error in every line in ontriggerenter method then, I changed(added) the lines with question marks. So, I get
ArgumentException: GetComponent requires that the requested component 'GameObject' derives from MonoBehaviour or Component or is an interface.
UnityEngine.GameObject.GetComponentInParent[T] (System.Boolean includeInactive) (at :0)
UnityEngine.GameObject.GetComponentInParent[T] () (at :0)
UpNdown.OnTriggerEnter (UnityEngine.Collider other)
I thought, I understood the OOP instance idea which objects in the scenes are instances scripts has their own value... but i dont understand that while I was operating on a instance why it is null in the memory :((((((((((((( if PC can't access how instantiates the object ?
SORRY I WRITE THIS LONG BUT IM ABOUT THE EDGE AGAIN I DON'T WANT TO QUIT BECAUSE OF FACING THIS PROBLEM AGAIN
TY FOR YOUR ANSWERS, ALREADY APPRECIATED :)
GameObject is no component (it is rather a container of all components attached to it!)
=> you can't get it using GetComponent or GetComponentInParent at all. (Btw Note that GetComponentInParent starts the search on this object itself first before bubling up the hierarchy so either way this isn't what you want to use).
What you want is simply transform.parent to get the Transform component of the parent object of the object this script is attached to (assuming the rest of your code does what it should)
private void OnTriggerEnter(Collider other)
{
// Rather use CompareTag instead of string ==
// The latter silently fails in case of typos making your debugging life miserabel
// it is also slightly less efficient
if (!other.CompareTag("add up")) return;
// Get the parent of this object
var parentTransform = transform.parent;
// Cache this you will need it later see below
var parentMovement = parentTransform.GetComponent<Movement>();
var collect = parentMovement.collected;
parentTransform.position += Vector3.up;
// By using the correct type you want later you can skip GetComponent
var newObjectCollider = Instantiate(other, Vector3.zero, Quaternion.identity);
Debug.Log(newObjectCollider);
Destroy(other.gameObject);
newObjectCollider.transform.parent = parentTransform;
newObjectCollider.transform.position = parentTransform.position + Vector3.down * (collect + 1);
newObjectCollider.isTrigger = false;
// This does absolutely nothing. Numeric values are passed by value and there is no connection
// between your local variable and the component you got it from
//collect++;
// you probably rather want to increase
parentMovement.collected++;
}
Or alternatively since you anyway have a specific component on your parent object you could also do
// Instead directly get this component
var parentMovement = GetComponentInParent<Movement>();
// Then wherever needed access the transform through it
var parentTransform = parentMovement.transform;
...
I'm quite sure though that the other way round it is more efficient since you already know exactly which parent you are searching the component on.
Or - and this would probably be the best option - cache that parent information once right away:
// If possible already reference this vis the Inspector
[SerializeField] private Movement parentMovement;
private Transform parentTransform;
private void Awake ()
{
if(! parentMovement) parentMovement = GetComponentInParent<Movement>();
parentTransform = parentMovement.transform;
}
Ty sir my first code was nearly the same of your first answer but didn't work again at least for error.
private Transform parentTransform;
private void Awake ()
{
if(! parentMovement) parentMovement = GetComponentInParent<Movement>();
parentTransform = parentMovement.transform;
}
But this worked, I guess the problem I need to define instances to class so they don't disappear instantly on the trigger function or direct I need to define them to class.
Anyway, thank you derHugo now need to solve different problems :D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveNavi : MonoBehaviour
{
public float speed;
private GameObject rig_f_middle;
// Start is called before the first frame update
void Start()
{
rig_f_middle = GameObject.Find("rig_f_middle.02.R");
}
// Update is called once per frame
void Update()
{
if(IKControl.startMovingNAVI == true)
{
Vector3 moveDir = (rig_f_middle.transform.position - transform.position).normalized;
transform.position += moveDir * speed * Time.deltaTime;
}
}
}
The problems with this is that the transform that move never stop moving even when it's very close to the target and I want it to stop moving.
I also want to make that the transform will get as much close as possible to the target now it's very close to it but not on it.
And last I want to make that when the object reached the target and stopped moving make the object child of the target.
You can introduce a check within the update to measure how far away your are from the destination transform, like this:
void Update ( )
{
if ( IKControl.startMovingNAVI == true )
{
var v = rig_f_middle.transform.position - transform.position;
if ( v.magnitude < yourToleranceValue )
{
this.transform.parent = rig_f_middle.transform;
this.enabled = false;
return;
}
Vector3 moveDir = v.normalized;
transform.position += moveDir * speed * Time.deltaTime;
}
}
Now, I'd personally go the extra step of only checking against sqrMagnitude, not magnitude as the computation is lighter, especially in an update loop, but maybe that's an optimisation you don't need or care about. Either way, you'd just set your yourToleranceValue appropriately, as a public, serialisable field, or your chosing. Then when the two transforms are close enough, it parents this transform, and also turns off this component so no more checks are performed.
I'm making a spawn system for my game, that spawns enemies at random positions, but Unity is telling me that I'm not checking if the object is already destroyed. I've tried to solve it with some other topics here but I couldn't do it.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawn : MonoBehaviour {
public GameObject Enemy2Spawn;
public float maxWidth;
public float minWidth;
public float rateSpwan;
private float currentRSpawn = 2.0f;
public int maxInimigo;
public int Inimigos = 0;
void Start()
{
transform.position = new Vector3(0, 6.03f, 0);
}
// Update is called once per frame
void Update()
{
if (currentRSpawn > Time.time & Inimigos<=maxInimigo)
{
transform.position = new Vector3(Random.Range(minWidth, maxWidth), transform.position.y, transform.position.z);
Instantiate(Enemy2Spawn);
currentRSpawn = currentRSpawn + rateSpwan;
Inimigos++;
}
if (Enemy2Spawn. == null)
{
Destroy(this.gameObject);
}
}
}
The error I'm getting is:
"The object of type 'GameObject' has been already destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object"
In the Update function, you are checking if the GameObject is null, which means that it does not exist, then you are using Destroy() to destroy that object that does not exist. Instead, you will want to check if the object exists in the if statement that spawns the enemies. Add that to the if statement, like this, and you should be good.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawn : MonoBehaviour {
public GameObject Enemy2Spawn;
public float maxWidth;
public float minWidth;
public float rateSpwan;
private float currentRSpawn = 2.0f;
public int maxInimigo;
public int Inimigos = 0;
void Start()
{
transform.position = new Vector3(0, 6.03f, 0);
}
// Update is called once per frame
void Update()
{
if (currentRSpawn > Time.time && Inimigos <= maxInimigo && Enemy2Spawn == null)
{
transform.position = new Vector3(Random.Range(minWidth, maxWidth), transform.position.y, transform.position.z);
Instantiate(Enemy2Spawn);
currentRSpawn = currentRSpawn + rateSpwan;
Inimigos++;
}
}
}
Well there are a couple things that seem off on this script:
before that: For the error if you set Enemy2Spawn to an object in the scene and it gets destroyed then the value is null then when it tries to Instantiate again, which causes the error (could put the condition "Enemy2Spawn != null" in the first if statement as a fix)
"transform.position = new Vector3(Random.Range(minWidth, maxWidth), transform.position.y, transform.position.z);"
This line changes the object attached to the script, it's position to be exact, which won't affect the position of the enemy you are going to spawn.
"Instantiate(Enemy2Spawn);" makes a clone of the variable's value. The clone inherits all values of the original, so position will be what the original's position is.
Unless you didn't set the value of "Enemy2Spawn" it would not become null, unless a different script changes the value and that it is a prefab. So, "if (Enemy2Spawn == null)" assuming the value is set, does not change else where and is set as a prefab in this case then the statement is never true. If the statement ever became true the line inside "Destroy(this.gameObject);" destroys the object that the script is attached to so it seems counter productive to me (to destroy the spawner) but if it's a measure to prevent error it should be in the Start or Awake function or if setting it with a different script just destroy the script instead of setting the variable to null (which i really doubt though).
Here is a changed script that should fix the issues i stated and fit your needs
using System.Collections;
using System.Collection.Generic;
using UnityEngine;
public class EnemySpawn : MonoBehaviour {
public GameObject Enemy2Spawn;
public float maxWidth;
public float minWidth;
public float rateSpwan;
private float currentRSpawn = 2.0f;
public int maxInimigo;
public int Inimigos = 0;
void Start()
{
transform.position = new Vector3(0, 6.03f, 0);
}
// Update is called once per frame
void Update()
{
if (currentRSpawn > Time.time & Inimigos<=maxInimigo)
{
Instantiate(Enemy2Spawn, new Vector3(Random.Range(minWidth, maxWidth), transform.position.y, transform.position.z), Quaternion.identity);
currentRSpawn = currentRSpawn + rateSpwan;
Inimigos++;
}
}
}
Different reason for the error, the given script may spit out the error but i don't see how it'd cause it so it would have to be caused by a different script that causes the object with the script attached to be destroyed just before executing the update function.
Also, the scripting API suggests calling Destroy like the following if using JS/unityscript
UnityEngine.Object.Destroy(this.gameObject);
I hope this helps, if not then more information is needed
I am trying to create a ball hitting game in the baseball format. I create a ball as a prefab. I want to push the ball to the main scene within a certain period of time.
For example; when the first ball is in the scene, the second ball will spawn after 5-6 seconds, then the third, fourth etc. I am the beginner level of Unity and I am not good at C#. I am not sure whether I am using the true functions such as Instantiate. Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour {
public float RotateSpeed = 45; //The ball rotates around its own axis
public float BallSpeed = 0.2f;
public GameObject[] prefab;
public Rigidbody2D rb2D;
void Start() {
rb2D = GetComponent<Rigidbody2D>(); //Get component attached to gameobject
Spawn ();
}
void FixedUpdate() {
rb2D.MoveRotation(rb2D.rotation + RotateSpeed * Time.fixedDeltaTime); //The ball rotates around its own axis
rb2D.AddForce(Vector2.left * BallSpeed);
InvokeRepeating("Spawn", 2.0f, 2.0f);
}
public void Spawn ()
{
int prefab_num = Random.Range(0,3);
Instantiate(prefab[prefab_num]);
}
}
After I apply this script, the result is not what I want.
Add InvokeRepeating("Spawn", 2.0f, 2.0f); to the Start not the FixedUpdate.
InvokeRepeating invokes the method methodName in time seconds, then repeatedly every repeatRate seconds.
You can check the documentation here.
Use Coroutines
private IEnumerator SpawnBall() {
while(true) {
Instantiate(baseball);
yield return new WaitForSeconds(5);
}
}
Which can then be started with StartCoroutine() and terminated in one of three ways:
internally by breaking out of the while loop with break (the function would then have no more lines to execute and exit)
internally by yield break
externally by calling StopCoroutine() on a reference to the coroutine
Alternative to the other answers: Just use a countdown. This sometimes gives you more control
// Set your offset here (in seconds)
float timeoutDuration = 2;
float timeout = 2;
void Update()
{
if(timeout > 0)
{
// Reduces the timeout by the time passed since the last frame
timeout -= Time.deltaTime;
// return to not execute any code after that
return;
}
// this is reached when timeout gets <= 0
// Spawn object once
Spawn();
// Reset timer
timeout = timeoutDuration;
}
I updated my script by considering your feedbacks and it works like a charm. Thanks to all!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Threading;
public class Ball : MonoBehaviour {
public float RotateSpeed = 45; //The ball rotates around its own axis
public float BallSpeed = 0.2f;
public GameObject BaseBall;
public Transform BallLocation;
public Rigidbody2D Ball2D;
void Start() {
Ball2D = GetComponent<Rigidbody2D>(); //Get component attached to gameobject
InvokeRepeating("Spawn", 5.0f, 150f);
}
void FixedUpdate() {
Ball2D.MoveRotation(Ball2D.rotation + RotateSpeed * Time.fixedDeltaTime); //The ball rotates around its own axis
Ball2D.AddForce(Vector2.left * BallSpeed);
}
public void Spawn ()
{
Instantiate (BaseBall, BallLocation.position, BallLocation.rotation);
}
}