I have a class below that I attach to a object in order to make it rotate around its pivot. I sent the pivot of the sprite via the inspector.
This works exactly how I want it too, BUT the issue I am having is that whenever I touch and drag it, and then touch and drag it again, it snaps to a new position.
What I would like for it to do is, when it is rotated and then rotated again, the sprite stays in its same rotation and not snap to a new position and I would like the angle of the sprite to be reset to 0. The next then is that I want the angle to continually rotate. So if I rotate it in the positive direction, the angle should keep increasing in the positive direction and not change..Such as 0---> 360 ----> 720 -----> etc, etc. And then when the mouse is released, the sprite stays in the same position but the angle is now set back to 0. And then when clicked again to rotate, it rotates from that exact position.
Here is my code thus far which works well for rotating, but I would like to modify it to achieve the above scenario. Any help with this?
public class Steering : MonoBehaviour {
float prevAngle,wheelAngle,wheelNewAngle = 0;
public SpriteRenderer sprite;
void Start () {
}
void Update () {
}
public float GetAngle(){
return wheelAngle;
}
void OnMouseDrag(){
Vector3 mouse_pos = Input.mousePosition;
Vector3 player_pos = Camera.main.WorldToScreenPoint(this.transform.position);
mouse_pos.x = mouse_pos.x - player_pos.x;
mouse_pos.y = mouse_pos.y - player_pos.y;
wheelNewAngle = Mathf.Atan2 (mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
if (Input.mousePosition.x > sprite.bounds.center.x) {
wheelAngle += wheelNewAngle - prevAngle;
} else {
wheelAngle -= wheelNewAngle - prevAngle;
}
this.transform.rotation = Quaternion.Euler (new Vector3(0, 0, wheelAngle));
Debug.Log (wheelAngle);
prevAngle = wheelNewAngle;
}
void OnMouseUp(){
prevAngle = wheelNewAngle;
wheelAngle = 0;
}
}
By angle of the sprite, do you mean the rotation? I'm not sure how the position is changing if there's nothing in your code doing that. Does it always move to the same position? I'm having a little trouble visualizing how your system is supposed to look but I hope this helps.
It looks like you might want to store the previous mouse position so you can get the relative amount to rotate each frame.
At the top:
Vector3 prevMousePos = Vector3.zero;
This method will help get the position when the player pressed:
void OnMouseDown(){
prevMousePos = Input.mousePosition;
}
Then in OnMouseDrag() get the difference between the two mouse positions to get the relative position (if you moved the mouse left, right, up, or down since pressing):
Vector3 mouseDiff = Input.mousePosition - prevMousePos;
With this it will use the relative mouse position after pressing instead of the current one, which should smooth things out.
Related
I am working on a simulation that takes place in the universe.
What I want to achieve is to move (by mouse or touch) an object around the planet at a fixed distance.
I've tried to use the RotateAround function that Unity provides, however I don't know how to take the mouse position into consideration when using that function.
So what I did is the following.
First I check if the mouse is pressed. If so I will get the mouse position and convert it to a position in the world space. I want to ignore the z because I don't want to move it in the z axis. But you could leave the z there if you want to move it in the z axis also.
Afterwards I check the direction from the planet to the mouse position. To keep it in orbit I will use the the fixed distance that is stored in maxDistance.
And finally if I want the object to look at the planet I will use LookAt.
public void Start()
{
maxDistance = Vector3.Distance(planet.position, transform.position);
}
public void Update()
{
if (Input.GetMouseButton(0))
{
var planetPos = planet.position;
var t = transform;
var mousePos = mainCamera.ScreenToWorldPoint(Input.mousePosition);
mousePos = new Vector3(mousePos.x, mousePos.y, planetPos.z);
var dir = (mousePos - planetPos).normalized;
t.position = planetPos + dir * maxDistance;
t.LookAt(planetPos, Vector3.right);
}
}
So I want to make a cube scaling by my mouse drag. However, the cube would be scaling in the up and down direction, I only want to scale in the up direction.
Here is my code, could someone please help me?
// Start is called before the first frame update
void Start()
{
InitialScale = transform.localScale;
InitialPos = transform.localPosition;
mainCamera = Camera.main;
CameraZDistance = mainCamera.WorldToScreenPoint(transform.position).z;
}
void Update()
{
onMouseDrag();
}
private void onMouseDrag()
{
if (Input.GetMouseButton(0)) {
Vector3 MouseScreenPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, CameraZDistance);
Vector3 mouseWorldPosition = mainCamera.ScreenToWorldPoint(MouseScreenPosition); // Screen point convert to world point
//change transform
float distance = Vector3.Distance(InitialPos, mouseWorldPosition);
transform.localScale = new Vector3(InitialScale.x, distance / 2f, InitialScale.z);
}
}
By default, when you create a gameobject (cube in this case), it's pivot is the center of the cube. So when you scale it in the y direction, it scales up and down equally. To scale only upwards, you want to put the pivot at the bottom of the cube, at position (0.5, 0, 0.5). To change the pivot position, you can do it in the transform section of the inspector panel in the unity editor. Alternative you can also do it through scripting by similarly referencing the transform of that cube.
Edit:
#Llama solution would work (moving the cube upwards as you scale it). I've used this solution before and if I recall correctly, there were no performance issues, however using the pivots would be the proper way to do it and I would assume it would perform better and more smoothly.
I've been doing some research on why my player(GameObject) is does not rotate toward my mouse position in my TopDown 3D game and I can't seem to find what is wrong with my code, so im making this post. The problem thats I have is that only the GameObject of my player (in my case, a capsule) rotate toward my mouse position but the axis of my player stays the same. In other word, I can't rotate the axis of my player, to face my mouse position, but I can rotate the GameObject of my player to face my mouse position. Its really hard to explain and this never happened to me before. Question is how can I rotate the axis of my player to face my mouse position. Keep in mind that my game is a top down view.
Here is the code im using for my playerMouvment and for my mouseLook:
public class Controller : MonoBehaviour
{
public float moveSpeed = 6;
Rigidbody rb;
Camera viewCamera;
Vector3 velocity;
void Start()
{
rb = GetComponent<Rigidbody>();
viewCamera = Camera.main;
}
void Update()
{
Vector3 mousePos = viewCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, viewCamera.transform.position.y));
transform.LookAt(mousePos + Vector3.up * transform.position.y);
velocity = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized * moveSpeed;
}
void FixedUpdate()
{
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}
}
Again I tried and look for any error in my code and I can't find anything that cause this weird situation and so if anyone can help me find a better way to write this code and solve my problem it would be great!
If I understand you correctly everything is fine with your game and the player is turning as it should, but only in the editor that the axis of the player (red, green and blue arrows) don't turn with the player?
If this is the problem it might be that you are using global space handle instead of local space. Clicking the icon I highlighted in the image should do the trick.
Use this code instead of LookAt() function
Vector2 direction = mousePos.position - player.transform.position;
player.transform.right /* Maybe you need Up or -Up or -right */ = direction;
This would work too in some cases
Vector2 direction = new Vector2
(
mousePos.position.x - player.transform.position.x,
mousePos.positoin.y - player.transform.position.y
)
player.transform.right /* Maybe you need Up or -Up or -right */ = direction;
I am creating a 2D game in Unity and wanted to see if anyone has any suggestion on how I can improve the script below which I am using to drag a paddle in a breakout/arkanoid style game. I know there are more complex ways to drag objects but this works ok for me but the only issue I encounter is that when I test my game on a mobile device the dragging is not 100% sharp and when I stop dragging the paddle seems to lag ever so slightly. I don’t have any issues with my mobile device as I have played other breakout games I downloaded from the Play store and the dragging is very crisp.
The script below is attached to the paddle.
Vector3 dist;
float posX;
float posY;
void OnMouseDown(){
dist = Camera.main.WorldToScreenPoint(transform.position);
posX = Input.mousePosition.x - dist.x;
posY = Input.mousePosition.y - dist.y;
}
void OnMouseDrag(){
Vector3 curPos = new Vector3(Input.mousePosition.x - posX, Input.mousePosition.y - posY, dist.z);
Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);
transform.position = worldPos;
}
I would advise doing your translation of the x and y-axis values using the mouse's position as it's being dragged via the subtracted difference between what the mouse's position value was as it's first pressed down.
A summary of that would be:
Store a 2D vector as a private field, called previousPos (so
mousedown and mousemove can both access it) and set it's value to the result of the mouse's current position.
In the mouse drag function, get the mouse position again and assign the
value to a 2D vector called currentPos and subtract that from
the previousPos, which gives you the x and y-axis values to set the
paddle's transform position to.
Then update the previousPos at the end of the drag function, and
assign the value of currentPos to it so that next time around it
gives the new change and not the aggregated change.
*You probably want to have a boolean that you set to true when mouse is down and at the end of the drag set it to false. Use that bool in the drag as a check to see if you need to set the paddle position in the first place (only set paddle position if the boolean is true) -- Not sure if unity does this for you by only firing mouse dragged when the mouse is down or not.
An example in code:
private Vector2 _previousPos;
private bool _isMouseDown;
void OnMouseDown(){
_isMouseDown = true; //because we're in the on mouse down function
_previousPos = Input.mousePosition; //get the current mouse position
}
void OnMouseDrag(){
if(!_isMouseDown)
return;
Vector2 currentPos = Input.mousePosition; //get updated mouse pos
Vector2 paddlePos = currentPos - previousPos; //the delta change
transform.position = paddlePos; //new paddle position
_isMouseDown = false; //drag is complete, mouse btn is no longer down
_previousPos = currentPos; //reset previous pos for next mouse move
}
I have a ball which rotates around the point 0,0,0 in the Z-axis. When the space button is pressed, the ball has to go inside the large circle. Now my code looks like this. When you press space, the ball does not behave as they should. I want to know how to make a balloon down exactly down
that's how the ball should behave ->
behavior image
my code:
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
transform.position = new Vector3 (transform.position.x - 1, transform.position.y - 1, 0);
} else {
transform.RotateAround(new Vector3(0,0,0), new Vector3(0,0,1), 2);
}
}
Your code to 'jump' the orbit doesn't do what you want because Transform.RotateAround modifies both the rotation and the position of the object's transform.
So jumping to (position - 1,1,0) in the world is going to return wildly different results every time.
What you want to do instead is calculate the (Vector) direction from the object to the centre of orbit (the difference), then scale that down to how far you want it to move, then apply it to the position.
private Vector3 _orbitPos = Vector3.zero;
private float _orbitAngle = 2f;
private float _distanceToJump = 2f;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
var difference = (_orbitPos - transform.position).normalized * _distanceToJump;
transform.Translate(difference);
}
transform.RotateAround(_orbitPos, Vector3.forward, _orbitAngle);
}
This will move the object to be orbiting 2 units closer when space is pressed immediately.
If you wanted to have a smooth transition instead of a jump, look into using Mathf.Lerp, Vector3.Lerp and the routines involved.