Hi everyone!
I got this problem that I am trying to solve for a few days now.
I have a game in which you have to click an object that is tagged “Movable”… but only click, not hold!
Since it should be about clicking only once and not holding, I am trying to solve my problem using Coroutine combined with Vector3.MoveTowards, however it doesn’t even move!
I have tried a few variations now, but I can’t make this GameObject to move no matter what.
On clicking the object it should store itself as a GameObject:
active = hit.transform.gameObject;
…and make target positions out of it depending on which key is pressed(w,a,s,d - we all know the directions of those):
Storing GameObject position:
x = active.transform.position.x;
y = active.transform.position.y;
z = active.transform.position.z;
… making target positions:
left = x + distanceStep;
right = x - distanceStep;
forward = z - distanceStep;
back = z + distanceStep;
So all in all:
- the object has to move on single click and at constant speed smoothly
- while moving - can’t do anything else!
- move exactly 4 units as it is defined in distance variable
Maybe I am still lacking some knowledge, but I am short on time and I have to solve this problem for my game to finally work.
It is becoming extremely frustrating.
Please help me with this because I have tried to read all of the official scripting documentation and yet here I am having this, what I believe to be a primitive problem.
And please keep the solution somewhat simple if it is possible since I am still something between Beginner and Intermediate level programmer, but I am slowly getting better!
Here is a full code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour{
private GameObject active;
private bool moving;
private float force, speed, distanceStep;
private float x, y, z;
private float left, right, forward, back;
private Vector3 position;
private Vector3 goLeft, goRight, goForward, goBack;
private Vector3 cursor;
void Start()
{
distanceStep = 4.0f;
force = 1.0f;
speed = force * Time.deltaTime;
}
void Update()
{
RaycastHit hit;
cursor = Input.mousePosition;
Ray ray = Camera.main.ScreenPointToRay(cursor);
if (Physics.Raycast(ray, out hit))
{
Debug.Log("HITTING ALL");
if (hit.collider.tag == "Movable")
{
Debug.Log("HITTING TAGGED");
if (Input.GetButtonDown("Fire1"))
{
Debug.Log("CLICKING TAGGED");
active = hit.transform.gameObject;
moving = true;
x = active.transform.position.x;
y = active.transform.position.y;
z = active.transform.position.z;
position = new Vector3(x, y, z);
left = x + distanceStep;
right = x - distanceStep;
forward = z - distanceStep;
back = z + distanceStep;
goLeft = new Vector3(left, y, z);
goRight = new Vector3(right, y, z);
goForward = new Vector3(x, y, forward);
goBack = new Vector3(x, y, back);
}
}
}
if (moving)
{
if (Input.GetKeyDown(KeyCode.A))
{
Debug.Log("going left");
StartCoroutine(move(goLeft));
}
else if (Input.GetKeyDown(KeyCode.D))
{
Debug.Log("going right");
StartCoroutine(move(goRight));
}
else if (Input.GetKeyDown(KeyCode.W))
{
Debug.Log("going forward");
StartCoroutine(move(goForward));
}
else if (Input.GetKeyDown(KeyCode.S))
{
Debug.Log("going back");
StartCoroutine(move(goBack));
}
}
}
IEnumerator move(Vector3 target)
{
float distance = Vector3.Distance(position, target);
if(distance > 0)
{
position = Vector3.MoveTowards(position, target, speed);
moving = false;
yield return null;
}
moving = true;
}
}