Creating Objects on Right Click to Navigate Around
A class assignment that I am working on requires me to have a player character move with left click and create objects the player character cannot pass through with right click.
I am having issues with my player character interacting with impassible objects, however, where the game eventually thinks that the player has reached the destination, even if it hasn't. Furthermore, the player object does not navigate around the object, and I am unable to figure out how to get it to do so. Also, for some reason, when I attempt to create a new impassible object, the game is creating two and placing one at the point I clicked, and the other on the middle of the plane.
This is the code that I have:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
// Max Speed Variable
private float MaxSpeed;
// Flag for If Clicked
private bool ifClicked;
// Player y axis location
private float playeryAxis;
// Destination Point
private Vector3 endPoint;
// Create Object Prefab
// Location Points for Righ Click Objects
private Vector3 objPoint;
// Use this for initialization
void Start ()
{
// Set Max Speed
MaxSpeed = 10f;
// Set ifClicked Flag to False
ifClicked = false;
// Get the y Axis value of the Player
playeryAxis = gameObject.transform.position.y;
}
// Update is called once per frame
void Update ()
{
if(Input.GetMouseButtonDown(0) && !ifClicked)
{
RaycastHit hit;
// Ray for Clicked Position
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Check if Ray hits Collider
if (Physics.Raycast(ray,out hit))
{
// Set ifClicked Flag to true
ifClicked = true;
endPoint = hit.point;
// Make sure Player does not move to incorrect y Axis point
// Only x Axis is necessary
// Return y Axis to original point
endPoint.y = playeryAxis;
}
}
// Check if ifClicked is true and if Player is not at clicked location
if (ifClicked && !(gameObject.transform.position.magnitude == endPoint.magnitude))
{
gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 1 / (MaxSpeed * (Vector3.Distance(gameObject.transform.position, endPoint))));
}
// Else if ifClicked is true and Player is at clicked location set ifClicked back to false
else if (ifClicked && (gameObject.transform.position.magnitude == endPoint.magnitude))
{
ifClicked = false;
}
// If Right Clicked
if (Input.GetMouseButtonDown(1))
{
RaycastHit hit;
// Ray for Clicked Position
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Check if Ray hits Collider
if (Physics.Raycast(ray, out hit))
{
// Get position for Object
objPoint = hit.point;
// Make sure Object is on the same y Axis as Player
objPoint.y = playeryAxis;
Instantiate(GameObject.CreatePrimitive(PrimitiveType.Cube), new Vector3(objPoint.x, objPoint.y, objPoint.z), Quaternion.identity);
}
}
}
}
I am still new to unity and C#, so I'm not really sure what all I'm doing. Any help would be greatly appreciated.
Your answer
Follow this Question
Related Questions
Game Object 2 different movement pivots 0 Answers
How to change scene when video ends? 0 Answers
Unable to pass values from one script to another 1 Answer
Disable 2 Scripts during Countdown 1 Answer