How to adjust the shooting angle of an object
- by Blue
I've been trying to add an angle adjustment feature to a power bar that I got from unity3dStudents. But I can't seem to get the code right. I'm using addforce to rigidbody, it works but the power is too great. I also found that rotating the object it's shooting from changes the angle. But I don't know how to proceed from that.
Can somebody show me the problem with the script below, as in how to add height to the addforce without it going to far up or to the side? Or how to change the angle of the object?
var theAngle : int;
var maxAngle : int = 130;
var minAngle : int = 0;
var angleIncreasing : boolean = false;
var angleDecreasing : boolean = false;
var rotationSpeed : float = 10;
var ball : Rigidbody;
var spawnPos : Transform;
var shotForce : float = 25;
function Update () {
if(Input.GetKeyDown("k")){
angleIncreasing = true;
angleDecreasing = false;
}
if(Input.GetKeyUp("k")){
angleIncreasing = false;
}
if(Input.GetKeyDown("l")){
angleIncreasing = false;
angleDecreasing = true;
}
if(Input.GetKeyUp("l")){
angleDecreasing = false;
}
-------
if(angleIncreasing){
theAngle += Time.deltaTime * rotationSpeed;
if(theAngle > maxAngle){
theAngle = maxAngle;
}
}
if(angleDecreasing){
theAngle -= Time.deltaTime * rotationSpeed;
if(theAngle < minAngle){
theAngle = minAngle;
}
}
}
function Shoot(power : float, angle : int){
----
var forward : Vector3 = spawnPos.forward;
var upward : Vector3 = spawnPos.up;
pFab.AddForce(forward * power * shotForce);
pFab.AddForce(upward * angle * 10);
----
}