- Home /
how can i make two objects go opposite directions ? any idea
i want to make two objects go opposite direction on position.x
when i drag the first object using the code below , i want other GameObject to go the opposite position ?
thank you
using UnityEngine;
using System.Collections;
public class MousePos: MonoBehaviour {
private Vector3 screenPoint;
private Vector3 offset;
public GameObject rightWall;
void OnMouseDown(){
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, 0, screenPoint.z));
}
void OnMouseDrag(){
Vector3 cursorPoint = new Vector3(Input.mousePosition.x, 0, screenPoint.z);
Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
transform.position = cursorPosition;
}
}
Answer by vir1234 · Apr 24, 2018 at 05:26 AM
Something like this?
private Vector3 screenPoint;
private Vector3 offset;
public GameObject rightWall;
public GameObject leftWall;
void OnMouseDown(){
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, 0, screenPoint.z));
}
void OnMouseDrag(){
Vector3 cursorPoint = new Vector3(Input.mousePosition.x, 0, screenPoint.z);
Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
transform.position = cursorPosition;
leftWall.transform.position = -cursorPosition;
}
yes , this what i need it , it worked , thanks a lot , i just needed to out public vector3 pos;
and put it on leftWall. transform.position = -cursorPosition -pos ; becouse the wall is not it is place
but it worked , thanks alot again
Answer by itcbenjamincu · Apr 24, 2018 at 05:56 AM
ok u need to make and obj of or aad some inicial parameters example (0,1):
0= void OnMouseDrag(){ Vector3 cursorPoint = new Vector3(Input.mousePosition.x, 0, screenPoint.z); Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset; transform.position = cursorPosition; }
1= void OnMouseDrag(){ Vector3 cursorPoint = new Vector3( -1*(Input.mousePosition.x), 0, screenPoint.z); Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset; transform.position = cursorPosition; }