x


Sidescroller, rotating via mouse control

Okay, lets cut to the chase. I am making a sidescroller game where my character is controlled left and right by A and D. The player can press W to jump and has the ability to press space to slow down time. I want to add a new aspect though, where the player can shoot and aim with the mouse (just like in this video):

Here is the script for character movement and jumping so far:

var speed = 6.0;

var jumpSpeed = 8.0;

var gravity = 20.0;



private var moveDirection = Vector3.zero;

private var grounded : boolean = false;

private var lastYSpeed : float = 0;



function FixedUpdate() {

   if (grounded)

   {

      // We are grounded, so recalculate movedirection directly from axes

      moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

      moveDirection = transform.TransformDirection(moveDirection);

      moveDirection *= speed;



      if (Input.GetButton ("Jump")) {

         moveDirection.y = jumpSpeed;

      }



      // On the ground

      lastYSpeed = 0;

   }

   else

   {

      // We are in the air, maintain lastYSpeed

      moveDirection = new Vector3(Input.GetAxis("Horizontal"), lastYSpeed, Input.GetAxis("Vertical"));

      moveDirection = transform.TransformDirection(moveDirection);



      // Remove speed factor from influencing Y value

      moveDirection.x *= speed;

      moveDirection.z *= speed;

   }



   // Apply gravity

   moveDirection.y -= gravity * Time.deltaTime;

   lastYSpeed = moveDirection.y;



   // Move the controller

   var controller : CharacterController = GetComponent(CharacterController);

   var flags = controller.Move(moveDirection * Time.deltaTime);

   grounded = (flags & CollisionFlags.CollidedBelow) != 0;

}



@script RequireComponent(CharacterController)

Thank you SO MUCH in advance, really apreciate it :)

more ▼

asked May 16 '12 at 08:38 AM

tahcet gravatar image

tahcet
45 2 3

(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2168
x1370
x984
x224
x70

asked: May 16 '12 at 08:38 AM

Seen: 373 times

Last Updated: May 16 '12 at 08:38 AM