|
Hi. I very stupid probably, no talent for scripting at all. in return I'm a pretty good artist/producer and somewhat technically orientated. Anyway I'm in the first phase of my project dev, It's a side scroller and I need to move my player (a little sub) up down left right, and angles up down pressing 2 keys. I need to have this working so that I can build my levels and run through them. Later I will draft in a coder, but for now I just need to concentrate my efforts on building my assets. So If someone up there could help me i would greatly appreciate. thanks.
(comments are locked)
|
|
The PlatformerController is probably not the best option since your sub is not being held to the ground by gravity. Try attaching the following script to your sub. Also make sure your sub has a Rigidbody component on it, that the Rigidbody's "Is Kinematic" field is checked, and that any other control scripts (such as PlatformerController) have been removed. EDIT: Here's a sample project showing how to use this script.
(comments are locked)
|
|
This is a very good staring point, thank you Ehren, hopefully I'll be able to grasp some of it. Unfortunately the tutorial stops before he gets to explain the movement. I'm having a really hard time making sense of the script alone. Plus there is no other movement than on the Y axis :(
Jan 06 at 11:16 PM
rev_camus 1
I would really appreciate if someone could write me a script to attach to my little sub so that i can move around and proceed to build my levels.
Jan 07 at 02:50 AM
rev_camus
One suggestion: you might want to delete your two answers since they're comments, not answers.
Jan 07 at 08:03 PM
Ehren
(comments are locked)
|
|
So today I've manage to move my little sub on the X axis using the PlatformerControler script from the 2d platform demo. the problem is how to move on the Y axis please help! VIDEO OF THE SUB MOVING here is the script: // Does this script currently respond to Input? var canControl = true; // The character will spawn at spawnPoint's position when needed. This could be changed via a script at runtime to implement, e.g. waypoints/savepoints. var spawnPoint : Transform; class PlatformerControllerMovement { // The speed when walking var walkSpeed = 3.0; // when pressing "Fire1" button (control) we start running var runSpeed = 10.0;
} var movement : PlatformerControllerMovement; // We will contain all the jumping related variables in one helper class for clarity. class PlatformerControllerJumping { // Can the character jump? var enabled = true;
} var jump : PlatformerControllerJumping; private var controller : CharacterController; // Moving platform support. private var activePlatform : Transform; private var activeLocalPlatformPoint : Vector3; private var activeGlobalPlatformPoint : Vector3; private var lastPlatformVelocity : Vector3; // This is used to keep track of special effects in UpdateEffects (); private var areEmittersOn = false; function Awake () { movement.direction = transform.TransformDirection (Vector3.forward); controller = GetComponent (CharacterController); Spawn (); } function Spawn () { // reset the character's speed movement.verticalSpeed = 0.0; movement.speed = 0.0;
} function OnDeath () { Spawn (); } function UpdateSmoothedMovementDirection () {
} function FixedUpdate () { // Make sure we are absolutely always in the 2D plane. transform.position.z = 0; } function ApplyJumping () { // Prevent jumping too fast after each other if (jump.lastTime + jump.repeatTime > Time.time) return;
} function ApplyGravity () { // Apply gravity var jumpButton = Input.GetButton ("Jump");
} function CalculateJumpVerticalSpeed (targetJumpHeight : float) { // From the jump height and gravity we deduce the upwards speed // for the character to reach at the apex. return Mathf.Sqrt (2 * targetJumpHeight * movement.gravity); } function DidJump () { jump.jumping = true; jump.reachedApex = false; jump.lastTime = Time.time; jump.lastStartHeight = transform.position.y; jump.lastButtonTime = -10; } function UpdateEffects () { wereEmittersOn = areEmittersOn; areEmittersOn = jump.jumping && movement.verticalSpeed > 0.0;
} function Update () { if (Input.GetButtonDown ("Jump") && canControl) { jump.lastButtonTime = Time.time; }
} function OnControllerColliderHit (hit : ControllerColliderHit) { if (hit.moveDirection.y > 0.01) return;
} // Various helper functions below: function GetSpeed () { return movement.speed; } function GetVelocity () { return movement.velocity; } function IsMoving () { return movement.isMoving; } function IsJumping () { return jump.jumping; } function IsTouchingCeiling () { return (movement.collisionFlags & CollisionFlags.CollidedAbove) != 0; } function GetDirection () { return movement.direction; } function GetHangTime() { return movement.hangTime; } function Reset () { gameObject.tag = "Player"; } function SetControllable (controllable : boolean) { canControl = controllable; } // Require a character controller to be attached to the same game object @script RequireComponent (CharacterController) @script AddComponentMenu ("2D Platformer/Platformer Controller")
(comments are locked)
|
|
I'm very new to Unity myself so bear in mind my suggestion will have all the hallmarks of a newbie, including all the bad coding habits that go with it! Anyway, it seems to me that the answer should be a lot more straightforward than trying to shoehorn the platformer script in. Our first lesson in Unity started with a simple movement script (forward, back, rotate left, rotate right) for a driving a cube around. I've modified it for 2D movement on the x-y plane. It might be a complete miss, but maybe it will point you in the right direction?
Note that pressing multiple keys works but your sub won't turn it's heading to this direction. However, I would assume just using Hope this helps. Thank you Straydogstrut, I'm currently working on new project with a coder, but when I have a bit of time I will try it!
Feb 22 at 04:32 PM
rev_camus 1
(comments are locked)
|
Unity Answers has moved to a new system, and some users may have trouble logging in.