- Home /
mouse to control animation
hi everyone I've got a little problem with controlling the animation with the mouse frame by frame
for example
i have two animation with 30 frames
the mouse set to 0 on axis's when the mouse moves to up +1 it starting the animation all the way up to +366 on axis
up+1 axis play on frame 1 up+366 axis play on frame 30
so when u move the mouse up the mouse is controlling what frame to be on if im half way up to the highest axis point then i only want to be half way on the animation
aiming up or down animation or left or right to dodge something
plz help
thanks in advance
Answer by Antony-Blackett · Jun 09, 2011 at 08:30 PM
C# script
void Start()
{
// First set both animations layer value to something different to each other. This allows you to play them both at the same time.
animation["aim"].layer = 1;
animation["dodge"].layer = 2;
// Second set both their speed to 0.
animation["aim"].speed = 0;
animation["dodge"].speed = 0;
// Thirdly play both the animations
animation.Play("aim");
animation.Play("dodge");
}
// Use the mouse input to set the normalized time of the animations to what you want. Something like this.
public float maxMouse = 366;
void Update()
{
float newTimeAim = Mathf.Min(maxMouse, Mathf.Max(0, Input.mousePosition.y)) / maxMouse;
float newTimDodge = Mathf.Min(maxMouse, Mathf.Max(0, Input.mousePosition.x)) / maxMouse;
animation["aim"].normalizedTime = newTimeAim ;
animation["dodge"].normalizedTime = newTimDodge ;
}
And a javascript version.
function Start()
{
// First set both animations layer value to something different to each other. This allows you to play them both at the same time.
animation["aim"].layer = 1;
animation["dodge"].layer = 2;
// Second set both their speed to 0.
animation["aim"].speed = 0;
animation["dodge"].speed = 0;
// Thirdly play both the animations
animation.Play("aim");
animation.Play("dodge");
}
// Use the mouse input to set the normalized time of the animations to what you want. Something like this.
var maxMouse : float = 366;
function Update()
{
var newTimeAim : float = Mathf.Min(maxMouse, Mathf.Max(0, Input.mousePosition.y)) / maxMouse;
var newTimDodge : float = Mathf.Min(maxMouse, Mathf.Max(0, Input.mousePosition.x)) / maxMouse;
animation["aim"].normalizedTime = newTimeAim ;
animation["dodge"].normalizedTime = newTimDodge ;
}
Note that I haven't tested these... good luck!
hi thanks for the quick reply im getting this error i fix other errors
Assets/test.js(8,28): BCE0044: expecting :, found '='.
This script is C# that's why. I'll add a javascript version to my answer.
sorry, my javascript is rusty. Fixed it and edited my answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220612102124im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mouse to control animations 0 Answers
Allow Unity to render as fast as possible? 0 Answers
Animations interupting Aim-Script 0 Answers
Syncing 30FPS animations in Unity to trigger events 2 Answers
Aim 360 degrees in 2D game 0 Answers