- Home /
Move character forward constantly
So I want my character to automatically move forward to the right. And control the character up and down using joystick. But seems like my script has a conflict and I'm new to Unity so I'm not sure how to fix it, if I reverse the code, one works then the other will not work. Thanks in advance.
public float moveSpeed = 2f;
public float forwardMovementSpeed = 3.0f;
public Rigidbody2D rb;
public Joystick joystick;
Vector2 movement;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
movement.y = joystick.Vertical;
transform.Translate(Vector2.right * forwardMovementSpeed * Time.deltaTime);
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
Answer by xxmariofer · Oct 31, 2019 at 10:12 AM
public float moveSpeed = 2f;
public float forwardMovementSpeed = 3.0f;
public Rigidbody2D rb;
public Joystick joystick;
Vector2 movement;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
movement.y = joystick.Vertical;
}
void FixedUpdate()
{
rb.MovePosition(rb.position + new Vector3(forwardMovementSpeed * Time.fixedDeltaTime, movement.y * moveSpeed * Time.fixedDeltaTime, 0));
}
Unfortunately there's a problem with this line of code (rb.position + new Vector3(forward$$anonymous$$ovementSpeed Time.fixedDeltaTime, movement.y moveSpeed * Time.fixedDeltaTime, 0)). It says Operator '+' is ambiguous in operands of type 'Vector2' and 'Vector3'. Any solution? Thanks for the help though.
rb.$$anonymous$$ovePosition(rb.position + new Vector2(forward$$anonymous$$ovementSpeed * Time.fixedDeltaTime, movement.y * moveSpeed * Time.fixedDeltaTime));
Thanks Hellium it works now! Appreciate your help.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Rb2D.MovePosition not working - weird error... 1 Answer
How do I make a character Lunge? 2 Answers
[C#] can someone give me a detailed explanation of lerp 1 Answer