Make an object move forward and back in one click.,Moving an object forward and back to it's original place with one click.
Sorry if this is missing some details, I typed it out and it got deleted and now I don't remember word for word what I wrote.
So I am working on my first actual game and am trying to make an object move toward the mouse, maybe 0.5 - 1 units, and then go back to default position. For this Idea I believe GetMouseButtonDown and GetMouseButtonUp will work fine.
I have a script that teleports the object to mouse but that isn't what I want as I only want it to go a small distance from the player (like a stabbing animation). and I also want to ignore the Y axis and just move it along the X and Z axis.
This script is made from a combination of what I found online and my direction/looking script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ClickAttack : MonoBehaviour {
Vector3 newPosition;
void Start () {
newPosition = transform.position;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
newPosition = hit.point;
transform.position = newPosition;
}
}
}
}
P.S. I am using Unity 2017.3.0 because I started this game in that version and didn't want to update and lose anything, please also let me know if I should update before I am finished.