Hello. I Have a baked navigation for a nav mesh component on my player script. Using mirror networking the player spawns on spawn point and it instantiates a camera.
It works JUST FINE when I start the game, host server. My unit spawns and I can move him around using ray trace and nav mesh. Even can right click an enemy and shoot fireballs.
THE ISSUE: When I move the camera with left mous button or keyboard. Raytracing then stops working properly and where I right click is not where the player tries to move, it geets confused and its like its clicking somewhere else.
HERE is my terrible camera control :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class CameraControl : NetworkBehaviour
{
private float speed = 2.0f;
private float zoomSpeed = 2.0f;
public float minX = -360.0f;
public float maxX = 360.0f;
public float minY = -45.0f;
public float maxY = 45.0f;
public float sensX = 100.0f;
public float sensY = 100.0f;
float rotationY = 0.0f;
float rotationX = 0.0f;
void Update()
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
transform.Translate(0, scroll * zoomSpeed, scroll * zoomSpeed, Space.World);
if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.position += Vector3.forward * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.position += Vector3.back * speed * Time.deltaTime;
}
if (Input.GetMouseButton(0))
{
rotationX += Input.GetAxis("Mouse X") * sensX * Time.deltaTime;
rotationY += Input.GetAxis("Mouse Y") * sensY * Time.deltaTime;
rotationY = Mathf.Clamp(rotationY, minY, maxY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
}
}
Here is the player script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using Mirror;
public class Player : MonoBehaviour
{
[SerializeField] private Transform bulletSpawnPoint;
[SerializeField] private GameObject bulletPrefab;
[SerializeField] private float shootDistance = 10f;
[SerializeField] private GameObject playerStar;
[SerializeField] private TextMesh healthText;
private Transform targetedEnemy;
private bool enemyClicked = false;
private bool walking;
public Animator anim;
public UnityEngine.AI.NavMeshAgent navAgent;
private float nextFire;
private float timeBetweenShots = 2f;
private bool isAttacking = false;
private Vector3 startingPosition;
public Camera cam;
// Start is called before the first frame update
void Start()
{
anim.GetComponent<Animator>();
cam.GetComponent<Camera>();
Debug.Log("Hello World");
Instantiate(cam);
}
// Update is called once per frame
void Update()
{
//if(!hasAuthority)
//{
// cam.enabled = false;
// } else
// {
// cam.enabled = true;
//
// }
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Input.GetButtonDown("Fire2"))
{
if (Physics.Raycast(ray, out hit, 100))
{
if (hit.collider.CompareTag("Enemy"))
{
targetedEnemy = hit.transform;
enemyClicked = true;
}
else
{
isAttacking = false;
walking = true;
enemyClicked = false;
navAgent.destination = hit.point;
navAgent.isStopped = false;
}
}
}
if (enemyClicked)
{
MoveAndShoot();
}
if (navAgent.remainingDistance <= navAgent.stoppingDistance)
{
walking = false;
}
else
{
if (!isAttacking)
walking = true;
}
anim.SetBool("IsWalking", walking);
}
void MoveAndShoot()
{
if (targetedEnemy == null)
{
return;
}
navAgent.destination = targetedEnemy.position;
if (navAgent.remainingDistance >= shootDistance)
{
navAgent.isStopped = false;
walking = true;
}
if (navAgent.remainingDistance <= shootDistance)
{
transform.LookAt(targetedEnemy);
if (Time.time > nextFire)
{
isAttacking = true;
nextFire = Time.time + timeBetweenShots;
CmdFire();
}
navAgent.isStopped = true;
walking = false;
}
void CmdFire() {
anim.SetBool("IsAttacking", isAttacking);
GameObject fireball = Instantiate(bulletPrefab, bulletSpawnPoint.position, bulletSpawnPoint.rotation) as GameObject;
Debug.Log("test");
fireball.GetComponent<Rigidbody>().velocity = fireball.transform.forward * 4;
// NetworkServer.Spawn(fireball);
Destroy(fireball, 3.5f);
}
}
}