- Home /
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 :)
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
click to move workig script!! but pls help with rotation!! :( 1 Answer
How do I move the player towards the direction of the camera using cinemachine? 0 Answers
simple movement and rotate help! 1 Answer
Having a first person player look at the mouse on the y axis 1 Answer