0
\$\begingroup\$

I'm really slow in uptaking this! I have a GameObject with a Rigidbody. Now I add via AddForce a velocity to the y-axis. But after that, I want to lerp it to zero. But how? My problem is, that I use the velocity also for steering my GameObject

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehaviour {

[Header("Movement")]
[SerializeField] private float speed;
[SerializeField] private float steeringPower;
[SerializeField] private float jumpHeigt;

[Header("Physics")]
[SerializeField] private float gravity;

private Rigidbody rigid;

void Start () {
    rigid = GetComponent<Rigidbody> (); 
}

void Update () {
    if (Input.GetKeyDown(KeyCode.UpArrow)) {
        rigid.AddForce (Vector3.up * jumpHeigt);
    } else if (Input.GetKeyDown(KeyCode.DownArrow)) {
        rigid.AddForce (Vector3.down * jumpHeigt);
    }
}

void FixedUpdate () {
    rigid.velocity = new Vector3 (Input.GetAxisRaw ("Horizontal") * steeringPower, rigid.velocity.y, speed);

    //lerp rigid.velocity.y to zero here
}
}
\$\endgroup\$
4
  • \$\begingroup\$ Are you sure you want to use Lerp? Or do you want to just decrease it over time? \$\endgroup\$
    – House
    Sep 12, 2017 at 22:01
  • \$\begingroup\$ @MichaelHouse doesnt matter p.s. thread was open as guest, now im logged in with my stackoverflow account. thats why I cannot comment. sorry for that \$\endgroup\$
    – Mirco
    Sep 12, 2017 at 22:38
  • \$\begingroup\$ meta.stackexchange.com/questions/18232/… \$\endgroup\$
    – House
    Sep 12, 2017 at 22:45
  • \$\begingroup\$ Can you explain what you're trying to do a bit more? I think MichaelHouse is really close but if you wanted gravity, you already have it in Unity by default, so I'm thinking that's not really what you want, is it? \$\endgroup\$ Sep 13, 2017 at 5:28

2 Answers 2

1
\$\begingroup\$

I'd suggest you don't do a Lerp, since that's really best for changing from one value to another over a certain amount of time. It doesn't deal with external changes simply, you'd have to manage that manually.

You essentially want gravity, or some constant force pulling down.

void FixedUpdate () {
    float yValue = rigid.velocity.y - (gravity * Time.fixedDeltaTime);

    rigid.velocity = new Vector3 (
                 Input.GetAxisRaw ("Horizontal") * steeringPower,
                 yValue,
                 speed);
}
\$\endgroup\$
0
\$\begingroup\$

If I understand correctly your question :

You basically want your player to move on the x and be able to jump(up/down) on the y, while costantly sliding along z.

Since you want to lerp y velocity to 0, and you can actually jump down, I guess you don't need gravity. Make sure to turn off gravity on your rigidbody. You might want to add some drag to it aswell, to avoid other rigidbodies to push your player too much.

Hope this can help you.

(Note: I changed the jump input block a little bit while i was testing the code, just change it back if you don't want that)

[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehaviour
{

[Header("Movement")]
[SerializeField]
private float speed;
[SerializeField]
private float steeringPower;
[SerializeField]
private float jumpHeigth;
[SerializeField]
private float jumpSpeed;

[Header("Physics")]
[SerializeField]
private float gravity;

private Rigidbody rigid;

void Start()
{
    rigid = GetComponent<Rigidbody>();
}

float jumpForce;

void Update()
{
    // Added this to make sure the player completed the current jump.
    // Remove it if you don't need it.
    if (Mathf.Abs(jumpForce)<1f)
    {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            jumpForce = jumpHeigth;
        }
        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            jumpForce = -jumpHeigth;
        }
    }

}

void FixedUpdate()
{

    Vector3 offset = new Vector3(Input.GetAxis("Horizontal") * steeringPower, jumpForce, speed) * Time.fixedDeltaTime;

    // Move the player by the offset value
    rigid.position += offset;

    // Gradually lerp jump force to 0 ...
    // (this one is not accurate, it will not cover the whole jump distance)
    // jumpForce *= .95f;

    // ...or move it contantly towards 0 
    jumpForce = Mathf.MoveTowards(jumpForce, 0f, jumpSpeed * Time.fixedDeltaTime);

}
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .