Hello,
I have been working on a game which implements a spring in which you pull the ball and then release it to shoot your target. Now my problem is that whenever the ball is released from the spring, the spring goes first and after that ball follows the same path. I want the ball to go in forward direction immediately after the release giving it an actual physics spring simultation of the real world.
I simply tried to transform the position of the ball with the pull and then on release providing it with a force in forward direction of the spring.
You can find its web version in the attachment.
Hope the issue is understandable.
503108–17823–$Spring.zip (53.8 KB)
Can you post the code you are using to set up the spring and also draw the spring graphic? It looks as though the graphic doesn’t perfectly represent the current length of the spring but it is hard to say what is going wrong without seeing the code.
Thanks Andee for your reply,
Here is the code for line renderer, it works between base and the holder of the spring:
var lengthOfLineRenderer : int = 2;
private var lineRenderer : LineRenderer;
var spher : GameObject;
function Start()
{
lineRenderer = GetComponent(LineRenderer);
lineRenderer.SetVertexCount(lengthOfLineRenderer);
lineRenderer.SetWidth(1,1 );
}
function Update()
{
var posz : Vector3 = Camera.main.WorldToScreenPoint(transform.position);
lineRenderer.SetPosition(0,transform.position );
lineRenderer.SetPosition(1,spher.transform.position);
}
Here is the code for dragging and releasing the ball:
private var flag:boolean=false;
private var holder:Transform;
var ball:Transform;
var base:Transform;
private var touched:boolean=false;
function Start()
{
holder=this.transform;
ball.position=holder.position;
}
function Update ()
{
var pos1=Camera.main.WorldToScreenPoint(holder.position);
var posz : Vector3 = Camera.main.WorldToScreenPoint(holder.position);
var pos=Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y,posz.z));
var pos_base : Vector3 = Camera.main.WorldToScreenPoint(base.position);
if(Input.GetMouseButtonDown(0) Vector2.Distance(pos1,Input.mousePosition)<50)
{
flag=true;
}
if(Input.GetMouseButtonUp(0) flag)
{
ball.rigidbody.velocity=(25/Vector3.Distance(holder.position,base.position))*holder.forward;
ball.rigidbody.useGravity=true;
flag=false;
}
if(flag)
{
var p1=Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y,pos_base.z));
holder.position=pos;
ball.position=holder.position;
base.transform.LookAt(p1);
}