I’m a complete beginner so this might be simple. How can I make my player to jump to the end point of LineRenderer. So far I managed to make my player jump to the same direction that my LineRenderer points, but I want it to land at the end of the line.
I have this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jump : MonoBehaviour
{
public Rigidbody rb;
public float height;
public Vector3 pos1;
public Vector3 pos2;
public LineRenderer i0;
public LineRenderer i1;
public float power;
void Start()
{
pos1 = i1.GetPosition(1);
pos2 = i0.GetPosition(0);
}
void FixedUpdate()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began && IsGrounded())
{
OnTouchJump(power);
}
}
public void OnTouchJump(float power)
{
Vector3 forceBB = new Vector3(0, 0, 0);
Vector3 dir = pos2 - pos1;
Vector3 n = pos1 + (dir / 2);
n = n + new Vector3(0, height, 0);
forceBB = n - pos1;
rb.AddForce(forceBB * power);
}
private bool IsGrounded()
{
return Physics.Raycast(transform.position, Vector3.down, 0.5f);
}
}