Answers for "how to make 3d player movement in unity"

C#
2

how to make 3d player movement in unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//Apply this script to what you want to move

public int MovementSpeed;

void Update(){


if(Input.GetKey("w"))
{
	transform.position += Vector3.forward * MovementSpeed * Time.deltaTime;
}
  
if(Input.GetKey("d"))
{
	transform.position += Vector3.right * MovementSpeed * Time.deltaTime;
}
  
if(Input.GetKey("a"))
{
	transform.position += Vector3.left * MovementSpeed * Time.deltaTime;
}
  
if(Input.GetKey("s"))
{
	transform.position += Vector3.back * MovementSpeed * Time.deltaTime;
}
Posted by: Guest on August-25-2021
3

how to add movement in unity 3d

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    //this is for first person movement
    public CharacterController controller;
    public float speed = 12f;
    Vector3 velocity;
    public float gravity = -9.81f;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;
    public Transform groundCheck;
    bool isGrounded;
    float jumpheight = 5f;
 
    // Update is called once per frame
    void Update()
    {
        //movement
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;
        controller.Move(move * speed * Time.deltaTime);

        //gravity
        velocity.y += gravity * Time.deltaTime;

        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
        controller.Move(velocity * Time.deltaTime);

        if(isGrounded && velocity.y >= 0)
        {
            velocity.y = -2f;
        }

        //jumping
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpheight * -2f * gravity);
        }

        //sprinting
        if(Input.GetKeyDown(KeyCode.LeftShift))
        {
            speed = 20f;
        }
        else
        {
            speed = 12f;
        }
    }
}
Posted by: Guest on August-04-2021
1

how to add movement in unity 3d

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
	public Transform cam;
	public CharacterController controller;
	public float speed = 6f;
	public float turnSmoothTime = 0.4f;
	public float jumpForce = 10f;
	float turnSmoothVelocity;
	public LayerMask groundMask;
	public float groundDistance = 0.4f;
	public Transform groundCheck;
	bool isGrounded = true;
	Vector3 velocity;
	public float gravityForce = -19.62f;
	float _slopeAngle;

	//Update is called once per frame
	void Update()
	{
		float horizontal = Input.GetAxisRaw("Horizontal");
		float vertical = Input.GetAxisRaw("Vertical");
		Vector3 direction = new Vector3(horizontal, 0f, vertical);

		if (direction.magnitude >= 0.1f)
		{
			float targetAngle = Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg + cam.eulerAngles.y;
			float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
			transform.rotation = Quaternion.Euler(0f, angle, 0f);

			Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
			controller.Move(moveDir.normalized * speed * Time.deltaTime);
		}

        //jumping
        if (isGrounded)
        {
			if (Input.GetKeyDown(KeyCode.Space))
			{
				velocity.y = Mathf.Sqrt(jumpForce * -2f * gravityForce);
			}
		}
	}

	void FixedUpdate()
	{
		if (isGrounded)
		{
			if (Input.GetKeyDown(KeyCode.LeftShift))
			{
				speed = 20f;
			}
			else if (Input.GetKeyUp(KeyCode.LeftShift))
			{
				speed = 6f;
			}
		}

		//Ground check
		isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

		//gravity
		velocity.y += gravityForce * Time.deltaTime;

		isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
		controller.Move(velocity * Time.deltaTime);

		if (isGrounded && velocity.y < 0)
		{
			velocity.y = -2f;
		}
	}
}
Posted by: Guest on August-07-2021
0

how to make an player movement in UNITY 2020

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ThirdPersonMovement : MonoBehaviour
{
   //keep in mind that it doesnt have a jump function
   //and that u need to link to the script a player and a camera. Good luck!
   public CharacterController controller;
    public Transform cam;

    public float speed = 6f;

    public float turnSmoothTime = 0.1f;

    // Update is called once per frame
    void Update()
    {
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        Vector3 direction = new Vector3(horizontal, 0f, vertical ).normalized;

        if(direction.magnitude >= 0.1f) 
        {
            float tragetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
            
             transform.rotation = Quaternion.Euler(0f, tragetAngle, 0f);
         
            
            Vector3 moveDir = Quaternion.Euler(0f, tragetAngle, 0f) * Vector3.forward;
            controller.Move(moveDir.normalized * speed * Time.deltaTime);
           
        }
    }
}
Posted by: Guest on February-20-2021

Code answers related to "how to make 3d player movement in unity"

C# Answers by Framework

Browse Popular Code Answers by Language