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 DragonSaige · Aug 28, 2012 at 10:19 AM · cameramovementrigidbodyswitchgameobject.active

When camera switches, rigidbody.velocity doesn't work

I'm trying to create a scene in which when the player reaches near a boat and presses 'f' , main camera switches to boat camera to simulate a "you have entered the boat" , My boat also moves according to a,w,s,d keys but the problem is whenever I press f the camera switches but the movement of boat doesn't take place.

Here is the script:

 #pragma strict
 
 var moveSpeedX:float=50;
 var moveSpeedZ:float = 80;
 private var player1:GameObject;
 var desiredDistance:float =5;
 @HideInInspector
 static var boatCam:Camera;
 @HideInInspector
 static var mainCam:Camera;
 private var guiOn:boolean;
 
 function Awake() {
 
     player1 = GameObject.FindWithTag("MainCamera");
     boatCam = GameObject.FindWithTag("Boat Cam").camera;
     mainCam = GameObject.FindWithTag("MainCamera").camera;
     mainCam.active = true;
     boatCam.active = false;
 
 }
 
 function Update () {
 //Distance Calculation
     var dist:float = Vector3.Distance(transform.position , player1.transform.position);
     if(dist<desiredDistance && Input.GetKeyDown(KeyCode.F))
     {
         //GUI On
         guiOn = true;
         
         //Camera Switching
         
         mainCam.active = false;
         boatCam.active = true;        
         
 
         // Movement
 
         var amtToMoveX = (moveSpeedX/rigidbody.mass)*Input.GetAxis("Horizontal");
         amtToMoveX*=Time.deltaTime;
         var amtToMoveZ = (moveSpeedZ/rigidbody.mass)*Input.GetAxis("Vertical");
         amtToMoveZ*=Time.deltaTime;
 
         rigidbody.velocity = Vector3(amtToMoveX,0,amtToMoveZ);
 
 
     }
     guiOn = false;
 }
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 ScroodgeM · Aug 30, 2012 at 10:34 PM

you applying movements just once, when F is pressed down. instead of this you should store current state like bool OnBoat; and set it to true once you are on boat. then in movement part check this state and apply movements to player or boat

Comment
Add comment · Show 2 · 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 DragonSaige · Aug 31, 2012 at 11:08 AM 0
Share

I did what you told,the scripts seems to be working but I also want to exit the boat when I press f again , how do I script that? I mean this 'toggle boat' system seems a bit complex.

avatar image ScroodgeM · Aug 31, 2012 at 11:14 AM 0
Share

if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
{
  if(dist < desiredDistance && !OnBoat)
  {
    ... switch to boat
  }
  else if(OnBoat && [check if we can switch off boat])
  {
    ... switch out of boat
  }
}

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

8 People are following this question.

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

Related Questions

Move RigidBody character relative to camera. 2 Answers

Movement feels like it's always going slightly off to the sides 1 Answer

Change player movement 0 Answers

Why is an object's movement jerky, when the camera is also moving by following a different rigidbody? 3 Answers

Character Moving in Elliptical Pattern 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