Jump to content

[1.12.2] Making player move/float towards certain destination


Naomiora

Recommended Posts

Basically what the title says, cancelling WASD and Spacebar keys. Then make the player float/move towards a certain destination. I got it to work for entities such as a cow or skeleton etc. the code I currently use to move an entity towards a location (in this case the location is another entity) is this:

 

	public void pullEntityToEntity(Entity entity, Entity secondEntity, double moveFactor) {
		double distX = entity.posX - secondEntity.posX;
		double distZ = entity.posZ - secondEntity.posZ;
		double dir = MathHelper.atan2(distZ, distX);
		double speed = 1f / secondEntity.getDistance(entity.posX, entity.posY, entity.posZ) * moveFactor;

		secondEntity.motionX = MathHelper.cos((float) dir) * speed;
		secondEntity.motionZ = MathHelper.sin((float) dir) * speed;
	}

 

 

the code I use to freeze certain entities is:

 

	public void freezeEntity(Entity entity) {
		entity.setVelocity(0, 0, 0);
		if (entity.prevPosX != entity.posX || entity.prevPosY != entity.posY || entity.prevPosZ != entity.posZ) {
			entity.posX = entity.prevPosX;
			entity.posY = entity.prevPosY;
			entity.posZ = entity.prevPosZ;
		}
    }

 

 

I also tried a simple network message to disable the WASD and spacebar key, which eventually turned out not to work.

 

I googled around why I couldn't change the entityplayer's position + velocity but I could not find why. The code above does work for other entities that isn't a EntityPlayer.

 

 

Thanks for reading and possibly your help! ^^

Link to comment
Share on other sites

	@SubscribeEvent
	public static void inputListener(InputUpdateEvent event) {
		if(ClientModule.isFrozen) {
			event.getMovementInput().moveStrafe = 0;
			event.getMovementInput().backKeyDown = false;
			event.getMovementInput().forwardKeyDown = false;
			event.getMovementInput().jump = false;
			event.getMovementInput().leftKeyDown = false;
			event.getMovementInput().rightKeyDown = false;
		}
	}

 

 

It does freeze most of my movement keys, except for my forwardkey and backkey. why do exactly those 2 keys still work?

Link to comment
Share on other sites

22 minutes ago, diesieben07 said:

Maybe you should set MovementInput#moveForward to 0 as well.

Oh, I didn't see that one! Ill look better next time! Thank you! Now the freezing part is solved, but how do I make the player float/move towards the certain location though? The code I currently have only works on Living AI-entities. Do I do that part in the InputUpdateEvent? I don't think so, if so thats gonna be alot of hard work xD

Edited by Naomiora
Link to comment
Share on other sites

48 minutes ago, diesieben07 said:

You'll have to adjust the position every tick. There is no "automatic movement" for players.

So something like this:
 

	public void pullEntityToEntity(Entity entity, Entity secondEntity, double moveFactor) {
		double distX = entity.posX - secondEntity.posX;
		double distZ = entity.posZ - secondEntity.posZ;
		double dir = MathHelper.atan2(distZ, distX);
		double speed = 1f / secondEntity.getDistance(entity.posX, entity.posY, entity.posZ) * moveFactor;

		secondEntity.motionX = MathHelper.cos((float) dir) * speed;
		secondEntity.motionZ = MathHelper.sin((float) dir) * speed;
	}

 

shouldn't work for an EntityPlayer? (secondEntity is the EntityPlayer in this case) If you think it should work, it doesn't for me. All it does is nothing, it does get casted though.

Link to comment
Share on other sites

You can use linear interpolation to smoothly move between two points. You will need the original start position, the final destination position and the amount between them the player should be. 

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

  • 2 weeks later...
On 3/25/2019 at 4:24 AM, Cadiboo said:

You can use linear interpolation to smoothly move between two points. You will need the original start position, the final destination position and the amount between them the player should be. 

Sorry for the horribly late reply, I tried doing this but I already knew maths wasn't my strongest part in life. I couldn't get it to work, and eventually recoded parts of my mod because I thought the reason why it didn't work was because of some other parts affecting the entities movement etc etc. So sorry for asking more help into the linear interpolation part, but could you explain how that'd work?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.