The children help with the code, that gets the character is controlled from the
The children help with the code, that gets the character is controlled from the keyboard and moving the crosshair cursor as much lead there and directed sight. I would like to replace it and put the joystick and fire button, but do not know how to try out is all that you can, you're my last hope
PLAYER using UnityEngine; using System.Collections;
[RequireComponent (typeof (PlayerController))] [RequireComponent (typeof (GunController))] public class Player : LivingEntity {
public float moveSpeed = 5;
public Crosshairs crosshairs;
Camera viewCamera;
PlayerController controller;
GunController gunController;
protected override void Start(){
base.Start();
}
void Awake(){
controller = GetComponent<PlayerController>();
gunController = GetComponent<GunController>();
viewCamera = Camera.main;
FindObjectOfType<Spawner> ().OnNewWave += OnNewWave;
}
void OnNewWave( int waveNumber){
health = startingHealth;
gunController.EquipGun (waveNumber - 1);
}
void Update(){
// Movement input
Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
Vector3 moveVelocity = moveInput.normalized * moveSpeed;
controller.Move(moveVelocity);
// Look input
Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.up * gunController.GunHeight);
float rayDistance;
if (groundPlane.Raycast(ray, out rayDistance)){
Vector3 point = ray.GetPoint(rayDistance);
//Debug.DrawLine(ray.origin,point,Color.red);
controller.LookAt(point);
crosshairs.transform.position = point;
crosshairs.DetectTargets (ray);
if((new Vector2 (point.x, point.z) - new Vector2 (transform.position.x, transform.position.z)).sqrMagnitude > 1){
gunController.Aim (point);
}
}
// Weapon input
if (Input.GetMouseButton(0)){
gunController.OnTriggerHold();
}
if (Input.GetMouseButtonUp(0)){
gunController.OnTriggerRelease();
}
if (Input.GetKeyDown (KeyCode.R)) {
gunController.Reload ();
}
if (transform.position.y < -10) {
TakeDamage (health);
}
}
public override void Die(){
AudioManager.instance.PlaySound ("Play Death", transform.position);
base.Die ();
}
} PLAYER CONTROLLER
using UnityEngine; using System.Collections;
[RequireComponent (typeof (Rigidbody))] public class PlayerController : MonoBehaviour {
Vector3 velocity;
Rigidbody myRigidbody;
void Start () {
myRigidbody = GetComponent<Rigidbody>();
}
public void Move(Vector3 _velocity) {
velocity = _velocity;
}
public void LookAt (Vector3 lookPoint) {
Vector3 heightCorrectedPoint = new Vector3( lookPoint.x, transform.position.y, lookPoint.z);
transform.LookAt(heightCorrectedPoint );
}
void FixedUpdate(){
myRigidbody.MovePosition(myRigidbody.position + velocity *Time.fixedDeltaTime);
}
}
1.png
(146.1 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612084443im_/https://answers.unity.com/themes/thub/images/avi.jpg)