Unity 3D Collisiondetection - c#

I generate an Gameobjct array of speheres and try to apply a collider to them to detect collision with my character controller.
i tried serverel methods, nothing worked. Why Unity doent detect the collision ?
Script for generating the array :
public GameObject[] Chunkzufall(float l, float b, int n)
{
GameObject chunk = new GameObject();
GameObject[] chunks = new GameObject[n];
chunkSpeed = new float[n];
chunkMaxH = new float[n];
chunkMinH = new float[n];
for (int j = 0; j < n; j++)
{
float h = 1;
float posX = Random.Range(0.0f, b);
float posZ = Random.Range(0.0f, l);
GameObject group = Chunk(h);
group.transform.Translate(posX, 0.0f, posZ);
group.transform.parent = chunk.transform;
chunk.tag = "reset";
chunk.name = "chunk";
chunkSpeed[j] = (float) Random.Range(-0.04f, 0.04f);
chunkMaxH[j] = (float) Random.Range(2.0f, 10.0f);
chunkMinH[j] = (float) Random.Range(-2.0f, -10.0f);
chunk.AddComponent<SphereCollider>();
chunk.GetComponent<SphereCollider>().isTrigger = true;
chunk.GetComponent<SphereCollider>().radius = 5.0f;
chunks[j] = chunk;
}
return chunks;
}
public void MoveChunks(GameObject[] chunks)
{
int i = 0;
foreach(GameObject chunk in chunks)
{
Vector3 position = chunk.transform.GetChild(i).position;
if (position.y >= chunkMaxH[i] || position.y <= chunkMinH[i])
{
chunkSpeed[i] = chunkSpeed[i] * -1;
}
position.y = position.y - chunkSpeed[i];
chunk.transform.GetChild(i).position = position;
chunk.GetComponent<SphereCollider>().center = position;
i++;
}
i = 0;
}
collision trigger function :
private void OnTriggerEnter(Collider col) {
if(col.gameObject.tag == "reset") {
transform.position = new Vector3(startX, startY, startZ);
Debug.Log("chunk");
}
}

To detect a trigger on the dynaimcally created object, you have to enable the IsTrigger flag, add collider to the object. The object must also have Rigidbody attached to it. It looks like you already have the IsTrigger flag and a collider but you are missing Rigidbody.
Replace this:
chunk.AddComponent<SphereCollider>();
chunk.GetComponent<SphereCollider>().isTrigger = true;
chunk.GetComponent<SphereCollider>().radius = 5.0f;
with
chunk.AddComponent<SphereCollider>();
chunk.AddComponent<Rigidbody>(); //ADDS RIGIDBODY COMPONENT
chunk.GetComponent<SphereCollider>().isTrigger = true;
chunk.GetComponent<SphereCollider>().radius = 5.0f;

Do you have Rigidbody attached? The objects need Rigidbody attached to it to be able to detect collision. And do note that for each pair of collider and collided objects, having one of them with Rigidbody attached is enough to capture the event. In your case, if you don't want to process the collision betweens spheres, then attach Rigidbody to your character object is good enough.

You could add a RigidBody to all of the spheres but that would be very inefficient if you want to have many of them. It is enough to have 1 RigidBody for every 2 objects that you want to check collision against.
So in your case it would be enough to put a RigidBody component on your character controller object.
Also, make sure the RigidBody is not set to Kinematic.

Related

Change gameobjects position if it's already used by a different gameobject?

Gameobjects are getting randomly spawned on some platform objects. I want to avoid these two different gameobject to be spawned at the same exact location (money2 should change it's position).
Here is the code:
void Start()
{
int randMoney = Random.Range(0, 8);
Vector3 moneyPos = transform.position;
moneyPos.y += 0.5f;
if (randMoney < 1)
{
GameObject moneyInstance = Instantiate(money, moneyPos, money.transform.rotation);
moneyInstance.transform.SetParent(gameObject.transform);
}
int randMoney2 = Random.Range(0, 8);
Vector3 money2Pos = transform.position;
money2Pos.y += 0.5f;
if (randMoney2 < 1)
{
GameObject money2Instance = Instantiate(money2, money2Pos, money2.transform.rotation);
money2Instance.transform.SetParent(gameObject.transform);
}
if (money2Pos == moneyPos)
{
//where gameobject2s position should change
}
}
Thank you for taking your time!
You're trying to do the same thing multiple times here. First the code block for money1 and money2 are similar if not exactly the same. Consider creating a function like this one:
public GameObject SpawnMoney(GameObject moneyPrefab, int spawnProbability, Vector3 spawnOffset)
{
int spawnPourcentage = Random.Range(0, 101); // 101 because the random is exclusive
bool willSpawn = spawnPourcentage <= spawnProbability;
//if not supposed to spawn, then do nothing
if(!willSpawn) return null;
//Spawn effectively the money with position offset
Vector3 plateformePosition = transform.position;
GameObject moneyInstance = Instantiate(moneyPrefab, plateformePosition + spawnOffset, Quaternion.identity, transform);
//return the instance if you want to manipulate it later ;)
return moneyInstance;
}
Now you have your function ready to go, you may spawn your coins in a start or even in a loop if you want to !
If you have questions, please ask

Disable rigidbody on a specific gameobject using C# and Unity

I'm having trouble in my pinch code because it also grabs my player. Here is my code:
using UnityEngine;
using System.Collections;
using Leap;
using Leap.Unity;
/**
* Detects pinches and grabs the closest rigidbody if it's within a given range.
*
* Attach this script to the physics hand object assinged to the HandController in a scene.
*/
public class MagneticPinch : MonoBehaviour {
public const float TRIGGER_DISTANCE_RATIO = 0.7f;
/** The stiffness of the spring force used to move the object toward the hand. */
public float forceSpringConstant = 100.0f;
/** The maximum range at which an object can be picked up.*/
public float magnetDistance = 0.5f;
protected bool pinching_;
protected Collider grabbed_;
public GameObject TrashAudio;
public GameObject myPlayer;
void Start() {
pinching_ = false;
grabbed_ = null;
}
/** Finds an object to grab and grabs it. */
void OnPinch(Vector3 pinch_position) {
pinching_ = true;
// Check if we pinched a movable object and grab the closest one that's not part of the hand.
Collider[] close_things = Physics.OverlapSphere(pinch_position, magnetDistance);
Vector3 distance = new Vector3(magnetDistance, 0.0f, 0.0f);
for (int j = 0; j < close_things.Length; ++j) {
Vector3 new_distance = pinch_position - close_things[j].transform.position;
if (close_things[j].GetComponent<Rigidbody>() != null && new_distance.magnitude < distance.magnitude &&
!close_things[j].transform.IsChildOf(transform)) {
grabbed_ = close_things[j];
distance = new_distance;
}
}
}
/** Clears the pinch state. */
void OnRelease() {
grabbed_ = null;
pinching_ = false;
TrashAudio.gameObject.SetActive (false);
//ActivateRigidbody ();
}
/**
* Checks whether the hand is pinching and updates the position of the pinched object.
*/
void Update() {
bool trigger_pinch = false;
HandModel hand_model = GetComponent<HandModel>();
Hand leap_hand = hand_model.GetLeapHand();
if (leap_hand == null)
return;
// Scale trigger distance by thumb proximal bone length.
Vector leap_thumb_tip = leap_hand.Fingers[0].TipPosition;
float proximal_length = leap_hand.Fingers[0].Bone(Bone.BoneType.TYPE_PROXIMAL).Length;
float trigger_distance = proximal_length * TRIGGER_DISTANCE_RATIO;
// Check thumb tip distance to joints on all other fingers.
// If it's close enough, start pinching.
for (int i = 1; i < HandModel.NUM_FINGERS && !trigger_pinch; ++i) {
Finger finger = leap_hand.Fingers[i];
for (int j = 0; j < FingerModel.NUM_BONES && !trigger_pinch; ++j) {
Vector leap_joint_position = finger.Bone((Bone.BoneType)j).NextJoint;
if (leap_joint_position.DistanceTo(leap_thumb_tip) < trigger_distance)
trigger_pinch = true;
}
}
Vector3 pinch_position = hand_model.fingers[0].GetTipPosition();
// Only change state if it's different.
if (trigger_pinch && !pinching_)
OnPinch(pinch_position);
else if (!trigger_pinch && pinching_)
OnRelease();
//this line of code is a self scripting for disabling the rigidbody from the player component
// Accelerate what we are grabbing toward the pinch.
if (grabbed_ != null) {
Vector3 distance = pinch_position - grabbed_.transform.position;
//DeactivateRigidbody (); //this is not to grab the rigidbody of the player
grabbed_.GetComponent<Rigidbody> ().AddForce (forceSpringConstant * distance);
TrashAudio.gameObject.SetActive (true);
//DeactivateRigidbody ();
}
}
}
Every object must have a rigidbody that is set to USE GRAVITY so my player as well so that it will not go through my terrain . My script is grabs the nearest object that has a rigidbody. The problem is when I grab some object its always grab itself I mean my player. I'm using a leap motion to grab.
So how can I obtain that if my script detects that it is grabbing my player it will stop grabbing but instead grab the object?
Really easy.
You can tag your player like player like in image:
then add to your code something like this into the method OnPinch
// Check if we pinched a movable object and grab the closest one that's not part of the hand.
Collider[] close_things = Physics.OverlapSphere(pinch_position, magnetDistance);
Vector3 distance = new Vector3(magnetDistance, 0.0f, 0.0f);
for (int j = 0; j < close_things.Length; ++j) {
if(close_things[j].gameobject.Tag.ToUppercase() == "PLAYER")
continue; //here you jump to a the next object
PS: Or you can simply use a different layer that ignore the player
By using
//attach this script to pinching
void DeactivateRigid(){
myPlayer.Getcomponent<Rigidbody>().isKenimatic == false;
myPlayer.Getcomponent<Rigidbody().detectCollision = true;
}
//attach this script to release
void ActivateRigid(){
...
}
My Unity is a bit rusted but you can attach a tag to GameObject, https://docs.unity3d.com/ScriptReference/GameObject-tag.html
Be sure to tag the player as a player, afterwards you could use a check whether the nearest gameobjects tag isn't "player".

C# How to make a smooth jump in unity3d without moving the X, towards the nearest object

I would like to make a smooth jump towards the nearest cube. I already have a script to detect the closest cube. I want that the X-axis is locked, so only the Y-axis and the Z-axis change when jumping. I would like to use a Jump animation when jumping. I already tried to use Vector3MoveTowards, but that didn't really work well, maybe I didn't use it properly.
Detect nearest cube where the player should jump to (C#)
void Update()
{
FindClosestCube ();
GameObject closestCube = FindClosestCube ();
Debug.Log (closestCube);
}
GameObject FindClosestCube() {
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("cube");
GameObject closest = null;
float distance = Mathf.Infinity;
float position = transform.position.z;
foreach (GameObject go in gos) {
float diff = go.transform.position.z - position;
float curDistance = diff;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}
The tricky part is that at some cubes you have to jump up (y+1), with some cubes you jump towards the same Y (y+0) and with some cubes you jump down (y-1).
How do I do this?
Image of how it looks like:
EDIT: I have this code right now:
----------------C#-----------------
Rigidbody rb;
public int clicks = 0;
Vector3 target;
public Animation jumpAnimation;
bool jump = false;
float cubeDiffY;
bool movePlayer;
public float smoothTime = 0.3f;
public float yVelocity = 0.0f;
void Start()
{
rb = GetComponent<Rigidbody> ();
}
void Update ()
{
FindClosestCube ();
GameObject closestCube = FindClosestCube ();
Debug.Log ("Closestcube = " + closestCube);
target = closestCube.transform.position + new Vector3 (0f, 0.7f, 0f);
cubeDiffY = target.y - transform.position.y;
movePlayer = true;
Debug.Log("Cube Difference Y-axis = " + Mathf.Round(cubeDiffY));
if (Input.GetMouseButtonDown (0))
{
clicks += 1;
jump = true;
jumpAnimation = gameObject.GetComponent<Animation>();
//jumpAnimation.Play ();
}
if (jump == true)
{
Jump ();
}
}
void Jump()
{
float newPosition = Mathf.SmoothDamp (transform.position.y, target.y, ref yVelocity, smoothTime);
transform.position = new Vector3 (0, newPosition, transform.position.z);
}
I calculated the difference in Y-axis between the cube where the player is standing on and the closestCube. But the Jump() doesn't work. How do I fix that?
Okay I set up a quick version of your game and got what you wanted to work, it is not exactly a quick solution, because what your doing doesn't have built in functionality for other than using animations.
Here is the character script that has all the code you need and commented thoroughly so it should explain itself.
using UnityEngine;
public class Character : MonoBehaviour
{
//the collider for the player
private new BoxCollider collider;
//the jump box collider on a empty game object that is a child to the player object
public BoxCollider JumpBox;
//the offset of the cube so it doesn't stop inside of it
public Vector3 cubeOffset;
//how high the jump will be
public float JumpHeight;
//how fast the jump will be
public float JumpSpeed;
//holds the change in position the jump will produce
private Vector3 jumpDelta;
//holds the destination cube the jump is attempting to hit
private Cube destinationCube;
//true if a jumping animation is currently playing
private bool jumping = false;
//used to swap the jump direction from up to down
private bool jumpDirection = true;
//used to hold the position of the jump so it knows when to stop
private float jumpPosition = 0;
// Use this for initialization
void Start()
{
collider = GetComponent<BoxCollider>();
}
// Update is called once per frame
void Update()
{
if(jumping)
{
//move straight towards the cube
transform.position = transform.position + (JumpSpeed * jumpDelta);
//move up and down to simulate a jump
//check the current move direction
if (jumpDirection)
{
//add to the jump position twice product of the JumpHeight the JumpSpeed so that it will
//rise and fall the same amount of time it takes to move to the destination
jumpPosition += JumpHeight * JumpSpeed * 2;
//if it has passed the jump height reverse the jump direction
if (jumpPosition >= JumpHeight)
jumpDirection = !jumpDirection;
transform.position += transform.up * JumpHeight * JumpSpeed * 2;
}
//the jump direction is going down
else
{
jumpPosition -= JumpHeight * JumpSpeed * 2;
transform.position -= transform.up * JumpHeight * JumpSpeed * 2;
}
//check if the character collider intersects witht he cubes collider
//if it has then stop jumping and set the final position as the destination position
if (collider.bounds.Intersects(destinationCube.BoxCollider.bounds))
{
jumping = false;
transform.position = destinationCube.transform.position + cubeOffset;
}
}
//detect a jump
if (Input.GetKeyDown(KeyCode.Space))
{
//detect all hits on the jump box
Collider[] hits = Physics.OverlapBox(JumpBox.center, JumpBox.size * 0.5f);
//get the closest collider with the right tag
Collider result = GetClosestColliderWithTag(hits, "Cube");
//if we have a result then begin the jumping animation
if(result != null)
{
//gets the destination cubes cube component(the custom class you have on your cubes)
destinationCube = result.gameObject.GetComponent<Cube>();
//calculate the jump delta
jumpDelta = (result.transform.position + cubeOffset) - transform.position;
//remove the left and right components so the jumping doesnt move to the left or right of the player
Vector3 component = Vector3.Project(jumpDelta, -transform.right);
jumpDelta -= component;
component = Vector3.Project(jumpDelta, transform.right);
jumpDelta -= component;
//setup the jump animation control fields to the initial values
jumpPosition = 0;
jumpDirection = true;
jumping = true;
}
}
}
private Collider GetClosestColliderWithTag(Collider[] colliders, string tag)
{
//just gets the closest collider
float distance = float.MaxValue;
int result = -1;
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].tag == tag)
{
float distanceTemp = Vector3.Distance(transform.position, colliders[i].transform.position);
if (distanceTemp < distance)
{
distance = distanceTemp;
result = i;
}
}
}
if (result != -1)
return colliders[result];
else return null;
}
}
And here is my cube script which has some things you will need to add
using UnityEngine;
public class Cube : MonoBehaviour {
//these arent important just fields I used to set up a quick version of your game
public GameObject StartPoint;
public GameObject EndPoint;
public float Speed;
private Vector3 directionVector;
private bool direction;
//YOU WILL NEED THIS!!
[HideInInspector]
public BoxCollider BoxCollider;
// Use this for initialization
void Start() {
//not important
directionVector = EndPoint.transform.position - StartPoint.transform.position;
directionVector.Normalize();
//DONT FORGET TO SET YOUR BOX COLLIDER
BoxCollider = GetComponent<BoxCollider>();
}
// Update is called once per frame
void Update()
{
float distance = 0;
if (direction)
{
distance = Vector3.Distance(EndPoint.transform.position, transform.position);
transform.position += directionVector * Speed;
if (distance < Vector3.Distance(EndPoint.transform.position, transform.position))
direction = !direction;
}
else
{
distance = Vector3.Distance(StartPoint.transform.position, transform.position);
transform.position -= directionVector * Speed;
if (distance < Vector3.Distance(StartPoint.transform.position, transform.position))
direction = !direction;
}
}
}
Previous Answer
I would say you need to calculate the perceived position of the object in the future.
Vector3 futurePos = cubePos + (cubeMoveDirection * cubeMoveSpeed);
Once you have the future position, even if it is not exact, you should aim your animation towards that position. To do this I would have the animation change a speed vector instead of an actual transforms position that way we can rotate this speed vector in any direction you want while keeping the orientation of the block. Otherwise you have to rotate the entire block to point towards the direction you want. If this is what you want then put your block under a empty gameobject, rotate the empty gameobject to point to where you want and do the speed calculations only.
Next your animation should have a net move vector which should be pre-calculated and scaled down or up to meet the distance to the future position. It will look something like this(note this is not tested)
//class fields
Vector3 AnimatedSpeed;
Vector3 AnimationDelta;
//basic calculation
//get the direction vector from the players current position to the future
block position
Vector3 dirVector = futurePos - transform.position;
//find the rotation from the current orientation to the direction vector
Quaternion rotation = Quaternion.FromToRotation(transform.forward, dirVector);
//calculate the distance from you to the cube and scale it with the magnitude of the AnimationDelta
float result = Vector3.Distance(transform.position, futurePos);
result = result / animationDelta.magnitude;
//finally rotate the forward vector by the rotation and multiply it by the
//animation speed and the result to get the step by step movement as
//the animation plays. NOTE: The animation should be based on forward direction
transform.position += (AnimationSpeed * rotation) * result * Time.deltaTime;
Hopefully this does it, like I said I haven't tested it at all so you may have to do some tweaking based on your particular case as this is essentially psuedo-code.
Good luck! I'm off to bed I'll check back when I wake up.

How can i make a character to turn 180 degrees on the place while walking?

using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityStandardAssets.Characters.ThirdPerson;
public class Multiple_objects : MonoBehaviour {
public GameObject prefab;
public GameObject[] gos;
public int NumberOfObjects;
private ThirdPersonCharacter[] thirdPersonCharacter;
private Animator[] _animator;
private int count = 0;
private List<float> floats = new List<float>();
public float smooth = 1f;
private Vector3 targetAngles;
void Awake()
{
Vector3 v3 = prefab.transform.position;
_animator = new Animator[NumberOfObjects];
gos = new GameObject[NumberOfObjects];
for(int i = 0; i < gos.Length; i++)
{
count = count + 2;
GameObject clone = (GameObject)Instantiate(prefab, Vector3.zero, Quaternion.identity);
gos [i] = clone;
gos [i].transform.position = new Vector3 (v3.x - count, v3.y, v3.z);
_animator [i] = gos[i].GetComponent<Animator> ();
float randomspeed = 0f;
// Keep repeating this until we find an unique randomspeed.
while(randomspeed == 0f || floats.Contains(randomspeed))
{
randomspeed = UnityEngine.Random.Range(1.0f, 15.0f);
}
floats.Add (randomspeed);
//float vertInput = 1.0f;
_animator [i].SetFloat ("Speed", randomspeed);
if (randomspeed != 0.0f)
_animator[i].speed = randomspeed;
else
_animator [i].speed = 1.0f;
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.S)) // some condition to rotate 180
targetAngles = gos[5].transform.eulerAngles + 180f * Vector3.up; // what the new angles should be
transform.eulerAngles = Vector3.Lerp(gos[5].transform.eulerAngles, targetAngles, smooth * Time.deltaTime); // lerp to new angles
}
}
In the Hierarchy i have two ThirdPersoncontroller: ThirdPersonController and ThirdPersonController(1)
I put the script on the ThirdPersonController and the prefab is the ThirdPersonController(1) then it's cloning more 15 ThirdPersonControllers.
Then each of the cloned 15 players walk in another speed.
Now in the Update function when i press on the S key the ThirdPersonController is turning 180 degrees and then all the cloned 15 players also turn 180 degrees and keep walking to this direction.
What i want to do is when one of the cloned players getting to Z position turn 180 degrees only this player and turn on place 180 degrees for a second ot two walk on place turn then continue walking to the turned direction.
So in the Update function i'm using now the S key as condition but it's turning the ThirdPersonController and all the clones turn according to him and walk to the turned direction.
I tried to use gos[0].transform also gos[5].transform but it's not turning only the fifth clone but turn all of them 180 degrees.
And the condition i tried to make if(gos[0].transform.position.z == 108.9377f) or if(gos[0].transform.position.z == 100) but it's never get to the next line i used a break point.
Rather than checking when you reach that exact value, I'd check for distance between you and the waypoint using Vector3.Distance
Now for the reason all the characters are walking the same action is because of this line :
transform.eulerAngles = Vector3.Lerp(gos[5].transform.eulerAngles, targetAngles, smooth * Time.deltaTime);
You'd want to do something like this in your update
for(int i = 0; i < gos.Length; i++)
{
//if(Input.GetKeyDown(KeyCode.S)) // some condition to rotate 180
// targetAngles = gos[i].transform.eulerAngles + 180f * Vector3.up;
// If the Distance between gos[i] and Vector3(0,0,100) is smaller than 3f
if(Vector3.Distance(gos[i].transform.position, new Vector3(0,0,100)) < 3f)
{
targetAngles = gos[i].transform.eulerAngles + 180f * Vector3.up;
}
gos[i].transform.eulerAngles = Vector3.Lerp(gos[i].transform.eulerAngles, targetAngles, smooth * Time.deltaTime);
}

objects using their own unique waypoints array

update...
First Class
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Wave
{
public GameObject enemyPrefab;
public float spawnInterval = 2;
public int maxEnemies = 20;
}
public class SpawnEnemy : MonoBehaviour
{
public GameObject[] waypoints;
public GameObject testEnemyPrefab;
public Wave[] waves;
public int timeBetweenWaves = 5;
private GameManagerBehavior gameManager;
private float lastSpawnTime;
private int enemiesSpawned = 0;
// Use this for initialization
void Start()
{
lastSpawnTime = Time.time;
gameManager =
GameObject.Find("GameManager").GetComponent<GameManagerBehavior>();
}
// Update is called once per frame
void Update()
{
// 1 Get the index of the current wave, and check if it’s the last one.
int currentWave = gameManager.Wave;
if (currentWave < waves.Length)
{
// 2 If so, calculate how much time passed since the last enemy spawn and whether it’s time to spawn an enemy. Here you consider two cases.
// If it’s the first enemy in the wave, you check whether timeInterval is bigger than timeBetweenWaves.
// Otherwise, you check whether timeInterval is bigger than this wave’s spawnInterval. In either case, you make sure you haven’t spawned all the enemies for this wave.
float timeInterval = Time.time - lastSpawnTime;
float spawnInterval = waves[currentWave].spawnInterval;
if (((enemiesSpawned == 0 && timeInterval > timeBetweenWaves) ||
timeInterval > spawnInterval) &&
enemiesSpawned < waves[currentWave].maxEnemies)
{
// 3 If necessary, spawn an enemy by instantiating a copy of enemyPrefab. You also increase the enemiesSpawned count.
lastSpawnTime = Time.time;
GameObject newEnemy = (GameObject)
Instantiate(waves[currentWave].enemyPrefab);
newEnemy.GetComponent<MoveEnemy>().waypoints = waypoints;
newEnemy.GetComponent<MoveEnemy>().JiggleWaypoints();
enemiesSpawned++;
}
// 4 You check the number of enemies on screen. If there are none and it was the last enemy in the wave you spawn the next wave.
// You also give the player 10 percent of all gold left at the end of the wave.
if (enemiesSpawned == waves[currentWave].maxEnemies &&
GameObject.FindGameObjectWithTag("Enemy") == null)
{
gameManager.Wave++;
gameManager.Gold = Mathf.RoundToInt(gameManager.Gold * 1.1f);
enemiesSpawned = 0;
lastSpawnTime = Time.time;
}
// 5 Upon beating the last wave this runs the game won animation.
}
else {
gameManager.gameOver = true;
GameObject gameOverText = GameObject.FindGameObjectWithTag("GameWon");
gameOverText.GetComponent<Animator>().SetBool("gameOver", true);
}
}
}
Second Class
using UnityEngine;
using System.Collections;
public class MoveEnemy : MonoBehaviour
{
[System.NonSerialized]
public GameObject[] waypoints;
private int currentWaypoint = 0;
private float lastWaypointSwitchTime;
public float speed = 1.0f;
// Use this for initialization
void Start()
{
lastWaypointSwitchTime = Time.time;
}
// Update is called once per frame
void Update()
{
// 1
Vector3 startPosition = waypoints[currentWaypoint].transform.position;
Vector3 endPosition = waypoints[currentWaypoint + 1].transform.position;
// 2
float pathLength = Vector3.Distance(startPosition, endPosition);
float totalTimeForPath = pathLength / speed;
float currentTimeOnPath = Time.time - lastWaypointSwitchTime;
gameObject.transform.position = Vector3.Lerp(startPosition, endPosition, currentTimeOnPath / totalTimeForPath);
// 3
if (gameObject.transform.position.Equals(endPosition))
{
if (currentWaypoint < waypoints.Length - 2)
{
// 3.a
currentWaypoint++;
lastWaypointSwitchTime = Time.time;
RotateIntoMoveDirection();
}
else {
// 3.b
Destroy(gameObject);
AudioSource audioSource = gameObject.GetComponent<AudioSource>();
AudioSource.PlayClipAtPoint(audioSource.clip, transform.position);
//<< deduct health
GameManagerBehavior gameManager =
GameObject.Find("GameManager").GetComponent<GameManagerBehavior>();
gameManager.Health -= 1;
//>>
}
}
}
public void JiggleWaypoints()
{
for (int i = 1; i < waypoints.Length; i++)
{
waypoints[i].transform.position = new Vector3(waypoints[i].transform.position.x + Random.Range(-3, 3), waypoints[i].transform.position.y + Random.Range(-3, 3), 0);
}
}
private void RotateIntoMoveDirection()
{
//1 It calculates the bug’s current movement direction by subtracting the current waypoint’s position from that of the next waypoint.
Vector3 newStartPosition = waypoints[currentWaypoint].transform.position;
Vector3 newEndPosition = waypoints[currentWaypoint + 1].transform.position;
Vector3 newDirection = (newEndPosition - newStartPosition);
//2 It uses Mathf.Atan2 to determine the angle toward which newDirection points, in radians, assuming zero points to the right.
// Multiplying the result by 180 / Mathf.PI converts the angle to degrees.
float x = newDirection.x;
float y = newDirection.y;
float rotationAngle = Mathf.Atan2(y, x) * 180 / Mathf.PI;
//3 Finally, it retrieves the child named Sprite and rotates it rotationAngle degrees along the z-axis.
// Note that you rotate the child instead of the parent so the health bar — you’ll add it soon — remains horizontal.
GameObject sprite = (GameObject)
gameObject.transform.FindChild("Sprite").gameObject;
sprite.transform.rotation =
Quaternion.AngleAxis(rotationAngle, Vector3.forward);
}
public float distanceToGoal()
{
float distance = 0;
distance += Vector3.Distance(
gameObject.transform.position,
waypoints[currentWaypoint + 1].transform.position);
for (int i = currentWaypoint + 1; i < waypoints.Length - 1; i++)
{
Vector3 startPosition = waypoints[i].transform.position;
Vector3 endPosition = waypoints[i + 1].transform.position;
distance += Vector3.Distance(startPosition, endPosition);
}
return distance;
}
}
Code is working 100% without errors, BUT....
After each spawn all objects get the same waypoint array. This can be seen on the screen as all objects jump to new waypoint in line together each time new object is spawned. I want the object which is already spawn to live life with it's own array of only once created waypoints.
You need to create a new array of waypoints each time you create one from the prefabricated object. You don't show your Instantiate method but I'm guessing that it has a line like this:
this.waypoints = prefab.waypoints;
This will mean that all object you create will share the same list of waypoints (as you've discovered).
What you need is something like this - assuming that the waypoints have X, Y, and Z properties):
this.waypoints = new GameObject[5];
for (int i = 0; i++ ; i < 5)
{
this.waypoints[i].X = prefab.waypoints[i].X;
this.waypoints[i].Y = prefab.waypoints[i].Y;
this.waypoints[i].Z = prefab.waypoints[i].Z;
}
(If you want your points to be a variable length you might want to consider using a list).
This means that each object has a list of unique points even if they start with the same values you can change each independently.
Based on ChrisFs' and Joe Blows' answers, do something like this in your MoveEnemy script:
private Vector3[] myWay;
public void JiggleWaypoints(GameObject[] waypoints)
{
myWay = new Vector3[waypoints.Length];
for(int i = 1; i < waypoints.Length; i++)
{
myWay[i] = new Vector3(waypoints[i].transform.position.x + Random.Range(-3, 4), waypoints[i].transform.position.y + Random.Range(-3, 4), 0);
}
}
myWay replaces the GameObject[].
In your SpawnEnemy script you do this:
GameObject e = (GameObject)Instantiate(enemyPrefab);
e.GetComponent<MoveEnemy>().JiggleWaypoints(waypoints);

Categories