Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by aivxx · Apr 24 at 10:30 PM · velocityswimming

If statement within FixedUpdate?

I am working with the XR Interaction Toolkit and using the Oculus Plugin. I have a Swimming script using the controller velocity to swim through the "water" but right now, I can only get it to work in FixedUpdate by itself. I want it to only be called when my player enters the water. I have tried to call it in OnCollionEnter/Stay but doesn't seem to work. Can I call an If statement for a collision in FixedUpdate? I cannot figure out how to do that. Here is my code

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.InputSystem;
 
 [RequireComponent(typeof(Rigidbody))]
 public class Swimmer : MonoBehaviour
 {
     [Header("Values")]
     [SerializeField] float swimForce = 2f;
     [SerializeField] float dragForce = 1f;
     [SerializeField] float minForce;
     [SerializeField] float minTimeBetweenStrokes;
 
     [Header("References")]
     [SerializeField] InputActionReference leftControllerSwimReference;
     [SerializeField] InputActionReference leftControllerVelocity;
     [SerializeField] InputActionReference rightControllerSwimReference;
     [SerializeField] InputActionReference rightControllerVelocity;
     [SerializeField] Transform trackingReference;
 
     Rigidbody _rigidbody;
     float _cooldownTimer;
     
 
     private void Awake()
     {
         _rigidbody = GetComponent<Rigidbody>();
         _rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
 
     }
 
     private void FixedUpdate()
     {
 
             _cooldownTimer += Time.fixedDeltaTime;
             _rigidbody.useGravity = false;
             //add velocity when both controllers pressed
             if (_cooldownTimer > minTimeBetweenStrokes
                 && leftControllerSwimReference.action.IsPressed()
                 && rightControllerSwimReference.action.IsPressed())
             {
                 var leftHandVelocity = leftControllerVelocity.action.ReadValue<Vector3>();
                 var rightHandVelocity = rightControllerVelocity.action.ReadValue<Vector3>();
                 Vector3 localVelocity = leftHandVelocity + rightHandVelocity;
                 localVelocity *= -1;
 
                 //swim forward
                 if (localVelocity.sqrMagnitude > minForce * minForce)
                 {
                     Vector3 worldVelocity = trackingReference.TransformDirection(localVelocity);
                     _rigidbody.AddForce(worldVelocity * swimForce, ForceMode.Acceleration);
                     _cooldownTimer = 0f;
                 }
 
             }
 
             //apply water drag force when player is moving
             if (_rigidbody.velocity.sqrMagnitude > 0.01f)
             {
                 _rigidbody.AddForce(-_rigidbody.velocity * dragForce, ForceMode.Acceleration);
             }
     
        
 
 
     }
 
     private void OnCollisionStay(Collision collision)
     {
           if (collision.gameObject.tag == "Water")
         {
             Debug.Log("Collision Enter");
            
         }
     }
 
 
     private void OnCollisionExit(Collision collision)
     {
         if (collision.gameObject.name == "Water")
         {
             _rigidbody.useGravity = true;
 
         }
     }
 
 
 }
 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Tsuki92 · Apr 25 at 12:16 PM

Make a "isSwimming" bool, set all of this to only be done if isSwimming is true?

Comment
Add comment · Show 1 · 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 aivxx · Apr 25 at 12:32 PM 1
Share

@Tsuki92 awesome, that worked! thank you incredibly much!

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

181 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

Related Questions

Detect if velocity equals 0? 2 Answers

Limit forward velocity of gameobject causes flying 2 Answers

Bullet shooting not working 0 Answers

How do I create Sling-Shot like propulsion? 1 Answer

MovePosition/Velocity not following a direction 0 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