My script here is supposed to make an object bounce when it touches a touchpad, but I did not want to use rigidbody, so I used this instead. But when running the script, instead of bouncing it teleports. This is the script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bounce : MonoBehaviour
{
//adjust this to change speed
[SerializeField]
float speed = 5f;
//adjust this to change how high it goes
[SerializeField]
float height = 0.5f;
Vector3 pos;
private void Start()
{
pos = transform.position;
}
void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag == "jumpPad")
{
//calculate what the new Y position will be
float newY = Mathf.Sin(Time.time * speed) * height + pos.y;
//set the object's Y to the new calculated Y
transform.position = new Vector3(transform.position.x, newY, transform.position.z);
}
}
}
↧