Unity 2D - Tap Image to accelerate car - unity3d

I am making a Hill Climb Racing clone as a school project. Currently my car is accelerating with the A and D keys in my editor (movement = Input.GetAxis("Horizontal");), however the game has to be for Android, therefore like in the OG game, I added 2 sprites for brakes and pedals and added EventSystems to them eg.:
public void OnPointerDown(PointerEventData eventData)
{
gas.sprite = OnSprite_gas;
is_clicking = true;
}
and I dont know how to change acceleration to when the gas image is clicked and held down and how to brake (but not go backwards) when the brake is held down.

You seem to be on the right track.
In the car's Update() method, you will want to check if the brake or accelerator button is_clicking property is set, and handle the movement force.
This could look something like:
void Update()
{
if (accelerator.is_clicking)
{
movement = new Vector3(1f, 0f, 0f) * speed;
}
else if (brake.is_clicking)
{
movement = new Vector3(-1f, 0f, 0f) * speed;
}
else
{
movement = new Vector3(0f, 0f, 0f);
}
}
void FixedUpdate()
{
rb.AddForce(movement * Time.fixedDeltaTime);
}
You could then check if the velocity is close to 0 to stop applying the brake force.

Related

In Unity3d how can I move a gameobject with mouse while interacting with physics engine?

I would like to develop a 3d game. An "air hockey" simulator. I would like to control the paddle with the mouse but at the same time, I would like to use the physics engine to handle collisions with the puck and the borders of the table.
I tried the following:
If I use a Kinematic paddle I lose the property to handle the physics by the engine.
If I use static object the same.
If I use a dynamic rigidbody I am not able to control smoothly the paddle.
What is the best way to handle this scenario?
Thank you.
What makes a air-puck feel good is the sliding effect it has. Ofcourse it doesn't continue forever, but still feels nice.
Here is what you can do:
Create an Paddle & Puck
Create two physics materials for both.
Decrease the friction on the material that both feels slidey, the puck a little more than the paddle.
For both:
Freeze the x & z rotation
Freeze the y position
Now the part that makes the paddle use physics & RigidBody correctly. Create a new script for moving the paddle:
public class PaddleMovement : MonoBehaviour
{
private RigidBody rb;
public float speed = 5;
public float minDist = 0;
public float maxDist = 5;
public LayerMask layers;
void Start()
{
rb = GetComponent<RigidBody>();
}
void Update()
{
// Paddle will only move if we hold down the mouse button
paddleGrabed = Input.GetInput(KeyCode.Mouse0);
}
void FixedUpdate()
{
if (paddleGrabed)
{
HandleMovement();
}
}
void HandleMovement()
{
Ray ray = Camera.main.ScreenToWorldPoint(Input.MousePosition);
RaycstHit hit;
if (Physics.Raycast(ray, out hit, 100f, layers))
{
// Calculate the slow effect as paddle comes close to the point;
float dist = Vector3.Distance
(
new Vector3(transform.position.x, 0 transform.position.z),
new Vector3(hit.point.x, 0, hit.point.z)
);
dist = Mathf.Clamp(dist, minDist, maxDist);
var slowEffect = dist / maxDist;
// Now move move the rigid body appropriately
var dir = new Vector3(hit.point.x, 0, hit.point.z) -new Vector3(transform.position.x, 0 transform.position.z);
dir.Normalize();
rb.MovePosition(transform.position + dir * slowEffect * speed * Time.deltaTime);
}
}
}
That should move the Paddle to a certain position with a bit of a lag and sliding effect.

Unity collider shaking after hitting another collider

I have a SMALL a problem. In Unity 3D 2020 Beta, I've put a player with a sphere collider on it and some cubes (walls) with box colliders. I've added a player controller script to the player object.
I've put the camera above the plane where the player and the walls are on, and I've made that the player should rotate to face the mouse position. I used rigidbody.AddForce for movement in a FixedUpdate function.
The player controller script is attached below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[Header("Keys")]
public KeyCode forward;
public KeyCode backward;
public KeyCode left;
public KeyCode right;
public KeyCode fire;
[Header("Health")]
public int hitpoints = 3;
[Header("Movement")]
public float speed;
public float turningSpeed;
[Header("Shooting")]
public GameObject bulletPrefab;
public Transform bulletSpawner;
public float bulletSpeed;
public float reloadTime;
private float currentReload;
private Rigidbody rb;
private Quaternion targetRotation;
void Start()
{
rb = GetComponent<Rigidbody>();
currentReload = reloadTime;
}
void LateUpdate()
{
if (hitpoints == 0)
Die();
// Rotation
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
targetRotation = Quaternion.LookRotation(hit.point - transform.position);
Debug.DrawLine(transform.position, hit.point, Color.white);
}
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turningSpeed * Time.deltaTime);
transform.eulerAngles = new Vector3(0, transform.rotation.eulerAngles.y, 0);
currentReload += Time.deltaTime;
// Shooting
if (Input.GetKeyDown(fire) && currentReload >= reloadTime)
{
currentReload = 0f;
GameObject bulletGO = Instantiate(bulletPrefab, bulletSpawner.position, transform.rotation);
bulletGO.transform.position = bulletSpawner.position;
Bullet bulletScript = bulletGO.GetComponent<Bullet>();
bulletScript.speed = bulletSpeed;
Destroy(bulletGO, 5f);
}
}
void FixedUpdate()
{
// Movement
if (Input.GetKey(forward))
{
rb.AddForce(Vector3.forward * speed, ForceMode.Force);
}
if (Input.GetKey(backward))
{
rb.AddForce(-Vector3.forward * speed, ForceMode.Force);
}
if (Input.GetKey(left))
{
rb.AddForce(Vector3.left * speed, ForceMode.Force);
}
if (Input.GetKey(right))
{
rb.AddForce(Vector3.right * speed, ForceMode.Force);
}
//transform.position = new Vector3(transform.position.x, 10, transform.position.z);
// ON RIGIDBODY I HAVE CONSTRAINS:
// POSITION: Y (thats why I commented the line above)
// ROTATION: X, Z (topdown -> so I want only rotation on Y)
}
private void Die()
{
Destroy(gameObject);
}
}
But the problem is when the player hits very hard a wall, the sphere collider starts shaking and the player does not look at the mouse position exactly (it is somewhere 10 degrees away most of the times - it depends on how hard do I hit the walls).
I can record if it helps. If you want any information, feel free to ask! Any help will be appreciated! :)
The problem here seems to be you're directly altering the transform of your object despite having a Rigidbody component. Generally you should avoid altering a transform directly when you have a Rigidbody attached, especially a non-kinematic one, as by attaching one you are signalling that the object is to be controlled by the physics simulation.
Solutions I would explore:
If you don't need a rigidbody, don't use one
If you can avoid altering the transform directly, then do not do so. You can rotate objects by applying torque and the likes
Try setting your object to kinematic if you don't need collisions to affect the rigidbody's physics
Manually set the torque and velocity of your object to 0 each fixed update

Raycast only returns false, even when true expected

I have the following FollowingNPC game object, which is supposed to follow the Player object if there is direct vision:
Here the Player object:
The BlockingLayer and Player Layers have the physics collision enabled between each other:
The FollowingNPC hast the following script attached, that always returns a "Not Hit" result: Raycast always false. This is not the expected output, as both objects seem to have a clear view between each other and the debug ray can be drawn without problems.
public class FollowingNPC : MonoBehaviour
{
private Vector3 heading, direction;
private float distance;
void Update()
{
heading = GameObject.FindGameObjectWithTag("Player").transform.position - transform.position;
distance = heading.magnitude;
direction = heading / distance;
RaycastHit hit;
if (Physics.Raycast(transform.position, direction, out hit))
{
if (hit.transform== GameObject.FindGameObjectWithTag("Player").transform)
{
Debug.DrawRay(transform.position, direction * distance, Color.red);
Debug.Log("Hit Player");
}
else
{
Debug.DrawRay(transform.position, direction * distance, Color.yellow);
Debug.Log("Hit Wall");
}
}
else
{
Debug.DrawRay(transform.position, direction * distance, Color.white);
Debug.Log("Not Hit");
}
}
}
Solved:
As derHugo suggested, Physics2D.Raycast should be used here. This function does not return a bool, so this is the implementation that worked for me:
void Update()
{
heading = GameObject.FindGameObjectWithTag("Player").transform.position - transform.position;
RaycastHit2D hit = Physics2D.Raycast(transform.position, heading.normalized, LayerMask.GetMask("Player"));
if (hit.collider != null)
if (hit.transform == GameObject.FindGameObjectWithTag("Player").transform)
Debug.Log("Hit Player");
else
Debug.Log("Hit Wall");
else
Debug.Log("Not Hit");
}
Also, it's important to note that, even with the mask, the Raycast returned the hit with the collider of FolloginNPC, so I had to disable it. I'll have to do some investigation or search a workaround.
Note that in Unity the Physics and Physics2D are completely independent and separated Physics engines!
Built-in 3D physics (Nvidia PhysX engine integration)
Built-in 2D physics (Box2D engine integration)
You have Rigidbody2D and Collider2D so you would rather want to use Physics2D.Raycast!
You should also strongly avoid using FindGameObjectWithTag in Update and even worse multiple times as it is quite expensive! Rather do it once and store the result.
// If possible and best would be to already reference it via the Inspector
[SerilaizeField] private Transform player;
// As fallback get it ONCE on runtime
private void Awake()
{
if(!player) player = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update()
{
// actually those wouldn't really need to be fields
heading = player.position - transform.position;
distance = heading.magnitude;
// This line isn't really necessary
// If something I would use
//direction = heading.normalized
direction = heading / distance;
var hit = Physics2D.Raycast(transform.position, direction);
if (hit.collider)
{
if (hit.transform == player)
{
Debug.DrawRay(transform.position, direction * distance, Color.red);
Debug.Log("Hit Player");
}
else
{
Debug.DrawRay(transform.position, direction * distance, Color.yellow);
Debug.Log("Hit Wall");
}
}
else
{
Debug.DrawRay(transform.position, direction * distance, Color.white);
Debug.Log("Not Hit");
}
}
Later you should also remove all these Debug.Log since they are also quite expensive when done in Update!

Unity jumping issue, character is not jumping at all

(This is 2D project)
My character need to run left and right and jump on ground. It is my first experience with Unity and it latest available version of it, I manage to make my Character run left and right and flip when it is change direction, but it does not jump at all.
For the Character I am currently using 2 Box Collider 2Ds and Rigidbody 2D. Character has mass equal 1.
For the Ground I am currently using 2 Box Collider 2Ds. Ground is single sprite which is cover bottom part of screen.
Below is the code for jumping and grounded I am currently trying to use.
'''
{
public float maxSpeed = 10f;
public float jumpVelocity = 10f;
private bool isGrounded;
private float move = 0f;
private Rigidbody2D rb2d; //Store a reference to the Rigidbody2D component required to use 2D Physics.
private SpriteRenderer sprt_render;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
isGrounded = true;
}
// Update is called once per frame
void Update()
{
GetInput();
}
private void GetInput()
{
move = Input.GetAxis("Horizontal");
if (transform.position.x > -6.2 && transform.position.x < 5.7)
{
rb2d.velocity = new Vector2(move * maxSpeed, rb2d.velocity.y);
}
else
{
rb2d.velocity = new Vector2(move * maxSpeed * -1, rb2d.velocity.y);
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
Debug.Log("Jump");
rb2d.velocity = new Vector2(rb2d.velocity.x, jumpVelocity);
isGrounded = false;
//rb2d.AddForce(Vector2.up * jumpVelocity, ForceMode2D.Impulse);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
}
'''
As you can see I try to use two different approach in connection with jump realisation using Velocity and using AddForce result is totally the same it is not jumping even first time. I check thought breakpoint it definitely go trough jumping code but nothing happened.

Vector2.MoveTowards -- Move To Target Position and Some More

I almost created game but there is one thing which is hard for me. I want to move player to target and some more.
The red dot is the goal, but I want to move the player to the goal and a little further.
P.S
If the player goes to the right then I want him to reach the goal and a little further to the right
same to the left, top, bottom
Look Attachment: https://imgur.com/a/RF0xIQq
Red dot is a target but i want player move to target and else more on the facing side (green dot)
i tried something like move forward but i dont have any idea.
void Start()
{
}
void Update()
{
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
transform.position = Vector2.MoveTowards(transform.position, target.position, (speed * Time.deltaTime));
}
//Mob have "Player" TAG (Player is not a player) |Targeting is fine|
You could add an offset value
// ajdust this in the inspector
public float offset = 0.1f;
and than add it to the position in the direction from the player to the target. As Foggzie mentioned this might not be a copy-past-able code yet since there might occure some hickups. To atleast prevent that the player turn around after overshooting the target and move back and forth you could use a setter method to get the direction only once:
public float offset;
public float threshold = 0.0001f;
public float speed;
private GameObject target;
private Vector3 direction;
private Vector3 targetPosition;
public void SetTarget(GameObject newTarget)
{
target = newTarget;
// adding the offset in that direction
targetPosition = target.transform.position + direction * offset;
// direction from the player to the target
direction = (target.transform.position - transform.position).normalized;
}
private void Update()
{
if (!target) return;
// make Player overshoot the target by offset
transform.position = Vector2.MoveTowards(transform.position, targetPosition, (speed * Time.deltaTime));
// stop if target is reached
if (Vector3.Distance(transform.position, targetPosition) <= threshold)
{
target = null;
}
}
I don't know when and how you change the target so currently it doesn't limit the player movement to only X and Y like in your pictures ... but you would than do e.g.
// Note that 'transform' is a built-in property of 'GameObject' and you shouldn't use `GetComponent` for it
SetTarget(GameObject.FindGameObjectWithTag("Player").transform);