Cant Jump and run at the same time (Unity) - unity3d

wont let me run and jump at the same time, movement works fine but when i sprint jump isnt registered i tried a few things but nothing was successful
the character is a capsule and camera
this is the relevant code:
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)
{
if(Input.GetKeyDown(KeyCode.Space))
{
velocityY = 10.0f;
Debug.Log("Jump");
}
else
{
velocityY = 0.0f;
}
}
speed = walkSpeed;
if(Input.GetKey(KeyCode.LeftShift))
{
//Debug.Log("Sprint");
speed = walkSpeed * sprintMultiplier;
}
velocityY += gravity * Time.deltaTime;
Vector3 velocity = transform.forward * currentDir.y * speed + transform.right * currentDir.x * speed + Vector3.up * velocityY;
controller.Move(velocity * Time.deltaTime);
//Debug.Log(speed);
}
thank you in advance for any help

Related

Dashing in Unity 3D using Rigidbody

im currently making a fps game and i have a rigidbody character controller and im trying to make it dash towards the direction the player is facing however my current dash function makes it go downwards and goes very fast
any ideas for how i can either fix the dashing or make a new dash mechanism?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour
{
float yaw = 0f, pitch = 0f;
Rigidbody rb;
public float walkSpeed = 5f, sensitivity = 2f;
bool jumping = false;
private float DashDistance = 5f;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
rb = GetComponent<Rigidbody>();
}
private void Update()
{
if (Input.GetKey(KeyCode.Space) && Physics.Raycast(rb.transform.position, Vector3.down, 1 + 0.001f))
rb.velocity = new Vector3(rb.velocity.x, 5f, rb.velocity.z);
if (Physics.Raycast(rb.transform.position, Vector3.down, 1 + 0.001f))
jumping = false;
else
jumping = true;
if (jumping && Input.GetKey(KeyCode.LeftControl))
Dash();
Look();
}
private void FixedUpdate()
{
Movement();
}
void Look()
{
pitch -= Input.GetAxisRaw("Mouse Y") * sensitivity;
pitch = Mathf.Clamp(pitch, -90f, 90f);
yaw += Input.GetAxisRaw("Mouse X") * sensitivity;
Camera.main.transform.localRotation = Quaternion.Euler(pitch, yaw, 0f);
}
void Movement()
{
Vector2 axis = new Vector2(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal")) * walkSpeed;
Vector3 forward = new Vector3(-Camera.main.transform.right.z, 0f, Camera.main.transform.right.x);
Vector3 wishDir = (forward * axis.x + Camera.main.transform.right * axis.y + Vector3.up * rb.velocity.y);
rb.velocity = wishDir;
}
void Dash()
{
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 1f, Input.GetAxis("Vertical"));
Vector3 offset = new Vector3(movement.x * transform.position.x, movement.y * transform.position.y, movement.z * transform.position.z);
rb.AddForce(transform.position + (offset * DashDistance), ForceMode.VelocityChange);
}
}
I think you should add force in the forward direction.
AddForce(transform.forward * yourForceValue);

Problem in my jumping Script with characheter controller unity

So, I was testing my scripts and i wanted my player to jump with character controller and i am having a problem with it.
Problem
```
public CharacterController control;
public float playerSpeed;
public float jumpSpeed;
void Start()
{
playerSpeed = 6.0f;
jumpSpeed = 50;
}
void Update()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Vector3 move = new Vector3 (h, 0, v);
Vector3 velocity = move * playerSpeed;
if (control.isGrounded && Input.GetKey(KeyCode.Space))
{
velocity.y += jumpSpeed; // velocity.y = jumpSpeed; tried both
}
else
{
velocity += Physics.gravity * Time.deltaTime;
}
control.Move(velocity * Time.deltaTime);
}
}```
Here's my unity screen and code above.
The problem is when i press jump it does jump but it's position goes to 2.068, i.e it jumps to low and when gravity is activated it comes down to slow, it takes around 6 seconds to come to its initial position.
I even tried to add a parent object to it so that it may change, but it does the same to it.
if (control.isGrounded && Input.GetKey(KeyCode.Space))
{
// first just try this
velocity.y += jumpSpeed * Time.deltaTime;
}
else
{
velocity.y -= Physics.gravity * Time.deltaTime;
}
and if it is not working
Vector3 move = new Vector3 (h, 0, 0);
Vector3 jump = new Vector3 (0, 0, v);
Vector3 _velocityMove = move * playerSpeed;
Vector3 _velocityJump = jump* jumpSpeed;
if (control.isGrounded && Input.GetKey(KeyCode.Space))
{
velocity.y += _velocityJump ;//and you can add ' * Time.deltaTime '
}
else
{
velocity += Physics.gravity * Time.deltaTime;
}
control.Move(_velocityMove * Time.deltaTime);
ohk so I tweaked some of my code and instead of declaring velocity.y in the condition of if the player is grounded , i declared outside the condition and change the value of variable in condition.
velocity.y = jumpSpeed;
if (control.isGrounded && Input.GetKey(KeyCode.Space))
{
jumpSpeed = 10;
}
else
{
jumpSpeed += Physics.gravity.y * Time.deltaTime;
}
This seems to do the trick and my player can jump nicely.

CharacterController jump in unity

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)

How can I match my animation with transform in Unity3d?

I have implemented a player movement and jump in my game. I also attached a run and jump animation to animator. However, the jump animation does not perfectly match with the player vertical movement. How can I match them perfectly?
Thanks in advance,
void Control()
{
if (charController.isGrounded)
{
float h = Joystick.GetHorizontalAxis("MyJoystick");
float v = Joystick.GetVerticalAxis("MyJoystick");
moveDirection = new Vector3(h, 0.0f, v);
moveDirection *= (speed * sprint);
anim.SetFloat("WalkSpeed", moveDirection.magnitude);
if (moveDirection.magnitude > 0.5)
{
anim.SetFloat("WalkSpeed", moveDirection.magnitude / speed);
anim.SetFloat("Walk", 1.0f);
transform.forward = moveDirection;
}
else if (moveDirection.magnitude > 0 && moveDirection.magnitude < 0.5)
{
anim.SetFloat("WalkSpeed", moveDirection.magnitude / (speed * 0.5f));
anim.SetFloat("Walk", 0.5f);
transform.forward = moveDirection;
}
else if (moveDirection.magnitude == 0)
{
anim.SetFloat("Walk", 0f);
}
if (Input.GetKeyDown("space"))
{
moveDirection.y = jumpSpeed;
anim.SetTrigger("Jump");
}
}
moveDirection.y -= gravity * Time.deltaTime;
charController.Move(moveDirection * Time.deltaTime);
}
There is an option in the Animator component to do what you want.
Here is a link that describes it in more details.

How to implement such control as in the game: rolly vortex?

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);
}
}