Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by SuperCrusher22 · Aug 05, 2021 at 01:16 PM · bouncetagging

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);
         }
     }
 
 }
Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rage_co · Aug 06, 2021 at 06:00 AM 0
Share

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......

avatar image SuperCrusher22 rage_co · Aug 08, 2021 at 08:56 PM 0
Share

@rage_co Where would I put the Vector3.Lerp or Vector3.MoveTowards in place of something else?

avatar image rage_co SuperCrusher22 · Aug 09, 2021 at 04:27 AM 0
Share

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....

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
 }

Comment
Add comment · Show 46 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SuperCrusher22 · Aug 12, 2021 at 01:29 AM 0
Share

This script does not really work. I get lot's of errors.

avatar image junk_rat_angel SuperCrusher22 · Aug 12, 2021 at 01:59 AM 0
Share

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?

avatar image SuperCrusher22 junk_rat_angel · Aug 12, 2021 at 02:51 AM 0
Share

Do you think you could make it into regular unity c# so I could test it out?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Want to set the fixed function of Paddle surface that if ball hits it and reflects to its opposite direction. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges