Quantcast
Viewing all articles
Browse latest Browse all 60

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


Viewing all articles
Browse latest Browse all 60

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>