I am learning Color Switch Unity 2D game. When my player touches any color of obstacles, it dies. Although it hits the same color of obstacles, game is over. Player should not be dies. Could you help me out with that?
public class Obstacles : MonoBehaviour
{
private Player player;
void Start()
{
player = GameObject.FindObjectOfType<Player>();
}
void OnTriggerEnter2D(Collider2D target)
{
if (player.activeColor != this.tag)
{
player.GameOver();
}
}
}
With class OnTriggerEnter2D, my player dies even though it touches with the same color of obstacles. Without it, the player continues even it touches the obstacles.
P.S.: it seems so easy to fix it up. But I am a beginner in Unity.
Related
In my game enemies are shot out from a cannon onto a spinning wheel. I want them to stick onto the spinning object on collision and spin in the direction of the spinning wheel.
Here's the code I have for when the enemy collides with the spinning circle.
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Spinner")
{
joint = gameObject.AddComponent<FixedJoint2D>();
joint.connectedBody = collision.rigidbody;
}
}[![enter image description here][1]][1]
When I hit the enemy(which has the navMeshAgent) with my player the enemy is sent to the corner of the map and fidgets. It is sent in the direction that the player pushes it in. I just want it to go to the player for now.
The code
{
GameObject playerPosition;
NavMeshAgent navMesh;
// Start is called before the first frame update
void Start()
{
playerPosition = GameObject.FindGameObjectWithTag("Player");
navMesh = GetComponent<NavMeshAgent>();
if(navMesh == null)
{
Debug.Log("Nav Mesh for enemy brock");
}
}
private void Update()
{
Vector3 dir = playerPosition.transform.position;
navMesh.destination = dir;
}
}
This is what it looks like
I am using an orthographic projection for camera to follow a player. I would like to find all gameobjects between the player and the camera so I can change the opacity so they are partially transparent while blocking the camera's view. I read about raycasting, but it seems it would give only the first object between the player and camera. What approaches are there to accomplish this?
Just use Physics.RaycastAll like this:
public class CameraScript : MonoBehaviour
{
//Attach this script to the camera//
public GameObject player;
void Update()
{
float dist = Vector3.Distance(transform.Position, player.transform.position);
RaycastHit[] hits = hits = Physics.RaycastAll(transform.position,
transform.forward, 100.0F);
foreach (RaycastHit h in hits)
{
//Change the opacity of the of each object to semitransparent.
}
}
}
I created a multiplayer game with photon in unity. The player is a rolling ball, i want to set a camera for each player but it can't be a child of the ball otherwise it rotates to. Without photon it worked with a script on the camera but now with the multiplayer the camera doesn't follow the rolling ball. How can i fix it?
You need to create a script and add it to your camera.
public GameObject player = GameObject.Find("Player");
this.transform.position = new Vector3(player.transform.position.x, player.transform.position.y, transform.position.z);
So your player is always in center of your camera.
You should add this piece of code.
GameObject player;
Vector3 cameraOffset;
void Start()
{
player = GameObject.Find("Player");
cameraOffset = new Vector3(0f, 0f, 0f)
}
void Update()
{
transform.position = new Vector3(player.transform.position.x + cameraOffset.x, player.transform.position.y + cameraOffset.y, player.transform.position.z + cameraOffset.z);
}
and attach it to you camera's script. I put the Offset as (0,0,0) but you should set an offset so your camera doesn't go inside of your player GameObject, but the amount is up to your criteria.
Is there a way to make a sprite draggable but only when the sprite itself is touched? Currently i have my game ,which uses anengine, set up to where the sprite follows your finger ever time the scene is touched. If you touch the opposite side of the scene the sprite gets "teleported" which is what i dont want.
I tried overriding the onAreaTouched method of the sprite and made it set its coordinates to where your finger currently is but this doesnt work too well. If you make sudden movements the draggablity wears off.
is there any simple way to accomplish this?
Answering my own question... i used this code and it worked perfectly:
draggableSprite = new Sprite(CAM_WIDTH/2, CAM_HEIGHT/2,
spriteTextureRegion, mVertexBufferObjectManager){
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionMove()){
spriteIsTouched = true;
}
else{
spriteIsTouched = false;
}
return super
.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
}
};
scene.attachChild(draggableSprite);
scene.registerTouchArea(draggableSprite);
scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
#Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
// TODO Auto-generated method stub
if(spriteIsTouched){
draggableSprite.setPosition(pSceneTouchEvent.getX() - (draggableSprite.getWidth()/2), pSceneTouchEvent.getY() - (draggableSprite.getHeight()/2));
//This sets the position of the sprite and then
//offsets the sprite so its center is at your finger
}
return false;
}
});`