Can anyone tell me how i would add thrust to my right mouse click.
var playerPrefab:Transform;
var player:Transform;
var thrust = 20.0;
private var moveDirection = Vector3.zero;
function FixedUpdate() {
//Thrust on Right Mouse Button
//if(Input.GetMouseButtonDown(1).ToString())
if(Input.GetButtonDown("Fire2"))
{
//moveDirection.player.AddForce(transform.forward * thrust);
//playerPrefab.AddForce(transform.forward * thrust);
//moveDirection = new Vector3(0, 0, Input.GetMouseButtonDown(1).ToString("Vertical"));
//Speed at which we move
//moveDirection *= thrust;
}
}
As you can see i have tried various ways
Thanks for any help.
Thanks i tried that and it doesn’t seem to work…? i have also added rigidbody component and it still don’t work.
Here’s my full script
var speed = 6.0;
var bulletPrefab:Transform;
var bulletspeed = 1000;
var aimPrefab:Transform;
var player:Transform;
var thrust = 20.0;
private var moveDirection = Vector3.zero;
function FixedUpdate() {
//Move in 3D space
//moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
//moveDirection = transform.TransformDirection(moveDirection);
//Thrust on Spacebar
//if (Input.GetButton ("Jump")) {
//moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
//moveDirection *= thrust;
//}
//Thrust on Right Mouse Buton
//if(Input.GetMouseButtonDown(1).ToString())
if(Input.GetButtonDown("Fire2"))
{
rigidbody.AddForce(transform.forward * thrust );
//moveDirection.player.AddForce(transform.forward * thrust);
//moveDirection = new Vector3(0, 0, Input.GetMouseButtonDown(1).ToString("Vertical"));
//Speed at which we move
//moveDirection *= speed;
}
//Move Left And Right, Up and Down
moveDirection = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
//Speed at which we move
//moveDirection *= speed;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
//Check to see if LEFT MOUSE button is pressed then fire
//if(Input.GetMouseButtonDown(0).ToString())
if(Input.GetButtonDown("Fire1"))
{
//var bullet = Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position,Quaternion.identity);
var bullet = Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position,Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward * bulletspeed);
}
}
@script RequireComponent(CharacterController)
Insert a Debug.Log(“Rightmouse”) before you add the force to the rigidbody. If the console shows the output of “Rightmouse”, it’s a problem with your setup, and not the script itself.
Note that entity that contains the script MUST contain the rigid body, AND the Character Controller script.
Try cranking your thrust up to 2000, or something insanely high like that.
Also, try editing the input settings to see if your keys are configured right…
Other than that… Show me a screenshot of the entity that this belongs to, in the hierarchy, all nodes expanded, inspector focused on the object with the controller script, and all components expanded and all fields expanded.
My understanding is that you wouldn’t typically want to attach both a rigidbody and a character controller to the same game object. Having never tried it myself I can’t say for sure what the results of doing so would be, but I wouldn’t be surprised if it didn’t work as expected.
I’m not sure what you’re trying to accomplish by using both here, but I would try just using one or the other (whichever is more appropriate for the type of motion you want to achieve), and see if that fixes the problem.
The character controller is basically kinematic. Using it with physics forces is probably not going work well because each frame it is going update its position based on the input provided to it. It’s OK to use a rigidbody and a character controller together, if you want to toggle them for explosions. Otherwise, it should probably be one or the other.
On a more topic-specific note, you should probably write your own movement script which is appropriate for your game.