- Home /
character controll
var Speed : float = 0;//Don't touch this var MaxSpeed : float = 30;//This is the maximum speed that the object will achieve var Acceleration : float = 100;//How fast will object reach a maximum speed var Deceleration : float = 50;//How fast will object reach a speed of 0 var grounded : boolean = false; var speed : float; var gravity = 20.0;
function Update () {
if (grounded) // to check if character grounded or not
{
moveDirection = new Vector3(0, 0, Input.GetAxis("Horizontal"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
if ((Input.GetKey (KeyCode.DownArrow))&&(Speed < MaxSpeed)) //to accelerate the speed of the character and decelerate the speed
Speed = Speed - Acceleration * Time.deltaTime;
if ((Input.GetKey (KeyCode.UpArrow))&&(Speed > -MaxSpeed))
Speed = Speed + Acceleration * Time.deltaTime;
else
{
if(Speed > Deceleration * Time.deltaTime)
Speed = Speed - Deceleration * Time.deltaTime;
else if(Speed < -Deceleration * Time.deltaTime)
Speed = Speed + Deceleration * Time.deltaTime;
else
Speed = 0;
}
transform.position.z = transform.position.z + Speed * Time.deltaTime;
if(Input.GetAxis("Horizontal") < -.1 ) //left control//
{
tiltAroundZ = Input.GetAxis("Horizontal") * ( - 35.0); //tilt the character left side
target = Quaternion.Euler (0, 0, tiltAroundZ);
transform.rotation = Quaternion.Slerp(transform.rotation, target,Time.deltaTime * smooth);
transform.Rotate(Vector3.up * -0.2); //rotate to left side
transform.Translate(-2,0,0); //translate to left side
}
else
{
tiltAroundZ = Input.GetAxis("Horizontal") * ( 0); //to bring back to normal position from the tilt
target = Quaternion.Euler (0, 0, tiltAroundZ);
transform.rotation = Quaternion.Slerp(transform.rotation, target,Time.deltaTime * smooth);
}
//right//
if(Input.GetAxis("Horizontal") > .1 )
{
tiltAroundZ1 = Input.GetAxis("Horizontal") * (-35.0);//tilt the character right side
target1 = Quaternion.Euler (0, 0, tiltAroundZ1);
transform.rotation = Quaternion.Slerp(transform.rotation, target1, Time.deltaTime * smooth);
transform.Rotate(Vector3.up *0.2 ); // rotate to right side
transform.Translate(2,0,0); // translate to right side
}
else
{
tiltAroundZ1 = Input.GetAxis("Horizontal") * (0); //to bring back to normal position from the tilt
target1 = Quaternion.Euler (0, 0, tiltAroundZ1);
transform.rotation = Quaternion.Slerp(transform.rotation, target1, Time.deltaTime * smooth);
}
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
if(Input.GetKey(KeyCode.Space)) //as a brake to bring the speed to 0
{
Speed = 0;
}
}
i am making a ice ski game. above code works up to some extent.
my need is when i press uparrow the character speed should gradually increase as in a car game and should move in z direction.
when i press left arrow it should turn a certain angle(as human turns) to left and should travel the same direction it has rotated.
when i press right arrow it should turn a certain angle(as human turns) to right and should travel the same direction it has rotated.
when i press uparrow the speed should increase gradually as acceleration in car.
when user release the uparrow the character should not stop immediately.it should travel a 3 meter due slipping effect of ice and then i want to stop the character to stop.
here my problem is when i press left arrow the character turns left and travels a distance.if the character is facing x direction after turing left.but when i press uparrow it is traveling in z direction.
irrespective of where the character is facing now.so i want when i press left arrow it should travel in the same path the character is facing now.
how to re arrange or do rectification in this code or new code to get my character works as in real ice skating game.
as i am new to unity i cannot able to find the correct solution for this problem. i an searching for past 5 days
the character should move as in a ice game
thanks in advance.
i want my character movement like is this video
The first thing I'd recommend is editing your post and re-posting the code with correct formatting. (The more or less random indentation makes it hard to follow.)
Answer by AngryOldMan · Apr 06, 2011 at 03:44 PM
That's a game not a video and it's a ski game not an ice skating game. As it goes that game is horrendous. Clearly a bunch of copy and paste codes with no intention to make it act smoothly or realistically. I would advise against trying to replicate this in anyway unless you plan to make a massive improvement on it (which wouldn't be hard given it's p/$$ poor quality) I think I understand what effect your after but when playing that game I'm sort of thrown, do you want to recreate just the character movement SOLELY without the camera? or how the character is interacting with the world? Blah in future try not to use TERRIBLE games as an example as to what you want in a game lol.
I have created an ice skating effect achieved by using this script and adjusting input settings. Adjust in edit ~> project settings ~> input settings the vertical axis to have a gravity of 0.1 (more if it's too slippy or less if it's not slippy enough) and sensitivity to 1 (more if it's too slow to accelerate, less if it's too quick to accelerate) and make sure snap is unchecked.
private var charController : CharacterController; private var moveDirection : Vector3 = Vector3.zero; var TurnSpeed : float = 50; var MoveSpeed : float = 10; var gravity = 50.0;
function Start() { charController = GetComponent(CharacterController); }
function Update() { //if the player is on the ground grant movement capabilities if(charController.isGrounded) { //controls rotation transform.eulerAngles.y += Input.GetAxis("Horizontal")(Time.deltaTime*TurnSpeed); //controls direction on a local basis (not world) moveDirection = Vector3(Input.GetAxis("Vertical"),0,0); moveDirection = transform.TransformDirection(moveDirection);
} //adds gravity to controller so it stays on the ground moveDirection.y -= gravity (Time.deltaTime/2); //moves the controller charController.Move(moveDirection (Time.deltaTime MoveSpeed)); }
If there is any problems with this then let me know but all I did was put this script on a cube then added a character controller to the cube, adjusted my input settings to those values and hit play. Done
Answer by Jesse Anders · Apr 02, 2011 at 06:37 AM
Code looks much better now :)
Your question is well-formed, but it covers quite a bit of ground. If you find you're not getting the answers you need here, you might instead try solving (and asking about, if necessary) one problem at a time.
Based on your post and your code, it's not immediately clear to me what parts are working and what parts (if any) are not working, and what parts in particular you need help with. In short, I think a more specific question with narrower scope might be easier to answer.
That's just my take on it though (someone may very well come along and give you exactly the answer you need :).
I'm not sure if that was directed at me, but I didn't proofread the code or try to compile it; I just suggested that the OP re-post it with better formatting.
"it's not immediately clear to me what parts are working and what parts (if any) are not working" - I just meant to say that it doesn't even compile yet :)
actually i want the character movement in ice skating game.when i press up arrow it should move forward and when i press down arrow it should move back.i have used transform.direction to move in any direction.when i release the up arrow my character should stop after traveling a small distance that is my need .there is no need for the tilt.for that i written the code but not getting correct character movement
i just want when i use transform.direction the character will in the direction it is facing. only thing i want is when ever i release the up arrow key the character should move a constant distance(let it be 3 meter)due to slippering effect of the ice and then it should completely stop it motion. when ever i release the up arrow that is the only part not working for me
Answer by Kroltan · Apr 05, 2011 at 08:19 PM
You need to set a variable with the value transform.TransformDirection(Vector3.forward) and update it in the Update() function to get the front of the character. Then just use
controller.Move(theVarMentionedAbove*Time.deltaTime)
Answer by zmar0519 · Apr 08, 2011 at 01:12 AM
I suggest a slightly different logic. Since this is a physics based game, then it should prbably be done with rigidbodies and colliders, not character controllers. What I did when I was making a marble runway game, is modify the values of a constant force component. That way, everything is done in the physics step. Also, You can add the ice physic material to your terrain collider. One final tip, use Input.GetAxis
or Input.GetAxisRaw
for your movement code, that makes the code much cleaner. Good luck =]
Answer by ahmedrouk11 · Oct 23, 2011 at 09:32 PM
im planning of creating soccer game .im beginer in unity and this will be my first project . i have watch tutorials and read about using mocap to animate actions such as shooting passing and others and i have a limted information about scripting and i know how to make conrols for the animation in unity.but the thing that i need to know is how the characters will react with the ball because mocap only track movment a body and that good ,but how im going to mix the animations without ball with a ball??
Hi There.
Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.
You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Player slows down when hitting an incline 0 Answers
Sprite animation not functioning correctly. 0 Answers
Character Rotation point at Mouse 1 Answer
C# Movement Script 3 Answers