• 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
0
Question by rev_camus · Jan 06, 2010 at 02:02 AM · 2dgamecontroller

Character control in X & y

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.

Comment
Add comment
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

4 Replies

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

Answer by Ehren · Jan 06, 2010 at 03:00 AM

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.

// The horizontal speed of the keyboard controls. A higher value will // cause the object to move more rapidly. var keyboardSpeed = 20.0;

// FixedUpdate is a built-in unity function that is called every fixed framerate frame. // According to the docs, FixedUpdate should be used instead of Update when dealing with a // Rigidbody. // See http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.FixedUpdate.html // for more information. function FixedUpdate () { // This is where we move the object.

 // Get input from the keyboard, with automatic smoothing (GetAxis instead of GetAxisRaw).
 // We always want the movement to be framerate independent, so we multiply by Time.deltaTime.
 var keyboardX = Input.GetAxis("Horizontal") * keyboardSpeed * Time.deltaTime;
 var keyboardY = Input.GetAxis("Vertical") * keyboardSpeed * Time.deltaTime;

 // Calculate the new position based on the above input.
 // If you want to limit the movement, you can use Mathf.Clamp
 // to limit the allowed range of newPos.x or newPos.y.
 var newPos = rigidbody.position + Vector3(keyboardX, keyboardY, 0.0);

 // Move the object.
 rigidbody.MovePosition(newPos);

}

// Require a Rigidbody component to be attached to the same GameObject. @script RequireComponent(Rigidbody)

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 rev_camus 1 · Jan 06, 2010 at 06:52 PM

This is a very good staring point, thank you Ehren, hopefully I'll be able to grasp some of it.

Comment
Add comment · Show 3 · 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 rev_camus 1 · Jan 06, 2010 at 11:16 PM 0
Share

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 :(

avatar image rev_camus · Jan 07, 2010 at 02:50 AM 0
Share

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.

avatar image Ehren · Jan 07, 2010 at 08:03 PM 0
Share

One suggestion: you might want to delete your two answers since they're comments, not answers.

avatar image
0

Answer by rev_camus 1 · Jan 07, 2010 at 06:38 PM

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 inAirControlAcceleration = 1.0;

// The gravity for the character var gravity = 60.0; var maxFallSpeed = 20.0;

// How fast does the character change speeds? Higher is faster. var speedSmoothing = 5.0;

// This controls how fast the graphics of the character "turn around" when the player turns around using the controls. var rotationSmoothing = 10.0;

// The current move direction in x-y. This will always been (1,0,0) or (-1,0,0) // The next line, @System.NonSerialized , tells Unity to not serialize the variable or show it in the inspector view. Very handy for organization! @System.NonSerialized var direction = Vector3.zero;

// The current vertical speed @System.NonSerialized var verticalSpeed = 0.0;

// The current movement speed. This gets smoothed by speedSmoothing. @System.NonSerialized var speed = 0.0;

// Is the user pressing the left or right movement keys? @System.NonSerialized var isMoving = false;

// The last collision flags returned from controller.Move @System.NonSerialized var collisionFlags : CollisionFlags;

// We will keep track of an approximation of the character's current velocity, so that we return it from GetVelocity () for our camera to use for prediction. @System.NonSerialized var velocity : Vector3;

// This keeps track of our current velocity while we're not grounded? @System.NonSerialized var inAirVelocity = Vector3.zero;

// This will keep track of how long we have we been in the air (not grounded) @System.NonSerialized var hangTime = 0.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;

// How high do we jump when pressing jump and letting go immediately var height = 1.0; // We add extraHeight units (meters) on top when holding the button down longer while jumping var extraHeight = 4.1;

// This prevents inordinarily too quick jumping // The next line, @System.NonSerialized , tells Unity to not serialize the variable or show it in the inspector view. Very handy for organization! @System.NonSerialized var repeatTime = 0.05;

@System.NonSerialized var timeout = 0.15;

// Are we jumping? (Initiated with jump button and not grounded yet) @System.NonSerialized var jumping = false;

@System.NonSerialized var reachedApex = false;

// Last time the jump button was clicked down @System.NonSerialized var lastButtonTime = -10.0;

// Last time we performed a jump @System.NonSerialized var lastTime = -1.0;

// the height we jumped from (Used to determine for how long to apply extra jump power after jumping.) @System.NonSerialized var lastStartHeight = 0.0;

}

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;

// reset the character's position to the spawnPoint
transform.position = spawnPoint.position;

}

function OnDeath () { Spawn (); }

function UpdateSmoothedMovementDirection () {
var h = Input.GetAxisRaw ("Horizontal");

if (!canControl) h = 0.0;

movement.isMoving = Mathf.Abs (h) > 0.1;

if (movement.isMoving) movement.direction = Vector3 (h, 0, 0);

// Grounded controls if (controller.isGrounded) { // Smooth the speed based on the current target direction var curSmooth = movement.speedSmoothing * Time.deltaTime;

 // Choose target speed
 var targetSpeed = Mathf.Min (Mathf.Abs(h), 1.0);

 // Pick speed modifier
 if (Input.GetButton ("Fire2") && canControl)
     targetSpeed *= movement.runSpeed;
 else
     targetSpeed *= movement.walkSpeed;

 movement.speed = Mathf.Lerp (movement.speed, targetSpeed, curSmooth);

 movement.hangTime = 0.0;

} else { // In air controls movement.hangTime += Time.deltaTime; if (movement.isMoving) movement.inAirVelocity += Vector3 (Mathf.Sign(h), 0, 0) Time.deltaTime movement.inAirControlAcceleration; }

}

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;

if (controller.isGrounded) {
    // Jump
    // - Only when pressing the button down
    // - With a timeout so you can press the button slightly before landing        
    if (jump.enabled && Time.time < jump.lastButtonTime + jump.timeout) {
        movement.verticalSpeed = CalculateJumpVerticalSpeed (jump.height);
        movement.inAirVelocity = lastPlatformVelocity;
        SendMessage ("DidJump", SendMessageOptions.DontRequireReceiver);
    }
}

}

function ApplyGravity () { // Apply gravity var jumpButton = Input.GetButton ("Jump");

if (!canControl) jumpButton = false;

// When we reach the apex of the jump we send out a message if (jump.jumping && !jump.reachedApex && movement.verticalSpeed <= 0.0) { jump.reachedApex = true; SendMessage ("DidJumpReachApex", SendMessageOptions.DontRequireReceiver); }

// * When jumping up we don't apply gravity for some time when the user is holding the jump button // This gives more control over jump height by pressing the button longer var extraPowerJump = jump.jumping && movement.verticalSpeed > 0.0 && jumpButton && transform.position.y < jump.lastStartHeight + jump.extraHeight && !IsTouchingCeiling ();

if (extraPowerJump) return; else if (controller.isGrounded) movement.verticalSpeed = -movement.gravity Time.deltaTime; else movement.verticalSpeed -= movement.gravity Time.deltaTime;

// Make sure we don't fall any faster than maxFallSpeed. This gives our character a terminal velocity. movement.verticalSpeed = Mathf.Max (movement.verticalSpeed, -movement.maxFallSpeed);

}

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;

// By comparing the previous value of areEmittersOn to the new one, we will only update the particle emitters when needed
if (wereEmittersOn != areEmittersOn) {
    for (var emitter in GetComponentsInChildren (ParticleEmitter)) {
        emitter.emit = areEmittersOn;
    }
}

}

function Update () { if (Input.GetButtonDown ("Jump") && canControl) { jump.lastButtonTime = Time.time; }

UpdateSmoothedMovementDirection();

// Apply gravity // - extra power jump modifies gravity ApplyGravity ();

// Apply jumping logic ApplyJumping ();

// Moving platform support if (activePlatform != null) { var newGlobalPlatformPoint = activePlatform.TransformPoint(activeLocalPlatformPoint); var moveDistance = (newGlobalPlatformPoint - activeGlobalPlatformPoint); transform.position = transform.position + moveDistance; lastPlatformVelocity = (newGlobalPlatformPoint - activeGlobalPlatformPoint) / Time.deltaTime; } else { lastPlatformVelocity = Vector3.zero;
}

activePlatform = null;

// Save lastPosition for velocity calculation. lastPosition = transform.position;

// Calculate actual motion var currentMovementOffset = movement.direction * movement.speed + Vector3 (0, movement.verticalSpeed, 0) + movement.inAirVelocity;

// We always want the movement to be framerate independent. Multiplying by Time.deltaTime does this. currentMovementOffset *= Time.deltaTime;

// Move our character! movement.collisionFlags = controller.Move (currentMovementOffset);

// Calculate the velocity based on the current and previous position.
// This means our velocity will only be the amount the character actually moved as a result of collisions. movement.velocity = (transform.position - lastPosition) / Time.deltaTime;

// Moving platforms support if (activePlatform != null) { activeGlobalPlatformPoint = transform.position; activeLocalPlatformPoint = activePlatform.InverseTransformPoint (transform.position); }

// Set rotation to the move direction
if (movement.direction.sqrMagnitude > 0.01) transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement.direction), Time.deltaTime * movement.rotationSmoothing);

// We are in jump mode but just became grounded if (controller.isGrounded) { movement.inAirVelocity = Vector3.zero; if (jump.jumping) { jump.jumping = false; SendMessage ("DidLand", SendMessageOptions.DontRequireReceiver);

     var jumpMoveDirection = movement.direction * movement.speed + movement.inAirVelocity;
     if (jumpMoveDirection.sqrMagnitude &gt; 0.01)
         movement.direction = jumpMoveDirection.normalized;
 }

}

// Update special effects like rocket pack particle effects UpdateEffects ();

}

function OnControllerColliderHit (hit : ControllerColliderHit) { if (hit.moveDirection.y > 0.01) return;

// Make sure we are really standing on a straight platform
// Not on the underside of one and not falling down from it either!
if (hit.moveDirection.y < -0.9 && hit.normal.y > 0.9) {
    activePlatform = hit.collider.transform;    
}

}

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

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 straydogstrut · Feb 21, 2010 at 10:28 PM

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?

var speed = 10.0;

function Update () {

// Get the horizontal and vertical axis. // By default they are mapped to the arrow keys. // The value is in the range -1 to 1

var htranslation = Input.GetAxis ("Horizontal") speed; var vtranslation = Input.GetAxis("Vertical") speed;

// Make it move 10 meters per second instead of 10 meters per frame... htranslation = Time.deltaTime; vtranslation = Time.deltaTime;

// Move translation along the object's z-axis transform.Translate (0, 0, htranslation); transform.Translate (0, vtranslation, 0); }

Note that pressing multiple keys works but your sub won't turn it's heading to this direction. However, I would assume just using transform.Rotate would allow you to do this?

Hope this helps.

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 rev_camus 1 · Feb 22, 2010 at 04:32 PM 0
Share

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!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

2D Animation does not start 1 Answer

Set Shader Rendering Mode to Fade via Code 1 Answer

[c#]collision script not working 1 Answer

2D Array's 1 Answer


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