Activity 3: How to move and rotate an object in Unity

So again, I did another game but in the same scripts as the first one. But the difference is that, we also added scripts for movement and rotation of the objects. So look at the previous blog to know how i started making the skeleton for this game. We had first added a Player Controller there so go and check it out.

check this blog - USING UNITY IN MAKING AN ANIMATION OR GAME

By the way, we do not want a game that lacks interaction and movement, and especially obstacles, that's why you are here. So lets by pass some few step in the previous one.

So to start. We had to place our sprites in the scene, also I downloaded a nice background and platform for Aesthetics sake. 



So same as the previous game, we had to put a player controler, this time we replace the rocket with a soccer ball and replacing BoxCollider 2D with CircleCollider 2D.



After that, put your first obstacle. So this one had the script for the object to move up and down continuously.



Script for object movement:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class Move : MonoBehaviour

{

    public float freq;

    public float amp;

    Vector3 posOffset = new Vector3();

    Vector3 tempPos = new Vector3();

    void Start()

    {

        freq = Random.Range(freq - 1, freq + 1);

        amp = Random.Range(amp - 1, amp + 1);

        posOffset = transform.position;

    }

    void Update()

    {

        tempPos = posOffset;


        transform.position = new Vector3(transform.position.x, tempPos.y + Mathf.Sin(Time.fixedTime * Mathf.PI * freq) * amp, transform.position.z);

    }

}

Then we can add another object, this one must move sideways from left to right. Same with the previous object. We must add another script for it.



Script for back and forth movement:

using UnityEngine;

using System.Collections;


public class BackAndForth : MonoBehaviour

{


    public float delta = 1.5f;  

    public float speed = 2.0f;

    private Vector3 startPos;


    void Start()

    {

        startPos = transform.position;

    }


    void Update()

    {

        Vector3 v = startPos;

        v.x += delta * Mathf.Sin(Time.time * speed);

        transform.position = v;

    }

}

The last object will be different. But it can rotate continuously like a wheel. And in order to do that, we also need to put a script for it.



Script for rotating object:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class rotateLocalAxis : MonoBehaviour

{

    public Vector3 rotationDirection;

    public float smoothTime;

    private float convertedTime = 200;

    private float smooth;


   

    void Start()

    {


    }


   

    void Update()

    {

        smooth = Time.deltaTime * smoothTime * convertedTime;

        transform.Rotate(rotationDirection * smooth);

    }

}

So now you had place the scripts on the objects. Now we need to put on the frequency, aptitude, speed, delta, rotation direction, and smooth time on the specific objects. Just explore tweaking its setting to see if the speed is reasonable and accurate to your desire. Also remember to put BoxCollider 2D on each objects so it will be a working obstacle for our player.



Now we had made the object move through adding scripts using C# in Unity.





WATCH THE VIDEO DOWN BELOW




Comments

Popular Posts