Why is this happening when I run my script?
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);
}
}
}
On trigger enter runs only once, so instead of setting the position at once, use Vector3.MoveTowards or Vector3.Lerp in the update or a similar repeating coroutine method......
@rage_co Where would I put the Vector3.Lerp or Vector3.MoveTowards in place of something else?
You have to put it in the Update method...(or a coroutine with a loop which i think is an unnecessary complication in your case).....
public float newY;
public float speed;
void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag == "jumpPad")
{
//calculate what the new Y position will be
newY = Mathf.Sin(Time.time * speed) * height + transform.position.y;
}
}
void Update()
{
Vector3 target = new Vector3(transform.position.x, newY, transform.position.z);
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
you will need to define newY as a public variable plus a speed variable...and you can then tweak the speed in the inspector to control how fast it goes....remember that MoveTowards provides a constant speed, if you don't like that, you can swap Vector3.MoveTowards for Vector3.Lerp safely....
Answer by junk_rat_angel · Aug 10, 2021 at 07:16 AM
sin should only start at 0 if your time variable starts at 0 at the start of a jump, which Time.time isn't going to do. You want a variable which is reset to start at 0 every on every collision. Pretty sure you probably also want to put all the code from the if statement into an independent function and trigger a state change that initiates the jump and cause the function to be called as long as its occuring because I dont think your position change will be called enough(if more than once, at the "moment" of collision) to simulate an arc. You can handle the state and timekeeping with the same variable if you set it to -1 to represent false.
Also you might wanna know when the arc ends now that you have a jumpState so you can reset it. I gave an example of a simple conditional but you probably want something more complex since I assume the jump is supposed to end early if theres an obstacle and you may need to clean up the position once the condition is reached since getting the exact value of the sin wave for the end of the jump may not be reliably doable.
Lastly im guessing you wanted an arc like the first half of a sine wave, which means you want to add the value the sin wave returns to the initial position not the current position so that as the values approach 0 you return to your initial y position(unless you meant to use the negative half of the wave period as well)
so something like
if(jumpPad){
jumpState = 0;
initialY = transform.position.y
}
void Update(){
if (jumpState>-1){
calcJump(jumpState)
jumpState ++;
}
calcJump(float jumpState){
if(jumpState>0 and Sin(jumpState * Speed)<= 0){
jumpState ==-1;
}
else {
float newY = Mathf.Sin(jumpState * speed) * height + initialY;
transform.position = new Vector3(transform.position.x, newY, transform.position.z);
}
This script does not really work. I get lot's of errors.
well that was actually supposed to be pseudo-code. the speed variable is capitalized inconsistently, you need an extra bracket to close the Update function before the declaring the calcJump function which needs a type declaration (i.e. void calcJump(float jumpState), the sin function doesnt have the mathf in the conditional and I switched
void OnTriggerEnter(Collider collision) { if (collision.gameObject.tag == "jumpPad") {
for the if(jumpPad) shorthand written there. you accounted for all that? what are the errors?
Do you think you could make it into regular unity c# so I could test it out?
Your answer
Follow this Question
Related Questions
bouncy collision not working! Thank you 0 Answers
C# Loop script,C# LOOP SCRIPT HELP NEEDED! 0 Answers
Usage of multiple tags or some other workaround? 1 Answer
Spotlight Bouncing Problem 0 Answers