Where do you want him to move? How quickly should he move there? What if something’s in the way?
It sounds like you’re trying to implement pathfinding. Or, at the very least, some kind of relatively simple AI to move the character automatically according to some set pattern.
Unfortunately, I don’t think that Unity’s built-in pathfinding (NavMesh) works with 2D. So if you want the character to move around intelligently, you’ll either need to write a fairly complex system that’s away of collisions, or look on the asset store for something.
If you just want kind of “dumb” AI, you could add some code to Update to change the velocity based on Mathf.Sin(Time.time) (or some modification of it). For example, you could set the x-component of the velocity to Mathf.Sin(Time.time), which would cause the character to move back and forth automatically.
Anyway, that’s one simple thing. But maybe you could explain the specific behavior you’re looking for.
I just want him to move in a straight line automatically then change direction with the arrow key to move in a straight line in that direction. But no waypoints.
My perception of your problem is that it stops moving when you don’t press anything. I’m also assuming there is no Vertical Movement except jumping.
I suggest changing this part in your code. Idea is that when there is no button pressed. We will just continue moving at the direction we were before.
We’ll be touching your Start() and FixedUpdate()…
void Start()
{
//Set a starting velocity when our Game Starts (Set x to 1 for moving Right and -1 for moving Left)
Vector3 DefaultVelocity = new Vector3(1 * spead, 0 ,0);
rb.velocity = DefaultVelocity;
}
void FixedUpdate()
{
//jump
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
//move left and right with left and right arrow keys
moveInput = Input.GetAxis("Horizontal");
//True if there is no Input
if (moveInput == 0f)
{
//We will try to use rb.velocity.x existing Velocity/Movement instead
//True if our Object is already moving
if (rb.velocity.x > 0f) //True if Moving Right
moveInput = 1f;
else if (rb.velocity.x < 0f) //True if Moving Left
moveInput = -1f;
//If both Statements failed, there is no Horizontal Movement
//moveInput will continue to be at 0
}
//True if there is Input OR we managed to use the existing Velocity instead to fill the value of moveInput
if (moveInput != 0f)
{
//Continue as per normal
rb.velocity = new Vector3(moveInput * spead, rb.velocity.y);
if (facingRight == false && moveInput > 0)
{
Flip();
}
else if (facingRight == true && moveInput < 0)
{
//Because he is moving left while facing right
Flip();
}
}
}
You will need to mention what controls your player will be using to make this character move. Point and Click? is it AI driven like Sims? Do you have a game example you can reference for us to understand better?
Pacman/Ms. Pacman controls, but still being able to jump too.
But I will try what the poster above you to see if it will work.
I will try to see if this will work. I don’t know if I will have time tonight but I will report back if it did or didn’t work. But from that code, it looks like it should work.
Yes you are right with what I want. Think how Pacman/Ms. Pacman is played, keeps moving in one direction until you decide to change it so it goes in the direction that you chose.
I tried your code but it caused my character to not move at all haha.
He also doesnt jump anymore either.
My bad, erased all your Start code when it was still needed haha… Put back rb = GetComponent() in Start() before anything else. My code did not have a reference to the Rigidbody, hence nothing is moving. It should work after that.
Think it was an accident but he removed some code from your Start(). You need the reference for Rigidbody so put back the code rb = GetComponent() in Start(). As of now, your Rigidbody is empty so nothing will happen, but once you put the code back in, his code should do what you want.
Well at least it got him to move and jump again so I guess we can go from there. He still only moves when I hold down the left or right arrow key, and then stops when I take my finger off the key.
He is supposed to move by itself without needing to press and hold down the arrow keys.
Well, its working, but now a couple more problems. When I move onto a slant like in the image below, he changes direction lol, thats not supposed to happen. Every time I jump, it causes him to stop moving, thats not supposed to happen either.
That actually fixed one of my issues with it. Often, when I would change direction, he would stop moving, this appeared to have fixed that.
It didn’t fix the other issues though (jumping causes him to stop moving instead of resuming after landing and going up a slanted ramp like object causes him to change direction).
if you mean if I hastily copy+pasted it into mine, nope. I did though carefully looked where your added code was and made sure I appropriately placed it in my script. Do you want me to show my whole script?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WizardPlayerContoller : MonoBehaviour
{
//this script is to get him to move and face the direction he is moving and jumping
protected Rigidbody2D rBody;
protected bool facingRight = true;
protected float moveDir;
public float spead;
public float jumpForce; //how high the character jumps
private float moveInput; //the key being pressed down
private Rigidbody2D rb;
//private bool facingRight = true;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
public int extraJumps;
//private float moveHorizontal;
//private float moveVertical;
//private Vector3 speed;
void Start()
{
rb = GetComponent<Rigidbody2D>();
//Set a starting velocity when our Game Starts (Set x to 1 for moving Right and -1 for moving Left)
Vector3 DefaultVelocity = new Vector3(1 * spead, 0, 0);
rb.velocity = DefaultVelocity;
moveDir = spead;
}
void FixedUpdate()
{
//jump
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
//move left and right with left and right arrow keys
moveInput = Input.GetAxis("Horizontal"); //* 5;
// rb.velocity = new Vector3(moveInput * spead, rb.velocity.y);
//True if there is no Input
if (moveInput == 0f)
{
//We will try to use rb.velocity.x existing Velocity/Movement instead
//True if our Object is already moving
if (rb.velocity.x > 0f) //True if Moving Right
moveInput = 1f;
else if (rb.velocity.x < 0f) //True if Moving Left
moveInput = -1f;
//If both Statements failed, there is no Horizontal Movement
//moveInput will continue to be at 0
}
//True if there is Input OR we managed to use the existing Velocity instead to fill the value of moveInput
if (moveInput != 0f)
//Continue as per normal
rb.velocity = new Vector3(moveInput * spead, rb.velocity.y);
if (facingRight == false && moveInput > 0)
{
Flip();
}
else if (facingRight == true && moveInput < 0)
{ //because he is moving left while facing right
Flip();
}
}
public void Update()
//press key to jump
{
float input = Input.GetAxis("Horizontal");
if (input > 0 && moveDir < 0)
{
moveDir = spead;
Flip();
}
else if (input < 0 && moveDir > 0)
{
moveDir = -spead;
Flip();
}
if (Input.GetKeyDown(KeyCode.Space)) //&& extraJumps > 0)
{
rb.velocity = Vector3.up * jumpForce;
}
}
void Flip() {
//moveInput = Input.GetAxis("Vertical") * 7;
facingRight = ! facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}