Unity C# - Move character while jumping - c#

My character moves great, and jumps great. But when jumping he just moves straight in the direction he came from and you can't rotate or move him while in the air. How can that be done?
From the Update Function:
if (controller.isGrounded)
{
moveD = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
moveD = transform.TransformDirection(moveD.normalized) * speed;
moveDA = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
if (moveDA.magnitude > 0)
{
gameObject.transform.GetChild(0).LookAt(gameObject.transform.position + moveDA, Vector3.up);
}
if (Input.GetButton("Jump"))
{
moveD.y = jumpSpeed;
}
}
moveD.y = moveD.y - (gravity * Time.deltaTime);
controller.Move(moveD * Time.deltaTime);

controller.isGrounded Is only true if the last time you called controller.Move() the bottom of the object's collider is touching a surface, so in your case once you jump, you cannot move until you hit the ground again.
You can solve this by separating your movement code and jumping code like so:
moveD = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
moveD = transform.TransformDirection(moveD.normalized) * speed;
moveDA = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
if (moveDA.magnitude > 0)
{
gameObject.transform.GetChild(0).LookAt(gameObject.transform.position + moveDA, Vector3.up);
}
if (controller.isGrounded)
{
if (Input.GetButton("Jump"))
{
moveD.y = jumpSpeed;
}
}
moveD.y = moveD.y - (gravity * Time.deltaTime);
controller.Move(moveD * Time.deltaTime);

Related

How to move forward an object in Unity?

I made a game object and attached a script to it. I need to rotate and move the object in a straight line, depending on the rotation. I made a turn, but I have problems with movement. Any solutions?
//Rotation
if (Input.GetAxis("Rotation") > 0) {
transform.Rotate(Vector3.back, turnSpeed * Time.deltaTime);
}
else if (Input.GetAxis("Rotation") < 0)
{
transform.Rotate(Vector3.back, -turnSpeed * Time.deltaTime);
}
//Velocity
if (Input.GetAxis("Thrust") != 0) {
rb.AddForce(Vector3.forward * Time.deltaTime * Speed);
}
else if (Input.GetAxis("Thrust") <= 0.1f){
rb.velocity = new Vector2(0, 0);
}
Rigidbody.AddForce applies the vector as force in world space. So, you need to give it a vector that is in the world space direction of the transform's forward. Luckily, that's as simple as using transform.forward, transform.up, or transform.right or a negation of one of them instead of Vector3.forward:
//Rotation
if (Input.GetAxis("Rotation") > 0) {
transform.Rotate(Vector3.back, turnSpeed * Time.deltaTime);
}
else if (Input.GetAxis("Rotation") < 0)
{
transform.Rotate(Vector3.back, -turnSpeed * Time.deltaTime);
}
//Velocity
if (Input.GetAxis("Thrust") != 0) {
Vector3 frontDirection;
// probably one of these for a 2D game:
frontDirection = transform.right;
//frontDirection = -transform.right;
//frontDirection = transform.up;
//frontDirection = -transform.up;
rb.AddForce(frontDirection * Time.deltaTime * Speed);
}
else if (Input.GetAxis("Thrust") <= 0.1f){
rb.velocity = new Vector2(0, 0);
}

Bullet instantiation angle is wrong when the player rotates

I created a projectile bullet effect with 3 different instantiation positions. It works perfectly while the player is facing right. But when facing left the angle of the bullet instantiation goes astray. Any help is appreciated.
Screenshot of when the player is turning right.
Screenshot of when the player is turning left.
The code of the player.
private IEnumerator FireContinuously()
{
while (true)
{
GameObject laser02 = Instantiate(bullet01, firePoint2.position, Quaternion.Euler(new Vector3(0, 0, 10)));
laser02.GetComponent<Rigidbody2D>().velocity = laser02.transform.right * projectileSpeed * direction;
GameObject laser03 = Instantiate(bullet02, firePoint.position, Quaternion.Euler(new Vector3(0, 0, 0)));
laser03.GetComponent<Rigidbody2D>().velocity = laser03.transform.right * projectileSpeed * direction;
GameObject laser04 = Instantiate(bullet03, firePoint3.position, Quaternion.Euler(new Vector3(0, 0, 345)));
laser04.GetComponent<Rigidbody2D>().velocity = laser04.transform.right * projectileSpeed * direction;
yield return new WaitForSeconds(projectileFiringPeriod);
}
}
public void Flipsprite()
{
bool playerhashorizontalspeed = Mathf.Abs(myRigidBody.velocity.x) > 0;
if (playerhashorizontalspeed)
{
direction = Mathf.Sign(myRigidBody.velocity.x);
transform.localScale = new Vector3(direction, 1f);
}
}
You are using hardcoded angles 10 and 345 regardless the direction you are looking.
Then using e.g. laser02.right always returns the same Vector3 only that you negate it. That results in an incorrect direction. It is the direction you get if you compare the images.
What you rather want is to negate also the angle by which you rotated the bullets.
private IEnumerator FireContinuously()
{
while (true)
{
var laser02 = Instantiate(bullet01, firePoint2.position, Quaternion.Euler(new Vector3(0, 0, 10 * direction)));
laser02.GetComponent<Rigidbody2D>().velocity = laser02.transform.right * projectileSpeed * direction;
var laser03 = Instantiate(bullet02, firePoint.position, Quaternion.Identity)
laser03.GetComponent<Rigidbody2D>().velocity = laser03.transform.right * projectileSpeed * direction;
var laser04 = Instantiate(bullet03, firePoint3.position, Quaternion.Euler(new Vector3(0, 0, 345 * direction)));
laser04.GetComponent<Rigidbody2D>().velocity = laser04.transform.right * projectileSpeed * direction;
yield return new WaitForSeconds(projectileFiringPeriod);
}
}
And a little hint:
If you would rather make your prefabs of type
[SerializeField] private RigidBody2D bullet01;
[SerializeField] private RigidBody2D bullet02;
[SerializeField] private RigidBody2D bullet03;
then Instantiate would directly return the according RigidBody2D reference and you could get rid of the GetComponent calls:
private IEnumerator FireContinuously()
{
while (true)
{
var laser02 = Instantiate(bullet01, firePoint2.position, Quaternion.Euler(new Vector3(0, 0, 10 * direction)));
laser02.velocity = laser02.transform.right * projectileSpeed * direction;
var laser03 = Instantiate(bullet02, firePoint.position, Quaternion.Identity)
laser03.velocity = laser03.transform.right * projectileSpeed * direction;
var laser04 = Instantiate(bullet03, firePoint3.position, Quaternion.Euler(new Vector3(0, 0, 345 * direction)));
laser04.velocity = laser04.transform.right * projectileSpeed * direction;
yield return new WaitForSeconds(projectileFiringPeriod);
}
}

Unity Player's Shoot Function Will Not Work

I am creating a game in Unity. I have so far been able to get my player to follow the mouse at a somewhat correct angle. I am also trying to implement a shooting function for the character that shoots from the correct angle (straight from the top of the player, toward the mouse at the time of shooting). When I click using this code, nothing happens. I have objects set up for player, bullet and fire Point.
public void Update()
{
if (FollowMouse || Input.GetMouseButton(0))
{
_target = Camera.ScreenToWorldPoint(Input.mousePosition);
_target.z = 0;
}
var delta = ShipSpeed * Time.deltaTime;
var bulletDelta = shootSpeed * Time.deltaTime;
bulletDelta *= Vector3.Distance(transform.position, _target);
if (ShipAccelerates)
{
delta *= Vector3.Distance(transform.position, _target);
}
angle = Mathf.Atan2(_target.y, _target.x) * Mathf.Rad2Deg;
transform.position = Vector3.MoveTowards(transform.position, _target, delta);
transform.rotation = Quaternion.Euler(0, 0, angle);
if (Input.GetMouseButtonDown(0) && Time.time > nextFire && numOfBullets > 0)
{
nextFire = Time.time + fireRate;
Instantiate(bullet, firePoint.position, firePoint.rotation);
numOfBullets--;
bullet.transform.position = Vector3.MoveTowards(bullet.transform.position, _target, bulletDelta);
}
}

Unity hold touch move horizontally

I have no idea how to start this, I want to move my character left and right when holding the touch.
Like in this game:
Example Game - Stairs from Ketchapp
I have only my script that detects the left or right space of the screen.
public float forwardSpeed = 5f;
public float sideSpeed = 5f;
private void Update()
{
Vector3 deltaPosition = transform.forward * forwardSpeed;
if (Input.touchCount > 0)
{
Vector3 touchPosition = Input.GetTouch(0).position;
if (touchPosition.x > Screen.width * 0.5f)
deltaPosition += transform.right * sideSpeed;
else
deltaPosition -= transform.right * sideSpeed;
}
transform.position += deltaPosition * Time.deltaTime;
}
This solution works for me. Its used in a simple block breaker game to move a paddle left or right.
void Update () {
if (Input.touchCount > 0){
Touch touch = Input.GetTouch(0);
int direction = (touch.position.x > (Screen.width / 2)) ? 1 : -1;
MovePaddle(direction);
}
}
void MovePaddle(int direction){
float xPos = transform.position.x + (direction * Time.deltaTime * paddleSpeed);
playerPos = new Vector3 (Mathf.Clamp (xPos, -8f, 8f), -9.5f, 0f);
transform.position = playerPos;
}
i think that what you are trying to say is to only move when you are pressing the screen, not?
maybe this might help you:
public float forwardSpeed = 5f;
public float sideSpeed = 5f;
private void Update()
{
Vector3 deltaPosition = transform.forward * forwardSpeed;
if (Input.touchCount > 0)
{
Vector3 touchPosition = Input.GetTouch(0).position;
if (touchPosition.x > Screen.width * 0.5f)
deltaPosition += transform.right * sideSpeed;
else
deltaPosition -= transform.right * sideSpeed;
}
else{
deltaPosition = sideSpeed;
}
transform.position += deltaPosition * Time.deltaTime;
}
pd: not tested because yet
I have a solution that is not really smooth
public float speed = 5;
public Rigidbody rb;
public void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
//Add touch support
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Touch touch = Input.touches[0];
h = touch.deltaPosition.x;
}
//Move only if we actually pressed something
if (h > 0 || h < 0)
{
Vector3 tempVect = new Vector3(h, 0, 0);
tempVect = tempVect.normalized * speed * Time.deltaTime;
//rb.MovePosition(rb.transform.position + tempVect);
Vector3 newPos = rb.transform.position + tempVect;
checkBoundary(newPos);
}
}
void checkBoundary(Vector3 newPos)
{
//Convert to camera view point
Vector3 camViewPoint = Camera.main.WorldToViewportPoint(newPos);
//Apply limit
camViewPoint.x = Mathf.Clamp(camViewPoint.x, 0.04f, 0.96f);
camViewPoint.y = Mathf.Clamp(camViewPoint.y, 0.07f, 0.93f);
//Convert to world point then apply result to the target object
Vector3 finalPos = Camera.main.ViewportToWorldPoint(camViewPoint);
rb.MovePosition(finalPos);
}
private void Start()
{
Application.targetFrameRate = 60;
}
void Update()
{
if (Input.touchCount > 0)
{
Touch t = Input.GetTouch(0);
transform.position = new Vector3(transform.position.x + t.deltaPosition.x * .02f, transform.position.y, transform.position.z );
}
}
You can use this. Simple :)

Rotate an object diagonally and prevent it from going back to 90,180,0,360 degrees

After holding down on a combination keys of AS,SD,DW or WA to move my object, it will successfully move diagonally and rotate to the correct position, but after releasing the keys, it will rotate back to either 0,90,180 or 360 depending on the nearest rotation after releasing my keys, I guess it's because of that 1 frame that I left the keys touched, so it ran that code and moved it to 0,90,180,360. But I don't know how to solve it.
I hope I've provided enough information, and thanks for helping out.
I'm pressing onto AW keys together
After releasing the keys, it doesn't stay that way but moves to either left or top
PlayerMovement.cs
using UnityEngine;
using System.Collections;
public class playerMovement : MonoBehaviour {
private float speedWalk = 7.5f;
private float speedRotate = 7.5f;
private GameObject raycastObject;
// Update is called once per frame
void FixedUpdate ()
{
//Non-Diagonal Movements
if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D)) //player movement up
{
transform.localPosition += new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.D)) //player movement down
{
transform.localPosition -= new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)) //player movement right
{
transform.localPosition += new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
}
if (Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)) //player movement left
{
transform.localPosition -= new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
}
//Diagonal Movements **########** I think this is the problem.
if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.W)) //player movement Top Left
{
transform.localPosition -= new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
transform.localPosition += new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
}
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D)) //player movement Top Right
{
transform.localPosition += new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
transform.localPosition += new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
}
if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.S)) //player movement Bottom Right
{
transform.localPosition += new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
transform.localPosition -= new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A)) //player movement Bottom Left
{
transform.localPosition -= new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
transform.localPosition -= new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
}
}
}
playerRotateMouse.cs
using UnityEngine;
using System.Collections;
public class playerRotateMouse : MonoBehaviour
{
public Transform Player;
private float speed = 7.5f;
Quaternion targetRotation;
void FixedUpdate()
{
Plane playerPlane = new Plane(Vector3.up, transform.position);
// Generates a ray from cursor
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (Input.GetButtonDown("Fire1") || Input.GetButtonDown("Fire2"))
{
if (playerPlane.Raycast(ray, out hitdist))
{
Vector3 targetPoint = ray.GetPoint(hitdist);
Debug.DrawRay(transform.position, targetPoint, Color.green);
targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
}
}
// Smooth rotation towards point.
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
//Rotate base on key pressed
if (Input.GetKey(KeyCode.W) && !(Input.GetButton("Fire1") || Input.GetButton("Fire2"))) //player rotate up
{
targetRotation = Quaternion.LookRotation(Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S) && !(Input.GetButton("Fire1") || Input.GetButton("Fire2"))) //player rotate down
{
targetRotation = Quaternion.LookRotation(Vector3.forward * -1);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D) && !(Input.GetButton("Fire1") || Input.GetButton("Fire2"))) //player rotate right
{
targetRotation = Quaternion.LookRotation(Vector3.right);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A) && !(Input.GetButton("Fire1") || Input.GetButton("Fire2"))) //player rotate left
{
targetRotation = Quaternion.LookRotation(Vector3.left);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
}
}
I would save a last timestamp, and check if the second key release is within a certain timeframe. If it is release within (for example) 20ms, don't reset it is pressed.
For example: (pseudo)
private bool[] _directionKeysPressed = new bool[4];
private DateTime _previousKeyRelease;
private void KeyDown(object sender, EventArgs e)
{
_keyPressedCount++;
switch(keys)
{
case(W): _directionKeysPressed[0] = true;
case(D): _directionKeysPressed[1] = true;
// .................
}
}
private void KeyUp(object sender, EventArgs e)
{
_keyPressedCount--;
if(_keyPressedCount == 0)
{
// all keys released
_directionKeysPressed[] <--- last direction...
// reset all bools
return;
}
// ONLY if the key is released outside the 20 ms, reset the key.
if(_previousKeyRelease.AddMilliseconds(20) < DateTime.UTCNow)
{ // RESET KEY
switch(keys)
{
case(W): _directionKeysPressed[0] = false;
case(D): _directionKeysPressed[1] = false;
// .................
}
}
_previousKeyRelease = DateTime.UTCNow;
}
Personally I would do this with an bitfield in a Int32, but it is more clear with an array of bool.

Categories