Ive been working on 2d movement along the XY planes, at the moment im just changing the transform.x and .y coords using a hacked version of the grid movement script which doesnt really give a smooth feel to it
Has anyone got a script or some help to achieve this with forces? (without gravity, just complete xy movement)
Ideally id like so if you press right for instance to move and let go you continue sliding (a small amount) as well as a very quick but noticable amount of acceleration before you reach the movement speed
If possible i was thinking of using a frame counter to achieve a very short boost effect that takes a few seconds to recharge again
Any help would be great guys, thanks!
http://www.re-vision1.co.uk/v2/unity/spritetest00/spritetest00.html
You can get that effect by changing the gravity in the input settings, so you don’t actually have to code anything. One possible drawback is that acceleration/deceleration are the same.
–Eric
If anyone was wondering how the camera works in the demo (it’ll probably stay the same in the full game)
Basically which ever player you have targeted (in this case the cube) an invisible object is created that moves to the middle point between the two entities, the camera follow script targets that object at run time, the zoom is set to a min value of 3 and increases depending on the distance between player and enemy
Change the gravity in the grid movement script? Unfortunately it doesnt do anything smoothly at the moment since its setting the positions of the transforms and not using forces
To use physics, you just need a rigidbody component on the object. To apply a force, you just use:-
var forceVector: Vector3 = <whatever>;
rigidbody.AddForce(forceVector);
The force vector’s direction indicates the direction the object will move and the magnitude of the vector represents the strength of the force. For something like a vehicle, you might find AddRelativeForce more convenient.
i managed to get that working the other day;
http://re-vision1.co.uk/v2/unity/spritetest01/spritetest01.html
although it doesnt seem to run with the same speed in a browser
to make it rapidly decelerate when nothing is pressed would something like this do
//if there is no input then
rigidbody.velocity = regidbody.velocity - ((rigidbody.velocity * 2) * .1);
Are you calling the physics functions from the FixedUpdate function instead of Update?
You can subtract a percentage of the speed each frame, but you might want to make it dependent on Time.deltaTime to avoid different behaviour in the browser.
I have a similar problem with my 2d platformer script. I want to have a jump mechanic with a physics feel on it without having to deal with rigidbodies much like the unity platform tutorials.
I’ve been digging through the lerpz tutorials to try and get my hands on the actual jumping code but to no avail. I see lots of “Input.GetButtonDown (“Jump”)” but nothing that seems to actually transform.translate the character controller.
For reference, my code looks like this: (It’s very simple as I have limited js experience)
private var controller : CharacterController;
private var moveDirection = Vector3.zero;
var gravity = 20.0;
var speed = 6.0;
var jumpSpeed = 3.0;
private var jumpApex: boolean = false;
private var jumping: boolean = false;
function Awake(){
controller = GetComponent (CharacterController);
}
function Update () {
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
if (controller.isGrounded) {
jumping = true;
transform.position += transform.up * jumpSpeed * Time.deltaTime;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
[/code]
Well I sort of fixed the problem I had with the choppy jump. Here’s the code that I have. It’s probably miles away from being optimized but I fixed it with limited knowledge.
The code works quite well and I’m pretty satisfied with the feel of the jump. Please let me know if there are any noob mistakes that I could change to optimize the script.
Cheers!
private var controller : CharacterController;
private var moveDirection = Vector3.zero;
private var jumpTimer = 0;
private var startingJumpPosY = 0;
//change this to affect how long the player's jump lasts
var hangTime = 0.5;
//private var jumpEnabled;
var gravity = 20.0;
var speed = 6.0;
var originalJumpSpeed: float = 15;
//how fast the player's jump is
var jumpSpeed: float = 10;
//how high the player controller is able to jump
var jumpHeight = 2;
private var jumpApex: boolean = false;
//is the player jumping?
private var jumping: boolean = false;
function Awake(){
controller = GetComponent (CharacterController);
}
function Update () {
//~ print("jumpTimer = " + jumpTimer);
//~ print("Time = " + Time.time);
//player can only move on the horizontal plane
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
//if the player is on the ground, allow jumping, switch jumping to true
if (controller.isGrounded) {
jumpTimer = hangTime + Time.time;
startingJumpPosY = transform.position.y;
jumping = true;
}
}
if ((transform.position.y <= startingJumpPosY + jumpHeight) jumping == true)
{
moveDirection.y = jumpSpeed;
print(jumpSpeed);
jumpSpeed -= Time.deltaTime;
}
if(transform.position.y >= startingJumpPosY + jumpHeight)
{
jumpSpeed = originalJumpSpeed;
jumping = false;
//print("STOP JUMPING");
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}