Hello, first of all, let u know im a begginer in all of this, and Im spanish, so sorry of my english and about my developer knowledge.
I want to create a player controller script to make the player jump in the direction i have the mouse, and depending in how much time you hold the jump key (space) the player jumps higher or lower. The lowest jump would be just by pressing once the key, then by one second more u hold u gain more force until 3 seconds.
Obviously in the world would be gravity so the player wouldnt jump in a straight line.
If someone knows how can I do this and wants to help me I would appreciate it very much, or maybe if u know a guide or a youtube video and can share here the link I would appreciate it too!
I post here a photo about the mechanic to explain it better:
Hello. I did a script so as to explain to you how you can manage this physics. I wrote just a few lines in order to help you understand the main idea, but you could improve this tiny example. Read the documentation which I put below because this is really important so as to understand how it works. In my script, you need a rigidbody2d on your sprite. Keep the left mouse button pressed to increase forceAmount value. You get to adjust values (mass, friction, forceAmount) to get the right behavior you want. Tested - it works well as expected ++
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
float forceAmount = 100; // minimum
void Start()
{ // set the framerate to 60 fps
Application.targetFrameRate = 60;
}
void Update()
{
// a simple log
Debug.Log("Force : " + forceAmount);
// an event on mouse
if (Input.GetMouseButton(0))
{ // keep the left mouse button pressed so as to increase foreAmount value
forceAmount++;
// give it a max value
if (forceAmount > 1000)
forceAmount = 1000;
}
// release left mouse button
if (Input.GetMouseButtonUp(0))
{ // mouse position
Vector3 posInScreen = Camera.main.WorldToScreenPoint(transform.position);
// so compute direction and normalize it
Vector3 dirToMouse = Input.mousePosition - posInScreen;
dirToMouse.Normalize();
// adding the force to the 2D Rigidbody according forceAmount value
GetComponent<Rigidbody2D>().AddForce(dirToMouse * forceAmount);
// reinitialize forceAmount value
forceAmount = 100;
}
}
}
Hello, thanks for the help, its all working I added an OnCollisionStay2D yo just let jump the player when is touching a wall or something. But the added force by holding the button doesnt works, its just added because its pressed once but if u hold it doesnt adds more force
So you know how to fix it?
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController2 : MonoBehaviour
{
float forceAmount = 500; // minimum
private bool canJump = false;
void Start()
{ // set the framerate to 60 fps
Application.targetFrameRate = 60;
}
void Update()
{
// a simple log
Debug.Log(“Force :” + forceAmount);
// an event on mouse
if (Input.GetKeyDown(“space”))
{ // keep the left mouse button pressed so as to increase foreAmount value
forceAmount += 500;
// give it a max value
if (forceAmount > 1500)
forceAmount = 1500;
}
// release left mouse button
if (Input.GetKeyUp(“space”) && canJump)
{ // mouse position
Vector3 posInScreen = Camera.main.WorldToScreenPoint(transform.position);
// so compute direction and normalize it
Vector3 dirToMouse = Input.mousePosition - posInScreen;
dirToMouse.Normalize();
// adding the force to the 2D Rigidbody according forceAmount value
GetComponent().AddForce(dirToMouse * forceAmount);
// reinitialize forceAmount value
forceAmount = 1000;
}
}
void OnCollisionStay2D(Collision2D obj)
{
canJump = true;
}
void OnCollisionExit2D(Collision2D obj)
{
canJump = false;
}
}
Hello. Please really please, can you format your code correctly? Respectfully it’s just annoying. When you write a message, there is a feature which is like a blank with <> symbols
Your code works - as expected. But you increase the forceAmount value by 500. So between 500 and 1500, just a few frames are enough (two exactly). If you want to manage the force - each second one degree more, I recommend you to use the framerate as a counter. Respectfully I prefer to use a Coroutine, but I am afraid that you will not understand me. Your canJump condition on the second event (button release) is not good. Put it on the button pressed only. You made a mistake - use GetKey, and not GetKeyDown (only one frame has been catched). Also I don’t understand why you want to use the spacebar key so as to manage your controller. Tested - it works ++
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
int counter = 0; // minimum
private bool canJump = false;
private int fps = 60;
void Start()
{ // set the framerate to 60 fps
Application.targetFrameRate = fps;
}
void Update()
{ // a simple log
Debug.Log("Force : " + counter + " Degree : "+ Mathf.FloorToInt(counter/fps));
// an event on mouse
if (Input.GetKey("space") && canJump)
{ // keep the left mouse button pressed so as to increase foreAmount value
counter ++;
// give it a max value
if (counter > 180)
counter = 180; // because 3*60 = 180
}
// release left mouse button
if (Input.GetKeyUp("space"))
{ // reinitialize forceAmount value at start
int forceAmount;
// compute forceAmount according counter value
if (counter > 120)
forceAmount = 3;
else if (counter > 60)
forceAmount = 2;
else
forceAmount = 1;
// mouse position
Vector3 posInScreen = Camera.main.WorldToScreenPoint(transform.position);
// so compute direction and normalize it
Vector3 dirToMouse = Input.mousePosition - posInScreen;
dirToMouse.Normalize();
// adding the force to the 2D Rigidbody according forceAmount value
GetComponent<Rigidbody2D>().AddForce(dirToMouse * 200 * forceAmount);
// reinitialize counter value
counter = 0;
}
}
void OnCollisionStay2D(Collision2D obj)
{
canJump = true;
}
void OnCollisionExit2D(Collision2D obj)
{
canJump = false;
}
}
So keep the spacebar pressed so as generate forceAmount increasing counter value :
less than one second (<60 counter) = forceAmount 1,
less than two seconds (<120 counter) = forceAmount 2,
more than two seconds (>120 counter) = forceAmount 3.