How to move forward after staring at one point then stop when looking elsewhere ?
Hello everybody,
My question is quite trikcy but it's because I'm trying some VR possibilities.
What I want is my player to move when looking somewhere, but he's moving only when staring at one point more than 2 seconds. Also I want him to stop moving if he's looking somewhere else, unless he looks for more than 2 seconds then he's walking there.
I don't know if it's clear, but what I want is not using a controller but to let the player move just by looking where he wants to go.
Thanks for your answers,
It's not tricky at all, but actually pretty simple. One question i have is are the positions predefined points in your game world or can the player move wherever he/she wants?
I will answer after you have answered my question :)
Thanks for your answer! I want my player to move where he wants, by just looking :)
Answer by Anton-Korhonen · Nov 27, 2016 at 03:05 PM
Note: This is untested code, you can use this as a base to refine your own perfect movement system:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float maxRayDistance = 100f;
public float waitTime = 2f;
public float headMovementRange = 3f;
public LayerMask layerMask;
private bool waiting = false;
private Vector3 currentRayPosition;
private Vector3 initialRayPosition;
public Transform player;
private RaycastHit target;
private void MoveToTargetPos(Vector3 position)
{
player.transform.position = initialRayPosition;
}
private IEnumerator WaitForMovement()
{
yield return new WaitForSeconds(waitTime);
MoveToTargetPos(initialRayPosition);
waiting = false;
}
// Update is called once per frame
void Update () {
Vector3 rayDirection = transform.TransformDirection(Vector3.forward);
Ray ray = new Ray(transform.position, rayDirection);
// Fire a ray from current head position towards rayDirection with a range of maxRayDistance
if (Physics.Raycast(ray, out target, maxRayDistance, layerMask))
{
if(!waiting)
{
// This is the first time the ray hits the ground, so let's store that position
initialRayPosition = target.point;
StartCoroutine(WaitForMovement());
// Set waiting to true so we start the coroutine only once
waiting = true;
}
// Store the current position of the ray
currentRayPosition = target.point;
// Calculate distance between the first ray and the current ray
if(Vector3.Distance(initialRayPosition, currentRayPosition) > headMovementRange)
{
/*
The distance is larger then headMovementRange, so stop the coroutine and let's start
from the beginning
*/
StopCoroutine(WaitForMovement());
waiting = false;
}
}
}
}
Attach this script to your eye camera and assign the player transform reference and test it out. Basically what it does:
It fires a raycast forwards from the head position and stores the point it hits the ground. Then it starts a timer that waits for the time specified with waitTime The update loop continues to fire rays and calculates the distance between the first ray that hit the ground and the ray that is currently hitting the ground. If the distance exceeds headMovementRange, we start this process from the beginning.
You are such a hero! I owe you a couple of beers here, thanks a lot I will try and test it in my project :)
No problem! I'm very glad to help :) If you're satisfied, make sure to mark my answer as correct!
I still have a quick question : How can I have a smooth movement because it's a bit hard here. Can I do it without this teleport but more like a movement where I'm ai$$anonymous$$g ?
Your answer
![](https://koobas.hobune.stream/wayback/20220612101522im_/https://answers.unity.com/themes/thub/images/avi.jpg)