- Home /
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;
}
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
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.
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
Follow this Question
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
Character Moving in Elliptical Pattern 0 Answers