- Home /
How to make object jump a specific distance ? Unity 3D
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);
}
}
I'm not super familiar with the LineRenderer, so this might be incorrect, but perhaps you can get the last point on the LineRenderer via
lineRenderer.GetPosition(lineRenderer.positionCount-1)
and use that to figure out where to go next ? The only issue is that using AddForce to do that might be tricky, so perhaps using something like Transform.Lerp to lerp the objects position from the starting point to the end might work ? though a different issue with that is, that it would probably ignore physics or just end up in issues, if you're moving a Physics object by updating it's Transform.position manually ^_^"
I highly recommend looking at this talk. It's a bit math heavy but it really helped me out on my game.
https://www.google.com/search?q=game+design+jump&oq=game+design+jump&aqs=chrome..69i57j0.5203j0j7&client=ms-unknown&sourceid=chrome-mobile&ie=UTF-8#
Answer by Eno-Khaon · Jun 27, 2020 at 01:10 AM
By having a LineRenderer point from your character to a target point (i.e. where you touch on the screen), this suggests a rather hands-off approach to making the jump. With that in mind, I would suggest taking a look at a previous post I made regarding trajectory.
Specifically, I could imagine that my examples of HitTargetBySpeed()
or HitTargetByAngle()
might be the sort of thing you're looking for in this situation.
Your answer
Follow this Question
Related Questions
How to make a ball jump? 2 Answers
Jumping not always work 2 Answers
Double Jump using First Person Controller 1 Answer
In Air Movement 0 Answers
I cant make my character jump 1 Answer