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 /
avatar image
0
Question by Miyahonmoto · Sep 21, 2020 at 02:42 PM · unity 5vector3ontriggerenterwaterunity 4.6

I want to change the player's ball velocity by entering transparence3D Objects

I am making unity 3D game that control stages to move a ball to hole of goal, in that game, I made transparence rectangular that when a ball enters in the rectangular, the velocity of ball slower, but when I made a program as shown below, the velocity of ball doesn't change at all. How can I solve this problem? using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class WaterPhysicsBehaviour : MonoBehaviour
 {
     public float VelocityRatioInWater = 0.5f;
 
     public Rigidbody Player;
 
     private void OnTriggerEnter(Collider other)
     {
         var CurrentVelocity = Player.velocity;
 
         if (other.tag == "Player")
         {
             CurrentVelocity = CurrentVelocity * VelocityRatioInWater;
         }
     }
 
     private void OnTriggerExit(Collider other)
     {
         var CurrentVelocity = Player.velocity;
 
         if (other.tag == "Player")
         {
             CurrentVelocity = CurrentVelocity * 1;
         }
     }
 }
 

alt text

water-scene.png (406.6 kB)
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by osuarrei · Sep 21, 2020 at 03:48 PM

CurrentVelocity is likely a copy of Player.Velocity. have you tried setting Player.Velocity?

Comment
Add comment · 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
0

Answer by unity_ek98vnTRplGj8Q · Sep 21, 2020 at 03:49 PM

You are reading the player's velocity, but never writing to it. Since velocity is just a float value, if you assign it to a variable and change that variable it won't change the original value. Change your code to

      private void OnTriggerEnter(Collider other)
      {
          var CurrentVelocity = Player.velocity;
  
          if (other.tag == "Player")
          {
              Player.velocity = CurrentVelocity * VelocityRatioInWater;
          }
      }

Or, more simply

      private void OnTriggerEnter(Collider other)
      {
  
          if (other.tag == "Player")
          {
              Player.velocity *= VelocityRatioInWater;
          }
      }

Now, I assume you want to set the velocity back to normal when you exit the cube. When this happens your velocity will be the slow value, so you can't just set it to CurrentVelocity * 1, you need to increase the speed. Try

      private void OnTriggerExit(Collider other)
      {
          var CurrentVelocity = Player.velocity;
  
          if (other.tag == "Player")
          {
              Player.velocity = CurrentVelocity * (1 / VelocityRatioInWater);
          }
      }
Comment
Add comment · Show 4 · 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 Miyahonmoto · Sep 22, 2020 at 12:32 AM 0
Share

As what you answer, I now fixed my program$$anonymous$$g, but how can I keep remain slower speed in the slower speed? I just used OntriggerStay, so I want to know other way to remain slower speed except Ontriggerstay. The way to keep slower speed in the transparent rectangular solid.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WaterPhysicsBehaviour : $$anonymous$$onoBehaviour
 {
 
 
     public float VelocityRatioInWater = 0.5f;
 
     public Rigidbody Player;
 
     private void OnTriggerEnter(Collider other)
     {
         var CurrentVelocity = Player.velocity;
 
         if (other.tag == "Player")
         {
             Player.velocity = CurrentVelocity * VelocityRatioInWater;
         }
     }
 
     private void OnTriggerStay(Collider other)
     {
         var CurrentVelocity = Player.velocity;
 
         if (other.tag == "Player")
         {
             Player.velocity = CurrentVelocity * VelocityRatioInWater;
         }
     }
 
     private void OnTriggerExit(Collider other)
     {
         var CurrentVelocity = Player.velocity;
 
         if (other.tag == "Player")
         {
             Player.velocity = CurrentVelocity * (1 / VelocityRatioInWater);
         }
     }
 }
 
avatar image unity_ek98vnTRplGj8Q Miyahonmoto · Sep 22, 2020 at 02:40 PM 0
Share

So you are actively controlling the player while he is in the water? You definitely don't want to use OnTriggerStay... can you paste you code that you use to move the player?

avatar image Miyahonmoto unity_ek98vnTRplGj8Q · Sep 22, 2020 at 02:57 PM 0
Share

I make video games that rotates the stage itself to move ball to put into the goal, therefore there is no specific code for play's ball , but I pan put script for the stages, inspector of the ball and script to control ball's behavior.

Rotating Stage

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 // Transform.rotation example.
 
 // Rotate a GameObject using a Quaternion.
 // Tilt the cube using the arrow keys. When the arrow keys are released
 // the cube will be rotated back to the center using Slerp.
 
 public class StageRotation : $$anonymous$$onoBehaviour
 {
     public float smooth = 5.0f;
     public float tiltAngle = 60.0f;
 
     void Update()
     {
         // Smoothly tilts a transform towards a target rotation.
         float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle * -1;
         float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle * -1;
 
         // Rotate the cube by converting the angles into a quaternion.
         Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);
 
         // Dampen towards the target rotation
         transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
     }
 }

Ball Behaviour

 using System.Collections;
 using System.Collections.Generic;
 using System.Threading;
 using UnityEngine;
 
 public class PlayerBallBehaviour : $$anonymous$$onoBehaviour
 {
     public Rigidbody rb;
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         if (rb.position.y < -35f)
         {
             transform.position = new Vector3(-9, 9, 5);
         }
 
         var currentYPosition = rb.transform.position.y;
         var currentVelocity = rb.velocity;
 
         if (currentYPosition > 35f) 
         {
             currentYPosition = 7f;
             currentVelocity.y = -5f;
         }
 
         rb.velocity = currentVelocity;
 
         Debug.Log(rb.velocity);
     }
 }
 

Inspector of ball

alt text alt text

ball-inspector-1.png (77.0 kB)
ball-inspector-2.png (68.4 kB)
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

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

Smooth movement for Gameobjects switching position 2 Answers

How to send Slider Information over websocket? 1 Answer

How can I enlarge the box collider when a prefab is active 1 Answer

2.5d Player Controller 0 Answers

How to fix the Chrome Web Player Problem 2 Answers


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