- Home /
Moving Object in constant speed in one axis while be able to control the object in all axis
Hi all, I need some help here. I can control an ball using Rigidbody2d.MovePostion (x and y direction).
I want same object to move up in constant speed by it self while same time i can control the ball using Rigidbody2d.MovePostion in any direction
This is what i have tried.
I added the ball in to camera as child and tired to move the camera up using translate.position Problem whit this is that ball is jagging while moving(controlling) up. (works fine if i use transform.position on ball, but i want to use Rigidbody2d)
Here is my code, here i can control the ball using Rigidbody2d.MovePostion, but ball is not moving up by it self.
void Update () {
if (Input.GetButtonDown("Fire1"))
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new
Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
if (Input.GetButton("Fire1"))
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y,
screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
mousePos = Vector3.SmoothDamp(this.transform.position, new Vector3(curPosition.x,
curPosition.y, curPosition.z), ref velocity, movementSmoth);
}
}
private void FixedUpdate()
{
//myRb.velocity = (Vector2.up * 3 * Time.deltaTime);
myRb.MovePosition(mousePos);
}
Answer by BPhage · Aug 07, 2018 at 06:27 PM
get mouse pos.
set balls rotation to mouse pos(try GameObject.LookAt(vector2)).
use
rb.AddForce(Vector2.forward * speed)
I didnt tried out 2d so it may not work sorry for my rubbish english.