Mouse controlled 2D object bleeding through walls
So, I'm working on a prototype that is based around mouse mechanics. You control your player by mouse and the goal is to kill enemies by colliding with them. The faster you collide with them the more damage you do. So far so good I got all of that working, now to my real problem tho. I of course built in some walls and obstacles wich I want the player to traverse around but if I drag my player in front of a wall for example and move my mouse further and further through the wall the player sort of "bleeds" through the wall. Also ( The player only moves when Mouse1 is held down) when i let my player stand infront of a wall and click far behind it, it sort of teleports to that cursor position instead of just "trying" to move there. I tried to constrain it a bit by adding movement speed but that does not seem to work. I still want the snappy speedy the more speed you collide with the more damage kind of thing. I will provide a short GIF for you to see
there you can see what i mean. I will also provide the code for my rather basic movement script Ignore the velocity calc unless you have an idea to incorporate it as I use that for damage calculation only at the moment.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
//Variables for MousePos and Movement
private Vector3 mousePosition;
public float moveSpeed = 0.1f;
//Variables to calculate velocity
public Vector3 previous;
public float velocity;
void Update()
{
VelocityCalc();
TemporaryMovement();
}
//Velocity calc for damage calc
public void VelocityCalc()
{
velocity = ((transform.position - previous).magnitude) / Time.deltaTime;
previous = transform.position;
}
//Movement Calculation
public void TemporaryMovement()
{
if (Input.GetMouseButton(0))
{
mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
}
}
}
Answer by xxmariofer · Aug 20, 2020 at 06:25 AM
changing the collision detection in the rigidbody from discrete to continues will help, but if you want proper collision you will have to move the object with the rigidbody adding forces not the transform.position
Okay so I changed the system from transform.position to rigidbody addforce and the collision works perfectly now but I have been searching for around 2 hours now for a new problem that popped up wich is, the object that is moving towards the cursor is kind of wobbly and not snappy if you know what i mean. I will provide a new gif so you know what i mean. It also does not stop around my cursor and I also cannot really negate the force when the target hits the cursor location since it changes all the time.
use the velocity property, dont use add forces, something like this
mousePosition = Input.mousePosition;
float speed = 7;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
Vector3 direction = mousePosition - transform.position;
direction = direction.normalize;
rigidbody.velocity = speed * direction;
Your answer
Follow this Question
Related Questions
How do I assign a velocity to an object on only 1 axis? 1 Answer
How to make a 2D object, which can be moved left and right, constantly fall downwards? 1 Answer
How to make an object move, not teleport? 0 Answers
How to give the player a limited amount of moves? 1 Answer
Face direction of a Vector 3 1 Answer