How to solve problem with nojump object, that has to jump? - c#

I have problem with my code, I´ve started learn C#, so I tried code some game(something like Geometry Dash). I want that move and on click jump. I wrote "move code" and "onclick jump code" but isn´t working.
I´m writing in Microsoft Visual Studio in C# and design in Unity 2018.3.5f1.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Plaxercontrol2 : MonoBehaviour
{
public Rigidbody2D rb;
public Transform groundCheck;
public Transform startPosition;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool onGround;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
rb.velocity = new Vector2(3, rb.velocity.y);
onGround = Physics2D.OverlapCircle(groundCheck.position,
groundCheckRadius, whatIsGround);
if (Input.GetMouseButtonDown(0) && onGround)
{
rb.velocity = new Vector2(5, rb.velocity.x);
}
}
}
Now, it´s only moving
and picture of Unity

Instead of changing yourself the velocity, I'm pretty sure you should use rb.AddForce() or something similar. That is how you're supposed to move rigid bodies.
You could do something like:
rb.AddForce(Vector2.up * whatEverValue, ForceMode2D.Impulse)

Related

why is my cube not going forward and is just staying at it's position in unity3d

using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody rb;
// Update is called once per frame
void fixedUpdate()
{
rb.AddForce(0, 0, 2000 * Time.deltaTime);
}
}
I am trying to make my cube go forward but it isn't moving forward. I am using the community version of visual studio 2019.
Check that yo attach the rb in the editor or with code as indicated in the documentation. Check code snippet below. Also you may want to try a bigger forca than 2000 * Time.deltaTime. Time.deltaTime is a small value. Check here. Try a bigger value, and check that the mass of the rigidbody is not very big so that the force is big enough to move the body.
using UnityEngine;
public class ExampleClass : MonoBehaviour
{
public float thrust = 1.0f;
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rb.AddForce(transform.forward * thrust);
}
}

Moving a rigid body in the direction it's facing in Unity and C#

I've been trying to get this working using pretty much every method I can find (even using the dreaded transform.translate) but I just can't seem to get it working. This is just a draft of the code and if there are any other ways to go about it I'm down to change some stuff.
Currently, he barely moves (it looks like he's getting stuck on the floor somehow.) I'm fairly new to moving objects using rigid bodies so I'm pretty much in the dark on how to solve this issue.
Here's the script from my latest crack at it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerTest : MonoBehaviour
{
public float speed = 10.0f;
public Rigidbody rb;
public Vector3 movement;
// Start is called before the first frame update
void Start()
{
rb.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("w"))
{
movement = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
}
}
void FixedUpdate()
{
moveCharacter(movement);
}
void moveCharacter(Vector3 direction)
{
rb.MovePosition(transform.position + (transform.forward * speed * Time.deltaTime));
}
}
In your code you have your moveCharacter function inside your Update function, enclosed is the fixed one, which should work now. Before your FixedUpdate was not getting called, therefore your moveCharacter function was not as well and your GameObject was not moving.
EDIT 1: You shoud also multiply with your movement direction as well, updated the script to fit that
EDIT 2: I misplaced the curly brackets, fixed that now
EDIT 3: You should also update your movement Vector3 every frame, not if W is pressed, fixed the script again.
EDIT 4: Fixed the movement bug (copy and paste entire script, since i changed more lines)
This is the updated script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerTest : MonoBehaviour
{
public float speed = 10.0f;
public Rigidbody rb;
public Vector3 movement;
// Start is called before the first frame update
void Start()
{
rb.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
movement = new Vector3(Input.GetAxis("Horizontal"), 1f, Input.GetAxis("Vertical"));
}
void FixedUpdate()
{
moveCharacter(movement);
}
void moveCharacter(Vector3 direction)
{
Vector3 offset = new Vector3(movement.x * transform.position.x, movement.y * transform.position.y, movement.z * transform.position.z);
rb.MovePosition(transform.position + (offset * speed * Time.deltaTime));
}
}
References: Rigidbody.MovePosition

Is there a way to rotate an object based on players position in unity 2d

I am trying to make a basic platformer using Unity and my player has a sort of gun. My bullet is only shooting in one location, and I would like it to rotate with the player. I am quite new to C# and need some help.
I have tried using if and else statements and I have tried manually rotating it.
My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class fire : MonoBehaviour
{
public Transform player;
public Rigidbody2D rb2d;
public Transform enemy;
public Transform myspawn;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Spawn"))
{
transform.position = player.position;
rb2d.velocity = new Vector2(10.0f, 0.0f);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.transform == enemy)
{
transform.position = myspawn.position;
}
}
}
I expect the output to rotate the bullet with the player, and using the if statements I got a lot of different errors. The actual output I got was the bullet just wouldn't move until you manually push the bullet.
If your gun is only shooting at one direction you can do something like this:
Create a new boolean variable, call it something like isLookingRight in your player controller script and set it to true if your player is initially looking at right. Whenever player moves left, change this variable to false and change your Update in the code you have provided to this:
void Update()
{
if (Input.GetButtonDown("Spawn"))
{
transform.position = player.position;
if(playerScript.isLookingRight){
rb2d.velocity = new Vector2(10.0f, 0.0f);
}
else{
rb2d.velocity = new Vector2(-10.0f, 0.0f);
}
}
What this code does is pretty simple, if your player is looking left, it just changes the velocity to the opposite of what it would normally be. But you need to be changing the isLookingRight correctly in your player movement script.

Why are collision checks in Unity2D providing various outcomes?

I'm trying to recreate AA in Unity2D to kind of learn how everything works, I'm using a YouTube tutorial to guide me through it and I have the exact code in the video but it doesn't seem to be working correctly, the pins won't stop at the same time when they touch the rotating ball (seen below). Some will go halfway into the ball while others will stop prematurely.
Here's the pin code (Pin.cs):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pin : MonoBehaviour {
private bool moving = true;
public float speed = 20f;
public Rigidbody2D rb;
void Update() {
if (moving)
rb.MovePosition(rb.position + Vector2.up * speed * Time.deltaTime);
}
void OnTriggerEnter2D(Collider2D collider) {
if (collider.tag == "Rotator") {
moving = false;
transform.SetParent(collider.transform);
}
}
}
Just use FixedUpdate() instead Update().
Refer to Unity Documentation - FixedUpdate link, FixedUpdate() should be used instead of Update() when dealing with Rigidbody.
So, Change your code to this:
public class Pin : MonoBehaviour
{
...
void FixedUpdate()
{
if (moving)
rb.MovePosition(rb.position + Vector2.up * speed * Time.deltaTime);
}
void OnTriggerEnter2D(Collider2D collider)
{
...
}
}
I hope it helps you.

Camera following player - issue being not smooth

I've written some code for my camera so that it follows my character (i'm making a 3D side-scrolling endless-runner/platform game).
It follows the player but its really jumpy and not smooth at all. How can i fix this?
I am avoiding parenting to the character because i don't want the camera to follow the player when it jumps upwards.
Here is my code:
using UnityEngine;
using System.Collections;
public class FollowPlayerCamera : MonoBehaviour {
GameObject player;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void LateUpdate () {
transform.position = new Vector3(player.transform.position.x, transform.position.y, transform.position.z);
}
}
I recommend using something like Vector3.Slerp or Vector3.Lerp instead of assigning the position directly. I included a speed variable, you can adjust it higher or lower to find the perfect speed for your camera to follow the player at.
using UnityEngine;
using System.Collections;
public class FollowPlayerCamera : MonoBehaviour {
public float smoothSpeed = 2f;
GameObject player;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void LateUpdate () {
transform.position = Vector3.Slerp(transform.position, new Vector3(player.transform.position.x, transform.position.y, transform.position.z), smoothSpeed * Time.deltaTime);
}
}
Hopefully this helps you get closer to your solution.

Categories

Resources