Question by
Zulerf · Apr 28, 2018 at 10:07 AM ·
movementmovement scriptclick to move
Click to move player flickering when trying to get past object,Click to Move player, flickering when colliding with other objects.
My player character is flickering/stuttering when clicking on a wall to get to the other side, this happens when the place I click is unreachable by the player. how would I make it so the player stops all movement if they click on an unreachable space, or if you think there is a better way to fix the problem. I am new to Unity so all feedback and help are appreciated. Thanks.
using UnityEngine;
using System.Collections;
public class PlayerInteraction : MonoBehaviour
{
private float speed = 10;
private Vector3 targetPosition;
private bool isMoving;
const int LEFT_MOUSE_BUTTON = 0;
private void Start()
{
targetPosition = transform.position;
isMoving = false;
}
void LateUpdate()
{
if (Input.GetMouseButton(LEFT_MOUSE_BUTTON))
{
SetTargetPosition();
}
if (isMoving)
{
MovePlayer();
}
}
void SetTargetPosition()
{
Plane plane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float point = 0f;
if (plane.Raycast(ray, out point))
{
targetPosition = ray.GetPoint(point);
}
isMoving = true;
}
void MovePlayer()
{
var adjusted = targetPosition;
adjusted.y = 200f;
transform.LookAt(adjusted);
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
if (transform.position == targetPosition)
{
isMoving = false;
}
}
void OnCollisionEnter(Collision collisionInfo)
{
var stop = transform.localPosition;
if (collisionInfo.collider.name != "WorldFloorFlat")
{
isMoving = false;
}
}
}
Comment
By stuttering do you mean continuously as it tries to move through a wall or do you mean it stutters just once as it hits the wall?