Player sliding while standing still - c#

I wrote this code for player movement:
public float gravity = -9.8f;
Vector3 movement = new Vector3(deltaX, 0, deltaZ);
movement = Vector3.ClampMagnitude(movement, speed);
movement.y = gravity;
The gravity variable is supposed to keep the Player to the ground but the problem is that it also slowly slides the player around the terrain.

Setting Horizontal Only look to the Player and Vertical Only look to the Camera in the Unity Editor solves the problem.

Related

Unity camera moving

I want to move my camera detached from player, but it should have the same speed as the player. If the player is not in the camera´s visible area, the player should die.
I´ve already done player death if it is not in camera area.
Vague question, with a vague answer:
Add a script like this on the camera object:
float speed = ??;
GameObject player;
void Update() {
var step = speed * Time.deltaTime
transform.position = Vector3.MoveTowards(transform.position, player.position, step)
}
and of course, instantiate the player variable in the Start function :)

Movement script relating to camera rotation [duplicate]

This question already has an answer here:
Player Movement Relative to Camera Direction C#
(1 answer)
Closed 1 year ago.
I am rather new to unity and I have a script that works with my rigidbody that is attached to a sphere object. The problem is that the sphere does not move in the direction that my cinemachine camera is facing, it of course only moves in the direction that it has initially been set to. How do I get my sphere's WASD controls to act in the direction that the camera is facing?
Here is my movement script:
public class BallControl2 : MonoBehaviour {
public float speed = 2;
public Rigidbody rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
float movementHorizontal = Input.GetAxis("Horizontal");
float movementVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(movementHorizontal, 0.0f, movementVertical).normalized;
rb.AddForce(movement * speed);
} }
Vector3 position and direction refers to the world position and direction of your character, to move your character in the direction of camera you'll need a reference to your camera's transform and multiply your horizontal movement with camTranform.right and vertical movement with camTransform.forward
Vector3 movement = * camT.right * movementH + camT.forward * movementV;
~where movementH is your horizontal movement variable and camT is your camera's transform.~

Look Relative Movement in Unity using Cinemachine

I'm relatively new to coding, having just started about a week ago, and I can't seem to figure out or find anything relating to look relative movement using a third person Cinemachine freelook camera.
What I'm trying to accomplish is a camera similar to Risk of Rain 2, or maybe Smite where you can orbit the player and look at the front of them while they're idle, but once you start moving the player rotates towards the direction the camera is looking, and stays facing that direction no matter how they're moving.
I have a basic scene set up with a Cinemachine freelook camera following a cube, but my problem is whenever I turn the camera then move, my character doesn't rotate and instead starts moving in the direction it's facing instead of where the camera's looking, then only starts rotating if I'm pressing a movement key, so the rotation gets disjointed from where the camera's looking, if that makes sense. I kind of frankensteined some code from Royal Skies' movement tutorial and someone else's rotation tutorial for a top-down game.
This is the code for a script applied to my player character;
public float speed = 4.5f;
public Vector3 deltaMove;
public float turnspeed = 500;
void Update()
{
deltaMove = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")) * speed * Time.deltaTime; //movement code
transform.Translate(deltaMove);
float horizontal = Input.GetAxis("Mouse X");
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D) == true) //if wasd pressed, rotate
{
transform.Rotate(horizontal * turnspeed * Vector3.up * Time.deltaTime, Space.World); //the rotation code
}
}
I'd be willing to do anything to be honest, I've been trying to figure this out for a bit now.

Unity3D - Why doesn't my player rotate properly?

I'm building a top down shooter and so I have my camera above my player and the map. Here's the code I've written in the player controller script for movement:
public class playerMovement : MonoBehaviour {
public float speed;
private Camera mainCamera;
void Start () {
mainCamera = FindObjectOfType<Camera>();
}
// Update is called once per frame
void Update () {
// player movement
transform.Translate(speed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, speed * Input.GetAxis("Vertical") * Time.deltaTime);
// Camera Ray casting
Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayLength;
if (groundPlane.Raycast(cameraRay, out rayLength)) {
Vector3 look = cameraRay.GetPoint(rayLength);
Debug.DrawLine(cameraRay.origin, look, Color.red);
transform.LookAt(new Vector3(look.x, transform.position.y, look.z));
}
}
}
I want to be able to move the player using the WASD keys and also rotate following the direction on where the mouse is, however I don't want the rotation of the player to change the direction of the keys, I need the player to move forwards if the W key is pressed no matter which way the player is facing.
However for some reason my code makes the player move forwards depending on which way it is facing which I don't want.
How can I fix this?
The problem is that your transform.Translate call is in "self" space. Forward, backward, left, right are all relative to the direction the transform is facing. That is why your player is moving relative to the facing direction.
If you want to translate relative to "global" or "world" space, you have to add an additional parameter.
// player movement
transform.Translate(speed * Input.GetAxis("Horizontal") * Time.deltaTime,
0f,
speed * Input.GetAxis("Vertical") * Time.deltaTime,
Space.World);
Note the Space.World parameter at the end, to set the world coordinate system.
You can find more in the Unity docs here: https://docs.unity3d.com/ScriptReference/Transform.Translate.html
You need to look at the difference between local and global coordinate systems.
Right now your WASD keys are moving the player character according to global coordinates, and you want the WASD movement to be dependant on the player's orientation so you need to use a local coordinate system.
http://wiki.unity3d.com/index.php?title=Converting_Between_Coordinate_Systems

Unity Smooth Follow Camera that Keeps Up

I'm working on a constant runner sidescrolling game, the player is constantly moving left to right at an ever-increasing speed, and when the player jumps he gains some extra speed for a limited time.
My camera follow code is as follows (C#):
float dampTime = 0.2f;
Vector3 positionVelocity = Vector3.zero;
void LateUpdate() {
transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref positionVelocity, dampTime);
}
Now this works fine at low speed, the camera follows smoothly when the player jumps.
However as the player's speed increases the camera gets more and more left behind, with the player moving more and more to the right hand side of the screen.
I want to keep the distance between the player and the right hand side of the screen constant when he is on the ground nomatter his running speed, but when he jumps and gains the short speed boost the camera should handle it smoothly rather than lurch forwards.
How is this possible?
Thanks!
enter code here
public float smoothSpeed = 0.125f;
public Vector3 offset;
private Vector3 desiredPosition;
void FixedUpdate(){
Vector3 smoothedPosition = Vector3.Lerp(transform.position,
desiredPosition, smoothSpeed);
transform.position = smoothedPosition;
}
just tweak the offset vector3 and it will works fine i hope that

Categories