- Home /
When the player clicks on an object, how do I make that object be forced away from them in the opposite direction?
Hi, as the title says, I want to force a game object away from the player in the way they are facing. I am using an FPS controller if this matters. This is probably a simple thing, but I'm new to Unity, and I can't figure out how to do it. I don't mind whether it is JavaScript or C#.
Another little side-question, is there a way to limit the distance the player can click on the object from?
Thanks in advance! :)
If you have a reference to the player object, you can move the other object with a myriad of ways. If it has a rigidbody, you can add force, if not, you can move it other ways. Here is a quick example.
private Rigidbody body;
public float force;
public float maxDistance;
void OnEnable()
{
if (body == null)
{
body = GetComponent<Rigidbody>();
}
}
void On$$anonymous$$ouseDown()
{
if (Vector3.Distance(transform.position, Game$$anonymous$$anager.PlayerObject.transform.position) < maxDistance)
{
body.AddForce(Game$$anonymous$$anager.PlayerObject.transform.forward * force);
}
}
oh, ok didn't know On$$anonymous$$ouseDown exists.... looks good ;-)
Hi! I couldn't get this to work. I'm getting the error 'The name 'Game$$anonymous$$anager' does not exist in the current context' Here is the code: using UnityEngine; using System.Collections;
public class $$anonymous$$oveObject : $$anonymous$$onoBehaviour {
private Rigidbody body;
public float force;
public float maxDistance;
void OnEnable()
{
if (body == null)
{
body = GetComponent<Rigidbody>();
}
}
void On$$anonymous$$ouseDown()
{
if (Vector3.Distance(transform.position, Game$$anonymous$$anager.PlayerObject.transform.position) < maxDistance)
{
body.AddForce(Game$$anonymous$$anager.PlayerObject.transform.forward * force);
}
}
}
Answer by toddisarockstar · Apr 02, 2017 at 08:58 PM
you might want to check out this link:
https://docs.unity3d.com/ScriptReference/Rigidbody.AddExplosionForce.html
if you are new here, there is a simple math pricipal to get direction between two points
you simply subtract the two Vector3 positions and the resulting vector3 represents direction!
you would simply use the result for the velocity or position of the object to be moved. (pry want to normalize it and multiply by a speed variable first)
Answer by dCalle · Apr 02, 2017 at 09:22 PM
the thing you want to push needs a collider and a rigidbody attached.
you want to read and use the Script that is used here
Understand every character in the Script
Adapt the Script to this:
RaycastHit hit; Ray ray = camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { Rigidbody rigid = hit.rigidbody; rigid.AddForce(Camera.main.transform.forward * XXX); }
Remember the object will always move in the Direction the Camera is looking
if you want to limit the Player Reach, check this out and find maxDistance ;-)
Hi! It unfortunately doesn't do anything. Here is the code, it doesn't give any errors, it just doesn't do anything. using UnityEngine; using System.Collections;
public class PushObject : $$anonymous$$onoBehaviour {
public Camera camera;
void Start(){
RaycastHit hit;
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit)) {
Transform objectHit = hit.transform;
Rigidbody rigid = hit.rigidbody;
rigid.AddForce(Camera.main.transform.forward * 100);
}
}
}
Your answer
Follow this Question
Related Questions
First person skydiver help 1 Answer
2D 360 degress platformer example needed 0 Answers
Some Ideas please? 1 Answer
Simple Crate pushing.. 2 Answers
Navmesh Agent velocity VS physics 1 Answer