I'm using this joystick and Mirror for networking
https://github.com/herbou/Unity_EasyJoystick
What I wanted is to detect the gameobject Joystick which is the child of Canvass using Raycast
I've tried this script
public class ShootBullet : NetworkBehaviour
{
[SerializeField]
private GameObject bulletPrefab;
private GameObject bullet;
// Set via the Inspector in Units/second
[SerializeField] private float _moveSpeed = 2f;
[SerializeField] private Camera _camera;
Vector3 touch_Pos = new Vector3(0, 0, 0);
private void Awake()
{
if (!_camera) _camera = Camera.main;
}
void Update()
{
if (Input.touchCount > 0 && this.isLocalPlayer)
{
Ray ray = _camera.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider != null)
{
Debug.Log(hit.collider.gameObject.name);
}
}
}
}
}
But it doesn't work
If you want to detect ui element use:
UI.GraphicRaycaster
more information here
https://docs.unity3d.com/2017.3/Documentation/ScriptReference/UI.GraphicRaycaster.Raycast.html
Related
I have prefab enemyRangeAttack and then I add this code for enemy (child of enemyRangeAttack)
public class Selectable : MonoBehaviour{
[HideInInspector] public GameObject Player;
public float range = 10f;
private void Awake()
{
Player = GameObject.FindGameObjectWithTag("Player");
}
private void OnMouseDown()
{
Debug.LogWarning("Mouse down "+ gameObject);
float dist = Vector3.Distance(Player.transform.position, transform.position);
if (dist < range)
{
Selected();
}
}
public void Selected()
{
GameObject newSelection = ObjectPooler.instance.SpawnFromPool(
"Mark",
transform.GetChild(0).gameObject.transform.position,
Quaternion.identity
);
newSelection.transform.SetParent(transform);
}
}
Game object enemyRangeAttack have circle collider2d and trigger is on and Game object enemy have too
And I have 3 scene, method OnMouseDown() not working for all my scene. How can I fix this
Capsule Collider height: 2; Capsule Collider radius: 0.5 While the
player is rgounded the Grounded the Falling and Grounded parameters
turn on and off continuously. This is the project (unity package)
Conditions: From Movements to Jump, Jump parameter has to be
triggered. From Jump to Falling, Falling parameter has to be true.
From Falling to Landing, Grounded parameter has to be true. From
Movements to Falling, Falling parameter has to be true.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Game.Manager;
public class PlayerController : MonoBehaviour
{
InputManager inputManager;
Animator animator;
Rigidbody playerRb;
CapsuleCollider collider;
bool hasAnimator;
bool grounded;
[SerializeField] float jumpFactor = 250f;
float Dis2Ground;
[SerializeField] LayerMask GroundCheck;
[SerializeField] float airResistance = 0.8f;
void Start()
{
collider = GetComponent<CapsuleCollider>();
Dis2Ground = collider.bounds.extents.y;
inputManager = GetComponent<InputManager>();
playerRb = GetComponent<Rigidbody>();
hasAnimator = TryGetComponent<Animator>(out animator);
fallingHash = Animator.StringToHash("Falling");
jumpHash = Animator.StringToHash("Jump");
groundHash = Animator.StringToHash("Grounded");
}
private void FixedUpdate()
{
HandleJump();
SampleGround();
}
private void HandleJump()
{
if (!hasAnimator) return;
if (!inputManager.Jump) return;
animator.SetTrigger(jumpHash);
}
public void JumpAddForce()
{
playerRb.AddForce(Vector3.up * jumpFactor, ForceMode.Impulse);
playerRb.AddForce(-playerRb.velocity.y * Vector3.up, ForceMode.VelocityChange);
animator.ResetTrigger(jumpHash);
}
private void SampleGround()
{
if (!hasAnimator) return;
RaycastHit hitInfo;
if(Physics.Raycast(transform.position, Vector3.down, out hitInfo, Dis2Ground + 0.1f))
{
grounded = true;
SetAnimationGrounding();
return;
}
Debug.Log(grounded);
animator.SetFloat(zVelHash, playerRb.velocity.y);
grounded = false;
SetAnimationGrounding();
return;
}
private void SetAnimationGrounding()
{
animator.SetBool(fallingHash, !grounded);
animator.SetBool(groundHash, grounded);
}
}
I'm using this joystick and Mirror for networking
https://github.com/herbou/Unity_EasyJoystick
And I have this script to fire a bullet on the user touch
public class ShootBullet : NetworkBehaviour
{
[SerializeField]
private GameObject bulletPrefab;
private GameObject bullet;
// Set via the Inspector in Units/second
[SerializeField] private float _moveSpeed = 2f;
[SerializeField] private Camera _camera;
Vector3 touch_Pos = new Vector3(0, 0, 0);
private void Awake()
{
if (!_camera) _camera = Camera.main;
}
void Update()
{
if (Input.touchCount > 0 && this.isLocalPlayer)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
var touch = Input.GetTouch(0);
touch_Pos = _camera.ScreenToWorldPoint(touch.position);
this.CmdShoot();
}
}
if (bullet != null)
{
bullet.transform.position = Vector3.MoveTowards(bullet.transform.position, touch_Pos, _moveSpeed * Time.deltaTime);
}
}
[Command]
void CmdShoot()
{
bullet = Instantiate(bulletPrefab, this.transform.position, Quaternion.identity);
NetworkServer.Spawn(bullet);
}
}
My problem right now is it's firing a bullet when I touch the joystick area. What is the efficient way to exclude input.touch on the joystick area
You can raycast touch position and check the result.
var mousePos = Input.mousePosition;
var mouseToRay = _camera.ScreenPointToRay(mousePos);
RaycastHit hit;
if (Physics.Raycast(mouseToRay, out hit))
{
var gameObject = hit.collider.gameObject;
//Here You can check the name of your object or the layer or tag
var isMouseUnderJoyStick = gameObject.CompareTag("Your_UI_Tag");
}
And you have to add collider component to your JoyStick
For some reason, my player character shakes up and down when the platform moves up and down, which prevents the player from jumping because the player is not grounded. I tried several things including adding a kinematic rigidbody to the platform and trying to make the player a child of the platform after landing on the platform but nothing has worked so far. Any help would be appreciated. Thank you!
Here is my code:
public class MovingPlatform : MonoBehaviour
{
private Vector3 posA;
private Vector3 posB;
private Vector3 nexPos;
public GameObject Player;
[SerializeField]
private float speed;
[SerializeField]
private Transform childTransform;
[SerializeField]
private Transform transformB;
void Start()
{
posB = childTransform.localPosition;
posB = transformB.localPosition;
nexPos = posB;
}
// Update is called once per frame
void Update()
{
Move();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.tag == "Player")
{
collision.collider.transform.SetParent(transform);
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.collider.tag == "Player")
{
collision.collider.transform.SetParent(null);
}
}
private void Move()
{
childTransform.localPosition = Vector3.MoveTowards(childTransform.localPosition, nexPos, speed * Time.deltaTime);
if (Vector3.Distance(childTransform.localPosition,nexPos) <= 0.1)
{
ChangeDestination();
}
}
private void ChangeDestination()
{
nexPos = nexPos != posA ? posA : posB;
}
}
Move(); should be executed in lateupdate, because otherwise you will set the position of the player and later the physics system will update it, causing stuttering.
Another solution would be raycasting straight down and placing your GameObject on the collision point (plus some kind of offset)
In my Unity my Player gets everytime stuck in the collider when he has a big force (If he falls from a high place, The Gravity Scale or his jumppower is high). In the picture you can see Colliders and Renderer.
Only one Component is not on in screen at the platformcomponents, Tilemap.
Here is my code for Playermovement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] private LayerMask platformsLayerMask;
private Rigidbody2D rigidbody2d;
private BoxCollider2D boxCollider2d;
private void Awake()
{
rigidbody2d = transform.GetComponent<Rigidbody2D>();
boxCollider2d = transform.GetComponent<BoxCollider2D>();
}
private void Update()
{
if (IsGrounded() && Input.GetKeyDown(KeyCode.Space))
{
float jump_power = 17.5f;
rigidbody2d.velocity = Vector2.up * jump_power;
}
HandleMovement();
}
private bool IsGrounded()
{
RaycastHit2D raycastHit2d = Physics2D.BoxCast(boxCollider2d.bounds.center, boxCollider2d.bounds.size, 0f, Vector2.down, .1f, platformsLayerMask);
return raycastHit2d.collider != null;
}
private void HandleMovement()
{
float moveSpeed = 10f;
if (Input.GetKey(KeyCode.A))
{
rigidbody2d.velocity = new Vector2(-moveSpeed, rigidbody2d.velocity.y);
}
else if (Input.GetKey(KeyCode.D))
{
rigidbody2d.velocity = new Vector2(+moveSpeed, rigidbody2d.velocity.y);
}
else
{
rigidbody2d.velocity = new Vector2(0, rigidbody2d.velocity.y);
}
}
}
Have you tried setting the player's rigidbody collision detection to continuous? If the collision detection is set to discrete, it's really easy to come accross this kind of problem when using the built in physics system