- Home /
Question by
xpath · Oct 09, 2020 at 08:56 AM ·
movelinerenderer
Move gameobject over linerenderer positions
Hi! It draws the linerenderer with the leading gameobject. I want to follow the shadow gameobject linerenderer positions from the back. How can I smooth the motion?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System;
using DG.Tweening;
public class PathCreator: MonoBehaviour
{
private LineRenderer lineRenderer;
private List<Vector3>points=new List<Vector3>();
//Joystick
public float speed;
public VariableJoystick variableJoystick;
public Rigidbody rb;
//
public GameObject player;
public GameObject shadow;
public float shadowSpeed;
//
private int index = 0;
public bool move=true;
void Awake()
{
lineRenderer=GetComponent<LineRenderer>();
}
public void FixedUpdate()
{
Vector3 direction = Vector3.forward * variableJoystick.Vertical + Vector3.right * variableJoystick.Horizontal;
// rb.AddForce(direction * speed * Time.fixedDeltaTime, ForceMode.VelocityChange);
rb.velocity=direction * speed * Time.fixedDeltaTime;
if(Input.GetMouseButtonDown(0))
points.Clear();
if(Input.GetMouseButton(0)){
// Ray ray; //=Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(player.transform.position,-player.transform.up*5, Color.blue);
RaycastHit hitinfo;
if(Physics.Raycast(player.transform.position,-player.transform.up,out hitinfo)){
if(DistanceToLastPoint(hitinfo.point)>1f){
points.Add(hitinfo.point);
lineRenderer.positionCount=points.Count;
lineRenderer.SetPositions(points.ToArray());
}
}
}
/* else if(Input.GetMouseButtonUp(0)){
}*/
if(points.Count>=3){
Move();
}
}
void Move(){
if(move){
shadow.transform.position = Vector3.MoveTowards(shadow.transform.position,
points[index],
shadowSpeed * Time.deltaTime);
if (shadow.transform.position == points[index])
{
index += 1;
points.Remove(points[index-1]);
// lineRenderer.positionCount=points.Count;
}
//if (index == points.Count-1)
//move=false;
//index =0;
}
}
private float DistanceToLastPoint(Vector3 point){
if(!points.Any())
return Mathf.Infinity;
return Vector3.Distance(points.Last(),point);
}
}
Comment
Your answer
Follow this Question
Related Questions
How to move BoxCollider2D so it covers a line renderer? 0 Answers
Move to line 0 Answers
How do I make a particle system follow a function? 1 Answer
How to configure a rope like joint 0 Answers