- Home /
Cannot control gameobject and play animation simultaneously
Hey there! I've been trying to fix this problem for a little under a week now, and have no idea what i'm doing wrong:
I have this turret that you control with the mouse. I want it to play a recoil animation on the nozzles the same time it fires a projectile. However, I can't get this to work. Here are some things that happen.
If I had the turret rotated, it would jerk back to the position it was animated in once I fired. And I can't control the turret while firing.
when I tried to make a new animation in Unity, It transported the entire turret to 0,0,0. And it didn't animate the nozzle, it animated the entire turret gameobject.
Here is the animation script:
#pragma strict
var projectile : GameObject;//ignore
internal var nextFire :float;
var fireRate : float = 0.5;
var clip1 : AnimationClip;
var clip2 : AnimationClip;
function Start () {
}
function Update () {
if (Input.GetButton ("Fire1")&& Time.time > nextFire) {
animation.Play(clip1.name);
animation.Play(clip2.name);
nextFire = Time.time + fireRate;
}
}
And the control script(sorry about the mess):
//#pragma strict
/*var forwardSpeed : float = 3;//ignore
var roll : float = 2;//ignore
var lift : float = 25; */
var yAxis : float = 2;
var xAxis : float = 2;
/*var deadZoneXPlus : float = 5;
var deadZoneXMinus : float = -5;
var deadZoneYPlus : float = 5;
var deadZoneYMinus : float = -5;
var bullet : GameObject;
var speed : float; */
function Start() {
}
function Update ()
{
//forward speed
//var forward = Input.GetAxis("Mouse Y")*forwardSpeed;
//turn roll
// var rolly = Input.GetAxis("Mouse X")*roll;
//rotate Buggy
//var lifty = Input.GetAxis("Jump")*lift;
//Lift dropship
var y = Input.GetAxis("Mouse Y")*yAxis;
//yaw
var x = Input.GetAxis("Mouse X")*xAxis;
//roll
//transform.Rotate(0,rolly, 0);
//if ( Input.GetAxis ("Mouse X") > deadZoneXPlus || Input.GetAxis("Mouse X") < deadZoneXMinus)
//{
//pitch
transform.Rotate(0,y,0);
//}
//if (Input.GetAxis ("Mouse Y") > deadZoneYPlus || Input.GetAxis ("Mouse Y") < deadZoneYMinus) {
//yaw
transform.Rotate(x,0,0);
//}
//move forward
//rigidbody.AddRelativeForce(0,forward,0);
//lift
//rigidbody.AddRelativeForce(0,0,lifty);
/*if (Input.GetButton("fire1")){
var clone : GameObject = Instantiate(bullet, transform.position, transform.rotation);
clone.transform.TransformDirection(Vector3(speed,0,0));
*/
}
}
Your animation probably animates position and rotation of the same objects you are transfor$$anonymous$$g. Therefore overwriting your transformations. If you want to negate this, put positioning in LateUpdate
, which is after animations have been appied
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Controlling animation with mouse 0 Answers
Can I make animations snap to a frame? 1 Answer
Zoom in/out with right button of mouse 1 Answer
Space Flying Script Using Mouse 1 Answer