- Home /
Clam Neck Stretch Not Satisfying Enough
I am making a game where a clam stretches its neck out super fast and super long to grab things. In order to accomplish this effect, I made an object to represent the tip of the neck and I send it moving straight upwards when the stretch begins. Along its path, I want to be able to curve it using the left and right arrow keys, allowing the player to control the trajectory in a way that feels not unlike Ness' Up-B in Smash Bros.
The neck is represented by a line traced using LineRenderer. I keep track of the many points hit along the way so that I can have the neck contract and have the tip move backwards along the path.
Here is a video of the controller in action.
As you can see, the movement is not very satisfying, nor is it very flexible (it would hard to accurately hit a target). I don't like the way that it picks up speed due to my use of AddForce but I do not know how else to push the end of the neck along. Also, The turning itself is choppy since it occurs only when I add a new point (when the position is far away enough from the last point) that I update the rotation using abar's solution for a 2D lookat equivalent.
Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;
public class Liney : MonoBehaviour
{
LineRenderer lr;
List<Vector3> points = new List<Vector3>();
Rigidbody2D rb;
Vector3 myStartPos;
Vector3 myPos;
Vector3 myLastPos;
private float speed;
void Awake()
{
lr = GetComponent<LineRenderer>();
rb = GetComponent<Rigidbody2D>();
myStartPos = new Vector3(rb.transform.position.x, rb.transform.position.y, 0);
myPos = myStartPos;
}
void UpdateMyPosition()
{
myLastPos = myPos;
myPos.Set(rb.transform.position.x, rb.transform.position.y, 0);
}
// Update is called once per frame
void Update ()
{
UpdateMyPosition();
if (Input.GetKeyDown (KeyCode.Space))
{
rb.velocity = speed * rb.transform.up;
points.Clear ();
lr.positionCount = 2;
points.Add(myPos);
points.Add(myPos);
lr.SetPositions(points.ToArray());
}
else if (Input.GetKey(KeyCode.Space))
{
if(Input.GetKey(KeyCode.LeftArrow))
rb.AddForce(-1 * rb.transform.right );
if(Input.GetKey(KeyCode.RightArrow))
rb.AddForce(1 * rb.transform.right );
if(Vector3.Distance(points.Last(), myPos) > .05f)
{
lr.positionCount++;
points.Add(myPos);
lr.SetPositions(points.ToArray());
transform.up = -1* (myLastPos - myPos);
}
else
{
lr.SetPosition(lr.positionCount - 1, myPos);
}
}
}
}
Your answer
Follow this Question
Related Questions
Making a border on a LineRenderer 0 Answers
LineRenderer max lenght 0 Answers
LineRenderer with smoot start/edge 2 Answers