I’m working on a top-down sport-like game prototype and used a control scheme from the documentation, which looks like this (this is my character’s control script):
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("HorizontalP1"), 0,
Input.GetAxis("VerticalP1"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
I would like to add on to this, so it includes instantiating a rigidbody traveling at a speed I choose, but I don’t know how exactly to direct the instantiated object in, basically the velocity, that the character is moving.
Here is what I have so far for instantiating the projectile (the “canfire” is an indicator that you can fire one projectile):
var projectile : Rigidbody;
var canfire : boolean = true;
var ballspeed : int = 25;
if (Input.GetButtonDown("Fire1")) {
// Instantiate the projectile at the position and rotation of this transform
clone = Instantiate(projectile, transform.position, transform.rotation);
Physics.IgnoreCollision(clone.collider, collider);
clone.velocity = transform.TransformDirection (-ballspeed,0,-ballspeed);
canfire = false;
}
}
In short, I want to instantiate a projecticle in the direction my character is traveling, at an increased rate.
I feel like there’s a fairly simple way to send the projectile in the direction the character is moving, any ideas anyone? Thanks ahead of time!
Also, am working w/Unityscript/JS.
Pat