How to implement such control as on video? Game: rolly vortex
Video - https://youtu.be/sShnjs6D59M
How to achieve such control? What should I use? help me please
My code (edited):
public class TouchControl : MonoBehaviour {
float level_width = 1f;
float speed = 0.01f;
void Update()
{
if(Input.touchCount > 0)
{
Debug.Log("Result = " + ((Input.GetTouch(0).position.x / Screen.width) - 0.5f) * 2);
if(((Input.GetTouch(0).position.x / Screen.width) - 0.5f) * 2 > 0) {
Debug.Log(((Input.GetTouch(0).position.x + 0.5f) / 2) + " result");
MoveLeft();
}
if (((Input.GetTouch(0).position.x / Screen.width) - 0.5f) * 2 < 0)
{
Debug.Log(((Input.GetTouch(0).position.x + 0.5f) / 2) + " result");
MoveRight();
}
}
}
void MoveLeft()
{
Vector3 newPos = transform.position;
newPos.x = (Input.GetTouch(0).position.x * level_width) * - 1;
transform.position = Vector3.Lerp(transform.position, newPos, speed * Time.deltaTime);
}
void MoveRight()
{
Vector3 newPos = transform.position;
newPos.x = Input.GetTouch(0).position.x * level_width;
transform.position = Vector3.Lerp(transform.position, newPos, speed * Time.deltaTime);
}
}
Something like this should get you started:
public float level_width = 5f;
public float speed = 0.1f; // 0..1
if(Input.touchCount > 0)
{
Vector3 newPos = transform.position;
newPos.x = Input.GetTouch(0).position.x * width;
transform.position = Vector3.Lerp(transform.position, newPos, speed * Time.deltaTime)
}
speed will adjust the time it takes for the ball to reach the touch position.
level_width helps to scale the input from screen coordinates to your world/level.
Edit: I simplified your code a bit:
public class TouchControl: MonoBehaviour {
float level_width = 10f;
float speed = 0.01 f;
float target_xpos = 0f;
void Update() {
if (Input.touchCount > 0) {
target_xpos = ((Input.GetTouch(0).position.x / Screen.width) - 0.5 f) * 2 * level_width;
MoveToFinger();
}
}
void MoveToFinger() {
Vector3 newPos = transform.position;
newPos.x = target_xpos;
transform.position = Vector3.Lerp(transform.position, newPos, speed * Time.deltaTime);
}
}
Related
So, apperently I am trying to make a game using unity.
So far so good.
But there is one problem: Everytime I crouch or slide, I cant move anymore until I stand up again.
I ve tried everything.
I ve even tried removing the sliding completely or writing a whole new code, yet nothing works.
Sorry for the long code, but I dont know anymore ;-;
using System;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
//Assingables
public Transform playerCam;
public Transform orientation;
//Other
private Rigidbody rb;
//Rotation and look
private float xRotation;
private float sensitivity = 50f;
public float sensMultiplier = 1f;
//Movement
public float moveSpeed = 4500;
public float maxSpeed = 10;
public bool grounded;
public LayerMask whatIsGround;
public float counterMovement = 0.175f;
private float threshold = 0.01f;
public float maxSlopeAngle = 35f;
public float sprintspeed = 20f;
public float speedOrigin = 10f;
//Crouch & Slide
private Vector3 crouchScale = new Vector3(1, 0.5f, 1);
private Vector3 playerScale;
public float slideForce = 0;
public float slideCounterMovement = 0.2f;
//Jumping
private bool readyToJump = true;
private float jumpCooldown = 0.25f;
public float jumpForce = 300f;
//Input
float x, y;
bool jumping, sprinting, crouching;
//Sliding
private Vector3 normalVector = Vector3.up;
private Vector3 wallNormalVector;
void Awake() {
rb = GetComponent<Rigidbody>();
}
void Start() {
playerScale = transform.localScale;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void FixedUpdate() {
Movement();
}
private void Update() {
MyInput();
Look();
}
/// <summary>
/// Find user input. Should put this in its own class but im lazy
/// </summary>
private void MyInput() {
x = Input.GetAxisRaw("Horizontal");
y = Input.GetAxisRaw("Vertical");
jumping = Input.GetButton("Jump");
crouching = Input.GetKey(KeyCode.LeftControl);
//Sprinting
if (Input.GetKeyDown(KeyCode.LeftShift) && !crouching)
StartSprint();
if (Input.GetKeyUp(KeyCode.LeftShift))
StopSprint();
//Crouching
if (Input.GetKeyDown(KeyCode.LeftControl))
StartCrouch();
if (Input.GetKeyUp(KeyCode.LeftControl))
StopCrouch();
}
private void StartCrouch() {
transform.localScale = crouchScale;
transform.position = new Vector3(transform.position.x, transform.position.y - 0.5f, transform.position.z);
maxSpeed = 7f;
}
private void StopCrouch() {
transform.localScale = playerScale;
transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
maxSpeed = speedOrigin;
}
//Sprinting
private void StartSprint() {
maxSpeed = sprintspeed;
}
private void StopSprint() {
maxSpeed = speedOrigin;
}
private void Movement() {
//Extra gravity
rb.AddForce(Vector3.down * Time.deltaTime * 10);
//Find actual velocity relative to where player is looking
Vector2 mag = FindVelRelativeToLook();
float xMag = mag.x, yMag = mag.y;
//Counteract sliding and sloppy movement
CounterMovement(x, y, mag);
//If holding jump && ready to jump, then jump
if (readyToJump && jumping) Jump();
//Set max speed
float maxSpeed = this.maxSpeed;
//If sliding down a ramp, add force down so player stays grounded and also builds speed
if (crouching && grounded && readyToJump) {
rb.AddForce(Vector3.down * Time.deltaTime * 3000);
return;
}
//If speed is larger than maxspeed, cancel out the input so you don't go over max speed
if (x > 0 && xMag > maxSpeed) x = 0;
if (x < 0 && xMag < -maxSpeed) x = 0;
if (y > 0 && yMag > maxSpeed) y = 0;
if (y < 0 && yMag < -maxSpeed) y = 0;
//Some multipliers
float multiplier = 1f, multiplierV = 1f;
// Movement in air
if (!grounded) {
maxSpeed = 8f;
}
// Movement while crouching
if (grounded && crouching) maxSpeed = 7f;
//Apply forces to move player
rb.AddForce(orientation.transform.forward * y * moveSpeed * Time.deltaTime * multiplier * multiplierV);
rb.AddForce(orientation.transform.right * x * moveSpeed * Time.deltaTime * multiplier);
}
private void Jump() {
if (grounded && readyToJump) {
readyToJump = false;
//Add jump forces
rb.AddForce(Vector2.up * jumpForce * 1.5f);
rb.AddForce(normalVector * jumpForce * 0.5f);
//If jumping while falling, reset y velocity.
Vector3 vel = rb.velocity;
if (rb.velocity.y < 0.5f)
rb.velocity = new Vector3(vel.x, 0, vel.z);
else if (rb.velocity.y > 0)
rb.velocity = new Vector3(vel.x, vel.y / 2, vel.z);
Invoke(nameof(ResetJump), jumpCooldown);
}
}
private void ResetJump() {
readyToJump = true;
}
private float desiredX;
private void Look() {
float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
//Find current look rotation
Vector3 rot = playerCam.transform.localRotation.eulerAngles;
desiredX = rot.y + mouseX;
//Rotate, and also make sure we dont over- or under-rotate.
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
//Perform the rotations
playerCam.transform.localRotation = Quaternion.Euler(xRotation, desiredX, 0);
orientation.transform.localRotation = Quaternion.Euler(0, desiredX, 0);
}
private void CounterMovement(float x, float y, Vector2 mag) {
if (!grounded || jumping) return;
//Slow down sliding
if (crouching) {
rb.AddForce(moveSpeed * Time.deltaTime * -rb.velocity.normalized * slideCounterMovement);
return;
}
//Counter movement
if (Math.Abs(mag.x) > threshold && Math.Abs(x) < 0.05f || (mag.x < -threshold && x > 0) || (mag.x > threshold && x < 0)) {
rb.AddForce(moveSpeed * orientation.transform.right * Time.deltaTime * -mag.x * counterMovement);
}
if (Math.Abs(mag.y) > threshold && Math.Abs(y) < 0.05f || (mag.y < -threshold && y > 0) || (mag.y > threshold && y < 0)) {
rb.AddForce(moveSpeed * orientation.transform.forward * Time.deltaTime * -mag.y * counterMovement);
}
//Limit diagonal running. This will also cause a full stop if sliding fast and un-crouching, so not optimal.
if (Mathf.Sqrt((Mathf.Pow(rb.velocity.x, 2) + Mathf.Pow(rb.velocity.z, 2))) > maxSpeed) {
float fallspeed = rb.velocity.y;
Vector3 n = rb.velocity.normalized * maxSpeed;
rb.velocity = new Vector3(n.x, fallspeed, n.z);
}
}
/// <summary>
/// Find the velocity relative to where the player is looking
/// Useful for vectors calculations regarding movement and limiting movement
/// </summary>
/// <returns></returns>
public Vector2 FindVelRelativeToLook() {
float lookAngle = orientation.transform.eulerAngles.y;
float moveAngle = Mathf.Atan2(rb.velocity.x, rb.velocity.z) * Mathf.Rad2Deg;
float u = Mathf.DeltaAngle(lookAngle, moveAngle);
float v = 90 - u;
float magnitue = rb.velocity.magnitude;
float yMag = magnitue * Mathf.Cos(u * Mathf.Deg2Rad);
float xMag = magnitue * Mathf.Cos(v * Mathf.Deg2Rad);
return new Vector2(xMag, yMag);
}
private bool IsFloor(Vector3 v) {
float angle = Vector3.Angle(Vector3.up, v);
return angle < maxSlopeAngle;
}
private bool cancellingGrounded;
/// <summary>
/// Handle ground detection
/// </summary>
private void OnCollisionStay(Collision other) {
//Make sure we are only checking for walkable layers
int layer = other.gameObject.layer;
if (whatIsGround != (whatIsGround | (1 << layer))) return;
//Iterate through every collision in a physics update
for (int i = 0; i < other.contactCount; i++) {
Vector3 normal = other.contacts[i].normal;
//FLOOR
if (IsFloor(normal)) {
grounded = true;
cancellingGrounded = false;
normalVector = normal;
CancelInvoke(nameof(StopGrounded));
}
}
//Invoke ground/wall cancel, since we can't check normals with CollisionExit
float delay = 3f;
if (!cancellingGrounded) {
cancellingGrounded = true;
Invoke(nameof(StopGrounded), Time.deltaTime * delay);
}
}
private void StopGrounded() {
grounded = false;
}
}
Have you tried to delete the return inside this?
if (crouching && grounded && readyToJump) {
rb.AddForce(Vector3.down * Time.deltaTime * 3000);
return;
}
Because of that, you are not applying force for move your object based on your inputs.
I have a character controller movement where the player can walk, run and jump. However, I facing an issue with the jumping. When jumping while walking, the jump speed looks good. But when jumping while running, the jump speed is too slow compared to the run speed. How can I fix this?
Here is my code:
public class PlayerMovement : MonoBehaviour
{
[SerializeField] Transform playerCamera = null;
[SerializeField] float mouseSensitivity = 3.5f;
[SerializeField] float walkSpeed = 10.0f;
[SerializeField] float RunSpeed = 12.0f;
[SerializeField] float gravity = 9.81f;
[SerializeField] bool lockCursor = true;
[SerializeField] [Range(0.0f, 0.5f)] float moveSmoothTime = 0.3f;
public float jumpHeight = 3f;
Vector3 velocity;
public float verticalVelocity;
float cameraPitch = 0.0f;
CharacterController controller = null;
Vector2 currentDir = Vector2.zero;
Vector2 currentDirVelocity = Vector2.zero;
Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;
void Start()
{
controller = GetComponent<CharacterController>();
if (lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
void Update()
{
UpdateMouseLook();
UpdateMovement();
}
void UpdateMouseLook()
{
Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);
cameraPitch -= currentMouseDelta.y * mouseSensitivity;
cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
playerCamera.localEulerAngles = Vector3.right * cameraPitch;
transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
}
void UpdateMovement()
{
Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
targetDir.Normalize();
currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);
if (controller.isGrounded)
velocity.y = 0.0f;
velocity += (velocity.y < 0 ? Physics.gravity * 2 : Physics.gravity) * Time.deltaTime;
velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + Vector3.up * velocity.y;
if (Input.GetKeyDown(KeyCode.J) && controller.isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
velocity += (velocity.y < 0 ? Physics.gravity * 2 : Physics.gravity) * Time.deltaTime;
}
controller.Move(velocity * Time.deltaTime);
if ((Input.GetKey("left shift") || Input.GetKey("right shift")) && controller.isGrounded && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.DownArrow))
{
velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * RunSpeed + Vector3.up * velocity.y;
controller.Move(velocity * Time.deltaTime);
}
}
I optimized the above code and removed some dublicate. I also noticed that the speed problem was due to the presence of isGrounded in the body of the running condition, which made running dependent on being on the ground. This is the player script:
public class Player : MonoBehaviour
{
[SerializeField] Transform playerCamera = null;
[SerializeField] float mouseSensitivity = 3.5f;
[SerializeField] float walkSpeed = 10.0f;
[SerializeField] float runSpeed = 12.0f;
[SerializeField] float gravity = 9.81f;
[SerializeField] bool lockCursor = true;
[SerializeField] [Range(0.0f, 0.5f)] float moveSmoothTime = 0.3f;
public float jumpHeight = 3f;
Vector3 velocity;
private float verticalVelocity;
private Vector3 inputVector;
float cameraPitch = 0.0f;
CharacterController controller = null;
Vector2 currentDir = Vector2.zero;
Vector3 currentDirVelocity = Vector3.zero;
Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;
void Start()
{
controller = GetComponent<CharacterController>();
if (lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
void Update()
{
UpdateMouseLook();
UpdateMovement();
}
void UpdateMouseLook()
{
var targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, moveSmoothTime);
cameraPitch -= currentMouseDelta.y * mouseSensitivity;
cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
playerCamera.localEulerAngles = Vector3.right * cameraPitch;
transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
}
void UpdateMovement()
{
// jumping
var isGrounded = Physics.Raycast(transform.position, Vector3.down, 1.1f) && velocity.y <= 0;
if (isGrounded)
{
velocity.y = Input.GetKeyDown(KeyCode.Space) ? Mathf.Sqrt(jumpHeight) : 0f;
}
else
{
velocity += (velocity.y < 0 ? Physics.gravity * 2 : Physics.gravity) * Time.deltaTime;
}
controller.Move(velocity);
// movement
inputVector = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized;
var smoothDamp = Vector3.SmoothDamp(inputVector, inputVector, ref currentDirVelocity, moveSmoothTime);
var runKey = (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) &&
!Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.DownArrow);
controller.Move(smoothDamp * (runKey ? runSpeed : walkSpeed) * Time.deltaTime);
}
}
Also, these settings were suitable for a height 2 capsule:
If your character is greater than 2, adjust the length of the ground detection raycast accordingly because the controller.isGrounded condition does not always work.
When you are using character controller you need to do all the calculations for all the situations.
To get the result you are looking for you can do a calculation beetween jump and walking speed.
Another way is to use Rigid body (lock rotations) and add forces for jump and move.
both examples here: https://www.youtube.com/watch?v=b1uoLBp2I1w
I updated my code, my current problem is just that the movement of the player doesn't work anymore and I would like to know what I have to do so that I can adjust the height of the jump and that when this height is reached, gravity comes through again
Code:
//Private Variables
private CharacterController _chaCont;
private float currentGravity;
private Vector3 finalMovement;
//Globally delared
//Public Variables
public float speed;
public float gravity;
public float jumpSpeed;
// Start is called before the first frame update
void Start()
{
_chaCont = gameObject.GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
finalMovement = jump() + ApplyGravity();
}
_chaCont.Move(finalMovement * Time.deltaTime);
}
Vector3 ApplyGravity()
{
Vector3 gravityMovement = new Vector3(0, -currentGravity, 0);
currentGravity += gravity * Time.deltaTime;
if (_chaCont.isGrounded)
{
if (currentGravity > 1f)
currentGravity = 1f;
}
return gravityMovement;
}
Vector3 Movement()
{
Vector3 moveVector = Vector3.zero;
moveVector += transform.forward * Input.GetAxis("Vertical");
moveVector += transform.right * Input.GetAxis("Horizontal");
moveVector *= speed;
return moveVector;
}
Vector3 jump()
{
Vector3 jumpVector = Vector3.zero;
jumpVector += transform.up * Input.GetAxis("Jump");
jumpVector *= jumpSpeed;
return jumpVector;
}
}
Good Night. Have you tried the input system C# provides should be like below code.
//Private Variables
private CharacterController _chaCont;
private float currentGravity;
private Vector3 finalMovement; //Globally delared
//Public Variables
public float speed;
public float gravity;
// Start is called before the first frame update
void Start()
{
_chaCont = gameObject.GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
Vector3 initialMovement = Movement();
if(Input.GetKeyDown(KeyCode.Space)){
finalMovement = initialMovement + ApplyGravity();
}
else{
finalMovement = initialMovement;
}
_chaCont.Move(finalMovement * Time.deltaTime);
}
Vector3 ApplyGravity()
{
Vector3 gravityMovement = new Vector3(0, -currentGravity, 0);
currentGravity += gravity * Time.deltaTime;
if (_chaCont.isGrounded)
{
if (currentGravity > 1f)
currentGravity = 1f;
}
return gravityMovement;
}
Vector3 Movement()
{
Vector3 moveVector = Vector3.zero;
moveVector += transform.forward * Input.GetAxis("Vertical");
moveVector += transform.right * Input.GetAxis("Horizontal");
moveVector *= speed;
return moveVector;
}
Hello I was trying to do movement script in Unity. Also I wanted to add jump, but everytime when I jump it moves up for like 0.025 on Y direction and stops the player in air*(if I am on 0 and I jump it moves on 0.02545 then 0.0543 etc...)* and I can spam Space to move player up. I added gravity but it looks it doesn't work.. I don't know how to fix it. I hope someone can help me with my problem...
Here is the function what I am using...
Vector3 moveDirection = Vector3.zero;
public float walkingSpeed = 10.0f;
public bool canJump = true;
public float jumpSpeed = 8.0f;
public float gravity = 10.0f;
CharacterController characterController;
void Movement()
{
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
float moveX = walkingSpeed * Input.GetAxis("Vertical");
float moveY = walkingSpeed * Input.GetAxis("Horizontal");
float MovementY = moveDirection.y;
moveDirection = (forward * moveX) + (right * moveY);
if (Input.GetButtonDown("Jump") && canJump)
{
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
characterController.Move(moveDirection * Time.deltaTime);
}
Because you are resetting moveDirection every frame (when you call moveDirection = (forward * moveX) + (right * moveY) you aren't letting the gravity accumulate. You should instead save the vertical speed separately and add it every frame.
Something like the following:
Vector3 moveDirection = Vector3.zero;
public float walkingSpeed = 10.0f;
public bool canJump = true;
public float jumpSpeed = 8.0f;
public float gravity = 10.0f;
CharacterController characterController;
private float verticalSpeed;
void Movement()
{
float moveX = walkingSpeed * Input.GetAxis("Vertical");
float moveY = walkingSpeed * Input.GetAxis("Horizontal");
verticalSpeed -= gravity * Time.deltaTime;
if (Input.GetButtonDown("Jump") && canJump)
{
verticalSpeed = jumpSpeed;
}
characterController.Move((moveX * transform.forward + moveY * transform.right + verticalSpeed * transform.up)
* Time.deltaTime);
}
transform.forward is equivalent to transform.TransformDirection(Vector3.forward)
for some reason my Player is reacting really weirdly to the gravity, he falls normally for about 1 second then just floats down the rest of the way really slowly. increasing the gravity doesnt change anything either. Im at a loss.
Any help would be greatly appreciated!
using UnityEngine;
public class newThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
public float speed = 6;
public float gravity = -20f;
public float jumpHeight = 1;
Vector3 velocity;
bool isGrounded;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
float turnSmoothVelocity;
public float turnSmoothTime = 0.1f;
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
}
}