controlling objects with a character controlled by a nav mesh agent
I'm trying to control objects(like cubes) in my game and move around with them using a 3rd person character controlled by a nav mesh agent. The main objective is to be able to some extent grab onto a cube(or make the cube stick to player when he interacts with it) and move around with it and at the end deposit the cube behind a truck. When the player holding the cube interacts with the truck(behind the truck) as he deposits, the cube should be destroyed or hidden for optimisation purposes. As I said before my player is controlled by a nav mesh agent and has a capsule col & rigidbody attached and I have a Interaction script currently attached to other objects in which the player picks up/collects and those items hide and show up in my inventory slots. but unlike what I'm trying to achieve these items are not movable. Thanks in advance I hope I was understandable. Heres the relevant code:
[Code=CSharp]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
public class PlayerMotor : MonoBehaviour
{
Transform target; //Target to follow
NavMeshAgent agent; //Referance to agent
// Use this for initialization
void Start()
{
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
if(target != null)
{
agent.SetDestination(target.position);
FaceTarget();
}
}
public void MoveToPoint(Vector3 point)
{
agent.SetDestination(point);
}
public void FollowTarget(Interactable newTarget)
{
agent.stoppingDistance = newTarget.radius * .8f;
agent.updateRotation = false;
target = newTarget.interactionTransform;
}
public void StopFollowingTarget()
{
agent.stoppingDistance = 0f;
agent.updateRotation = true;
target = null;
}
void FaceTarget()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0f, direction.z));
if(direction != Vector3.zero)
{
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
}
}
}[/Code]
[Code=CSharp]
using UnityEngine.EventSystems;
using UnityEngine;
[RequireComponent(typeof(PlayerMotor))]
public class PlayerController : MonoBehaviour
{
Camera cam;
PlayerMotor motor;
public Interactable focus;
public LayerMask movementMask; //Filter out everything not walkable
// Use this for initialization
void Start()
{
cam = Camera.main; //Referance to camera
motor = GetComponent<PlayerMotor>(); //Referance to motor
}
// Update is called once per frame
void Update()
{
if (EventSystem.current.IsPointerOverGameObject())
return;
//If we left click on mouse
if (Input.GetMouseButtonDown(0))
{
//We create a ray
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//If the ray hits
if (Physics.Raycast(ray, out hit, 100, movementMask))
{
motor.MoveToPoint(hit.point); // Move to where we hit
RemoveFocus();
}
}
//If we right click on mouse
if (Input.GetMouseButtonDown(1))
{
//We create a ray
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//If the ray hits
if (Physics.Raycast(ray, out hit, 100))
{
Interactable interactable = hit.collider.GetComponent<Interactable>();
if (interactable != null)
{
SetFocus(interactable);
}
}
}
}
void SetFocus(Interactable newFocus)
{
if(newFocus != focus)
{
if(focus != null)
focus.OnDefocused();
focus = newFocus;
motor.FollowTarget(newFocus);
}
newFocus.OnFocused(transform);
}
void RemoveFocus()
{
if(focus != null)
focus.OnDefocused();
focus = null;
motor.StopFollowingTarget();
}
}
[/Code]
[Code=CSharp]
using UnityEngine;
public class Interactable : MonoBehaviour
{
public float radius = 2f;
public Transform interactionTransform;
bool isFocus = false;
Transform player;
bool hasInteracted = false;
public virtual void Interact()
{
//This method is meant to be overwritten
Debug.Log("Interacting with" + transform.name);
}
void Update()
{
if (isFocus && !hasInteracted)
{
float distance = Vector3.Distance(player.position, interactionTransform.position);
if(distance <= radius)
{
Interact();
hasInteracted = true;
}
}
}
public void OnFocused(Transform playerTransform)
{
isFocus = true;
player = playerTransform;
hasInteracted = false;
}
public void OnDefocused()
{
isFocus = false;
player = null;
hasInteracted = false;
}
private void OnDrawGizmosSelected()
{
if (interactionTransform == null)
interactionTransform = transform;
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(interactionTransform.position, radius);
}
}
[/Code]
Your answer
Follow this Question
Related Questions
Picking up item if player is within a certain radius of the item. 0 Answers
How to move in game 2D game object with mouse? 2 Answers
Not sure what this error is telling/asking me to do? 1 Answer
How can I make my VR controller stick to a virtual Joystick and still control it? 1 Answer
Allow player to pick up item only if they are in a certain radius. 0 Answers