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]
Related
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 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.
I have multiple spheres in my mobile game that are attached to hinge joints. The spheres are able to swing and knock into each other, causing other spheres to swing. I am creating movement in the spheres by touching on a sphere and dragging it to a new location. Letting go is supposed to cause the sphere that I just moved to swing accordingly.
The issue is that I am able to move the spheres well outside of the space provided by the hinge. I never want the spheres to move anywhere that they wouldn't be able to swing using the hinge. I am able to move the spheres multiple units/meters away from their original position, when ideally I wouldn't be able to move them more than a few centimeters. The spheres should just stop moving if I hit a limit in the hinge.
Here's my code for the script that controls movement of the spheres:
GameObject selectedObject;
Vector3 screenPoint;
Vector3 offset;
void Update () {
if (Input.touchCount == 0)
{
return;
}
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began) // when screen is touched...
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenToWorldPoint(touch.position), Camera.main.transform.forward, out hit)) // ...cast a ray...
{
if (hit.collider.tag == "Sphere") //...and check if ray hits a sphere
{
selectedObject = hit.collider.gameObject;
screenPoint = Camera.main.WorldToScreenPoint(selectedObject.transform.position);
offset = selectedObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, screenPoint.z));
}
}
}
if (touch.phase == TouchPhase.Moved)
{
Vector3 touchPoint = new Vector3(touch.position.x, touch.position.y, screenPoint.z);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touchPoint) + offset;
selectedObject.transform.position = touchPosition;
}
}
Any help is greatly appreciated! Let me know if I need to explain more or show a video of the issue.
I know I am a little late but you can use AddForce() and AddTorque() methods on the object you want to move but that works only if you have RigidBody on the object.
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.