Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-2
Question by robertmathew · Apr 01, 2011 at 11:38 AM · charactercontrol

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.

see this video

i want my character movement like is this video

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jesse Anders · Apr 01, 2011 at 11:59 AM 0
Share

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.)

6 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Proclyon · Apr 06, 2011 at 03:49 PM 0
Share
  • for background check

avatar image
0

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 :).

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Apr 04, 2011 at 08:20 PM 0
Share

The code doesn't compile.

avatar image Jesse Anders · Apr 05, 2011 at 11:47 AM 0
Share

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.

avatar image Statement · Apr 05, 2011 at 12:33 PM 0
Share

"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 :)

avatar image robertmathew · Apr 05, 2011 at 01:08 PM 0
Share

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

avatar image robertmathew · Apr 05, 2011 at 01:15 PM 0
Share

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

Show more comments
avatar image
0

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 =]

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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??

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Nov 09, 2012 at 06:08 AM 0
Share

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 =]

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges