Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

20BCG10106

2
Posts
2
Topics
1
Following
A member registered Apr 11, 2021

Creator of

Recent community posts

Hey, there I’m ANSH PARASHAR student of VIT BHOPAL …

So today I’m gonna show you how to make a FIRST PERSON MOVEMENT in UNITY …

 

First let’s start by adding a TERRAIN. (You can update it later)

But note this while add terrain make sure u add a LAYER from the Inspector window and name it as

(Ground)….

 

Now Add an EMPTY GAME OBJECT 

Make sure the parameters are same as on the INSPECTOR window….

Now as a Child Object Add a Cylinder to this EMPTY GAME Object 

Make sure the parameters are same as on the INSPECTOR window….

 

Again as a Child Object Add a Camera to it ….

Make sure the parameters are same as on the INSPECTOR window….

 

And Lastly add an EMPTY GAME OBJECT for Ground Check …




Now lets start writing the script

1) For player movement :- Add the script to Parent Game Object……


using System.Collections;

using System.Collections.Generic;

using UnityEngine;

 

public class playerMovement : MonoBehaviour

{

    public CharacterController controller;

    public float speed = 12f;

    public float gravity = -9.81f;

 

    public Transform groundCheck;

    public float groundDistance = 0.4f;

    public LayerMask groundMask;

 

    Vector3 velocity;

    bool isGrounded;

 

    // Start is called before the first frame update

    void Start()

    {

 

    }

 

    // Update is called once per frame

    void Update()

    {

 

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

        if(isGrounded && velocity.y < 0)

        {

            velocity.y = -2f;

        }

 

        float x = Input.GetAxis("Horizontal");

        float z = Input.GetAxis("Vertical");

 

        Vector3 move = transform.right * x + transform.forward * z;

 

        controller.Move(move * speed * Time.deltaTime);

 

        velocity.y += gravity * Time.deltaTime;

 

        controller.Move(velocity*Time.deltaTime);

    }

 

 

}

 

 

 

Now in (In Inspector Window)   Public Controller Add the parent object

and in the Ground Check add the GroundCheck Child Object …

And in Ground Mask add the layer of {Ground}-(As I told u above to add it to terrain).



2) For Camera Movement :- Attach one Script to the Child Object Camera 

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

 

public class MouseLook : MonoBehaviour

{

 

    public float mouseSensitivity = 100f;

 

    public Transform playerBody;

 

    float xRotation = 0f;

 

    // Start is called before the first frame update

    void Start()

    {

        Cursor.lockState = CursorLockMode.Locked;

    }

 

    // Update is called once per frame

    void Update()

    {

        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;

        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

 

        xRotation -= mouseY;

        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

 

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);

 

        playerBody.Rotate(Vector3.up * mouseX);

    }

}

 

 

Now in (In Inspector Window)   Public Player Body Add the parent object ..



And That’s IT…..U r Done with your First Person Mouse Look.





THANK YOU.....


Here are some references and source where you can download and find the material  : 

https://assetstore.unity.com/

https://learn.unity.com/tutorial/working-with-the-terrain-editor-1#5f1ead89edbc2...

https://docs.unity3d.com/ScriptReference/CharacterController.Move.html

(4 edits)

Hello, I’m a student of VIT BHOPAL

Name : Ansh Parashar

Reg . No. : 20BCG10106

 

So I’m going to show how to make an object(Boat) float on water .

So first we have to add water in our scene ,like this

You can get this on asset store if u wanted (i.e, LowPoly Water)

 

Now add a Floating object u wanted …..

I have add a boat like this 



Now ,we have to Create a script for the floating object(BOAT)

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]

public class floatobject : MonoBehaviour

{

    public float waterlevel = 0.0f;

    public float floatthreshold = 2.0f;

    public float waterdensity = 0.1250f;

    public float downforce = 4.0f;

    float forceFactor;

    Vector3 floatForce;

    void FixedUpdate()

    {

        forceFactor = 1.0f - ((transform.position.y - waterlevel) / floatthreshold);

        if(forceFactor > 0.0f)

        {

            floatForce = -Physics.gravity * GetComponent<Rigidbody>().mass * (forceFactor - GetComponent<Rigidbody>().velocity.y * waterdensity);

            floatForce += new Vector3(0.0f, -downforce*GetComponent<Rigidbody> ().mass , 0.0f);

            GetComponent<Rigidbody>().AddForceAtPosition(floatForce, transform.position);

        }

    }



So this Script will make your object float on water and this is it …… 





Further u can add script to the object to make it move …….

 

 Here are some references and source where you can download and find the material  : 

https://assetstore.unity.com/

https://answers.unity.com/questions/879712/making-an-object-float-in-water.html

https://docs.unity3d.com/560/Documentation/Manual/HOWTO-Water.html



THANK YOU.