Play animation when moving with mobile joystick - unity3d

Recently i implemented mobile joystick to my game in unity. But i want to play run animation with animator when the player is moving forwards or backwards. The problem is that when the player is moving forwards or backwards the animation does not play.
Note: I tried moving animator.SetBool("isRunning", true); inside if (currentPos != lastPos) the animation is playing correctly
Part of my player movement script:
horizontalInput = joystick.Horizontal;
Vector3 currentPos = gameObject.transform.position;
if (currentPos != lastPos)
{
// Player is moving
// Check if the player is moving forwards or backwoards
if (currentPos.x < lastPos.x)
{
// Player is moving backwoards
animator.SetBool("isRunning", true);
animator.SetBool("isRunning", false);
}
else if (currentPos.x > lastPos.x)
{
// Player is moving forwards
animator.SetBool("isRunningBackword", true);
animator.SetBool("isRunningBackword", false);
}
lastPos = currentPos;
}

Related

Jump functionality for a 2D Top Down Unity game?

I am struggling to find an efficient way to let my player jump in a 2D Top Down world. I can see a lot of tutorials about platformer views where the camera is oriented at the side of the player, but nothing really working for a top down view like startdew Valley.
I am not using physics, so I move the character on the tilemap using a Couroutine which moves the player to the next position on grid, here it is my Update and DoMove methods:
private void Update()
{
if (!isMoving)
{
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
if (input.x != 0)
input.y = 0;
if (input != Vector2.zero)
{
animator.SetFloat("Horizontal", input.x);
animator.SetFloat("Vertical", input.y);
var targetPos = transform.position + new Vector3(input.x, input.y, 0f);
// obstacle detection
Vector3Int obstaclesMapTile = obstacles.WorldToCell(targetPos - new Vector3(0, .5f, 0));
if (obstacles.GetTile(obstaclesMapTile) == null)
{
StartCoroutine(DoMove(targetPos));
}
}
animator.SetFloat("Speed", input.sqrMagnitude);
}
}
private IEnumerator DoMove(Vector3 newPos)
{
isMoving = true;
while ((newPos - transform.position).sqrMagnitude > Mathf.Epsilon)
{
transform.position = Vector3.MoveTowards(transform.position, newPos, moveSpeed * Time.fixedDeltaTime);
yield return null;
}
transform.position = newPos;
isMoving = false;
}
Is there anybody which could give me an hint on how to add a jumping feature? ( ideally with animation support?) I am kind of running out of ideas.
Thanks in advance.
Just think of it as animation only. Since it is 2D top down, it's more about it looking like it jumps, and then if it has to go over something while in the jump animation, test for just that.
For example; if over hole and jump animation is playing, then allow movement over the whole, otherwise fall. So if the player presses the button for jump, the animation would play, and there should be some variable storing what animation the player is currently in.

Unity 3D NavMesh Agent when hit breaks

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

Move object with hinge joint, but keep it connected to hinge

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.

How to let a camera follow a rolling ball with photon in unity

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.

buttons only react to clicks/touches when clicking/touching to the right of them

I'm making a 2D game in Unity3D for android. Right now I'm making buttons. And this buttons does not react clicks/touched properly. I've got same issue with mouse clicks and touches both. Every button has trigger boxcollider with a same size as an object. BUT buttons react only when I click on area, that is right from a button. I don't understand why is it so. What should I do? Here is my code:
if (Input.GetMouseButtonDown(0)) {
Vector3 i = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 1));
RaycastHit2D hit = Physics2D.Raycast (i, i);
if (hit.transform != null) {
if (hit.transform.tag == "button") {
hit.transform.gameObject.SetActive(false);
}
}
}
Also, I've instantiated an object on mouse click on "i" position to check does it convert screen position to world correctly, and it works fine.
the first parameter in Physics2D.Raycast is the origin and the second one is direction so you should make the raycast from your ray.origin in the direction of ray.direction
void Update () {
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction, Mathf.Infinity);
if (hit) {
if(hit.collider.gameObject.tag=="button"){
//do something
}
}
}
}
Try to handle it by this way:
if (Input.GetMouseButtonDown(0)) {
Vector3 pos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector2 touchPos = new Vector2(pos.x, pos.y);
Collider2D hit = Physics2D.OverlapPoint(touchPos);
if (hit) {
Debug.Log(hit.transform.gameObject.name);
if (hit.transform.tag == "button") {
hit.transform.gameObject.SetActive(false);
}
}
}