im creating simple script which tells gameobject that it needs to follow other gameobject in defined distance.
code:
void Update()
{
objPos = GameObject.FindGameObjectWithTag("Player").transform.position;
transform.position = new Vector3(objPos.x + 0.5f, objPos.y + 0.5f);
}
problem is: following gameobject is flickering. When you watch it, it appears to be few frames behind followed gameobject. I noticed it before in my earlier prototype but now its crutial for my new game. So i think its pretty common issue.
Is there sollution for this unwanted behaviour?
Thanks
GameObject.FindGameObjectWithTag("Player") has high performance cost, instead of using it each frame, hold an instance of the player. If the player is constant in the scene,
public Transform playerTransform;
void Update()
{
transform.position = playerTransform.position + new Vector3(0.5f, 0.5f);
}
and then assign the Player transform in Unity editor to this object using inspector. (Or dynamically in the code)
If your Player object gets destroyed and a new one instantiates once in a while, you can either assign the new one to the follower objects, or use a simple accessor method like this:
Transform _playerTransform;
Transform playerTransform
{
get
{
if(_playerTransform == null)
{
_playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
return _playerTransform;
}
}
void Update()
{
transform.position = playerTransform.position + new Vector3(0.5f, 0.5f);
}
Related
So I'm trying to make a little pushback effect in my tests arena, I've got a sphere collider and here is my script:
// PushBack Class Script
if (Input.GetKeyDown(KeyCode.Q))
{
explosion_ball.transform.position = transform.position;
StartCoroutine(WaitAndPrint());
}
IEnumerator WaitAndPrint()
{
float i = 0;
while (i < 1)
{
i += 0.01f;
explosion_ball.radius = curve.Evaluate(i) * 10;
yield return new WaitForSeconds(0.01f);
}
}
//__________//
Sphere collider is set and stuff, but it doesn't push things back like I thought it would.
Thanks!
Edit:
explosion_ball is a sphere collider, I'm changing it with the point on the animation curve and * it by 10
EDIT:Unity Rigid bodies go to sleep so[Also Change interpolation to continuous if collider changes size too quickly]
A.check if obstacle rigid bodies are Sleeping with onTriggerEnter and Wake Up
void OnTriggerEnter(collider){ if(rb.IsSleeping()){rb.WakeUp();}
or
B.Attach this forceWakeUp Script to all Objects you want to be obstacles.
using UnityEngine;
public class forceWakeUp : MonoBehaviour
{
private Rigidbody rb;
void Start()
{
rb = gameObject.GetComponent<Rigidbody>();
}
void Update()
{
if(rb.IsSleeping()){rb.WakeUp();}
}
}
Forcibly Keeping Many objects awake Will impact performance.So ,your decision.
You need to scale up the collider on the object by radius and all the objects it is supposed to wobble need to have Rigid body component attached to them
2.if you ARE doing above things and its not adding any force you could just add a force on all the overlapping objects using the "OnCollisionEnterTrigger" and AddForceMethod radially away from the Sphere
Obstacle.position - Sphere.position is the vector Radially away from Sphere I think.
You don't need coroutines for this I think.
While searching through the Unity scripting API, found a method in there called
Rigidbody.AddExplosionForce(explosionForce, explosionPosition, explosionRadius, upwardsModifier, mode)
In order to get the rigid bodies that are to be affected by the explosion I would need to get them using Physics.OverlapSphere(position, radius) this gets all of the objects within the radius variable, then get the component.
Combining these two would also look like:
Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
foreach (Collider hit in colliders)
{
if (hit.transform.tag == "Interactable")
{
if (hit.gameObject != hitgameObject)
{
Rigidbody rb = hit.GetComponent<Rigidbody>();
if (rb != null)
rb.AddExplosionForce(power, transform.position, radius, upForce, ForceMode.Impulse);
}
}
}
If there is any explaining that you would like me to do about my variables mentioned I will reply :)
Thanks for the help guys.
I don't know your curve and what values that evaluation produces, but you can check your code visually with Window/Analysis/Physics Debugger, or write a gizmo:
private void OnDrawGizmos()
{
Gizmos.color = new(1.0f, 0.0f, 0.0f, 0.5f);
Gizmos.DrawSphere(explosion_ball.transform.position, explosion_ball.radius);
}
Or simply just use https://docs.unity3d.com/ScriptReference/Rigidbody.AddExplosionForce.html
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
I have a problem with camera in Unity. I want to follow to the ball. I want to make auto respawn for the ball and camera. But the code works but camera doesn't follow to the ball.
This is code for auto respawn:
public class start : MonoBehaviour
{
public GameObject Camera;
public GameObject Ball;
void Start()
{
GameObject ball = GameObject.Instantiate(Ball);
Ball.name = "Ball";
ball.transform.position = transform.position + Vector3.up * 0.5f;
Instantiate(Camera);
Camera.name = "Camera";
Camera.transform.position = transform.position + new Vector3(0.25f, 1.75f, 6.5f);
}
The code for camera controller:
public Transform Ball;
void Update()
{
Rigidbody rigidbody = Ball.GetComponent<Rigidbody>();
float ballVelocity = rigidbody.velocity.sqrMagnitude;
UnityEngine.Vector3 vector = new UnityEngine.Vector3(0, 4, 7);
vector *= (1+ ballVelocity / 30);
UnityEngine.Vector3 nowaPozycjaKamery = Ball.position + vector;
transform.position = UnityEngine.Vector3.Lerp(transform.position, nowaPozycjaKamery, Time.deltaTime*5);
transform.LookAt(Ball);
}
Camera worked well before auto respawn. Code work that on start camera follow to good position, but is freezed when the ball change the position.
I wish you provided more information on this question. Like is there any NullReferenceException or other exceptions. But I'll try to answer your issue anyways. I think the problem is that the camera script doesn't have the reference to the new ball that's being spawned (this is in the case of destroying the old ball and Instantiating a new one with Destroy(gameObject)).
What I suggest is to add a Method in the camera script that will be in charge of injecting the new ball into the script. And then you call this method with the new spawned ball in the script responsible for spawning. Or a more straightforward but not efficient way is to check if the ball property is null and then you make a call to GameObject.Find("Name of The ball object") and you give the name of the ball object.
I have a script that moves instantiated game objects depending on their tag. However, the game objects that have been instantiated are not moving.
Instantiation Script:
void Update()
{
tillNextSpawn += Time.deltaTime;
Debug.Log(tillNextSpawn);
if (tillNextSpawn >= 2)
{
UnityEngine.Debug.Log("Instantiating circle");
screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), Camera.main.farClipPlane / 2));
Instantiate(circle, screenPosition, Quaternion.identity);
tillNextSpawn = 0.0f;
}
}
Enemy controller script(moves the enemies)
void FixedUpdate()
{
/*GameObject[]*/
var objects = GameObject.FindGameObjectsWithTag("Enemy");
var objectCount = objects.Length;
foreach (var obj in objects)
{
// Move the players accordingly
var rb = obj.GetComponent<Rigidbody2D>();
Debug.Log(rb);
Vector2 direction = (player.position - obj.transform.position).normalized;
rb.velocity = direction * moveSpeed;
}
}
You should be able to that, and the issue maybe somewhere else in your project.
I created a sample project that recreates what you're trying to do, trying to be as similar as I could be to the sample code you send, you can find it here:
https://github.com/webocs/unity-so-sample-tags
For what I can see, your console is sending an exception
get_main is not allowed to be called...
What comes to my mind, is that that exception is breaking the entire execution, and that's why nothing is happening.
As a side note, I don't know your project so I don't really know why you're building it this way. Said that, why aren't you creating an Enemy script that's attached to the enemy prefab? If you have many enemies you're going to be finding and iterating through all of them in each update tic. If you create an Enemy script and attach it to the prefab you should be able to handle the movement of the enemy using the transform of the gameObject that the script is attached to. This way each enemy is a standalone entity.
I hope all of this helps!
Edit:
I've edited the repo and added a scene called "IndividualEnemies" that illustrates what I told you in the comments
If you want the enemy to follow the player, try doing the following:
//Attach this script to the enemy
Transform player;
private Rigidbody2D rb;
private Vector2 movement;
public float moveSpeed;
void Awake()
{
player = ScriptNameOnPlayer.instance.gameObject.transform;
}
// Start is called before the first frame update
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
}
void Update()
{
Vector3 direction = player.position - transform.position;
direction.Normalize();
movement = direction;
}
void FixedUpdate()
{
moveCharacter(movement);
}
void moveCharacter(Vector2 direction)
{
rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
}
//But make sure your player script has this line of code:
public static ScriptNameOnPlayer instance;
Hope this helps !
My game is a 2D RTS, and I was wondering if anyone knew of a good tutorial for Unity, or if someone well-versed in the syntax of it could tell me what I could have done wrong.
So, I have my camera object, and my player object, both tagged. The player object just has a sprite on it, and is set to rigidbody. The script goes as follows:
using UnityEngine;
using System.Collections;
public class AIsciript : MonoBehaviour
{
private bool thisIsPlayer = true;
private GameObject objPlayer;
private GameObject objCamera;
//input variables (variables used to process and handle input)
private Vector3 inputRotation;
private Vector3 inputMovement;
//identity variables (variables specific to the game object)
public float moveSpeed = 100f;
// calculation variables (variables used for calculation)
private Vector3 tempVector;
private Vector3 tempVector2;
// Use this for initialization
void Start()
{
objPlayer = (GameObject)GameObject.FindWithTag("Player");
objCamera = (GameObject)GameObject.FindWithTag("MainCamera");
if (gameObject.tag == "Player")
{
thisIsPlayer = true;
}
}
// Update is called once per frame
void Update()
{
FindInput();
ProcessMovement();
if (thisIsPlayer == true)
{
HandleCamera();
}
}
void FindInput()
{
if (thisIsPlayer == true)
{
FindPlayerInput();
}
else
{
FindAIInput();
}
}
void FindPlayerInput()
{
//find vector to move
inputMovement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
//find vector to the mouse
tempVector2 = new Vector3(Screen.width * 0.5f, 0, Screen.height * 0.5f);
// the position of the middle of the screen
tempVector = Input.mousePosition;
// find the position of the mouse on screen
tempVector.z = tempVector.y;
tempVector.y = 0;
Debug.Log(tempVector);
inputRotation = tempVector - tempVector2;
}
void FindAIInput()
{
}
void ProcessMovement()
{
rigidbody.AddForce(inputMovement.normalized * moveSpeed * Time.deltaTime);
objPlayer.transform.rotation = Quaternion.LookRotation(inputRotation);
objPlayer.transform.eulerAngles = new Vector3(0, transform.eulerAngles.y + 180, 0);
objPlayer.transform.position = new Vector3(transform.position.x, 0, transform.position.z);
}
void HandleCamera()
{
objCamera.transform.position = new Vector3(transform.position.x, 15, transform.position.z);
objCamera.transform.eulerAngles = new Vector3(90, 0, 0);
}
}
I just figured I would post the code just in case, but I figure it's probably not the issue, as I tried to force it to move in Start() and nothing happened.
You shouldn't be using all those checks for thisIsPlayer. You should have separate classes for the player entity and non-player entities.
Public variables are exposed in the editor and get serialized with the entity when the level is saved. This could mean that moveSpeed is not currently set to what it is initialized to in this class.
You shouldn't add force to a rigidbody in the Update method. There's a FixedUpdate method that's used for applying physics. This is because Update is called once per frame, no matter what the framerate, and FixedUpdate is only called at specific intervals, so physics forces aren't affected by framerate.
Also, you shouldn't try to apply a force and set the position of the transform of the same object. Strange things will happen.
If you go into the Unity Asset Store (available in the Window menu within Unity) there is a section called "Complete Projects" that contains some free tutorials. I can't remember which of them is written in C#, but even the JavaScript ones will give you some ideas on how to structure the project.
I don't know if I understood it right: your problem is that it is not moved by the AI?
if this is so, one problem could be that you initialize your
private bool thisIsPlayer = true;
with true but I can not see any condition setting it to false (enter ai mode)
just my 2 cents :)