I am a beginner in unity and tried make a simple game named pong.
My tutorial taught me to choose 2 separate axes. I chose Vertical for my Left Racket and Vertical1 for my right racket. However, when I used the Down and Up arrow keys, both rackets moved. Surprisingly, using "W" and "S" keys can move my left one separately.
I experimented another choice by assigning Vertical2 for Left Racket. This time, my problem was solved.
These are images of my game:
This is my code (C#) for moving the rackets:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveRacket : MonoBehaviour {
public float speed = 30;
public string axis="Vertical";
void FixedUpdate()
{
float v = Input.GetAxisRaw(axis);
GetComponent<Rigidbody2D>().velocity = new Vector2(0, v) * speed;
}
}
Related
So I'm using this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class wands : MonoBehaviour
{
public Transform WandPos;
void Update()
{
WandPos.rotation = Quaternion.Lerp(new Quaternion(0,0,1,0), new Quaternion(0,0,0.866025448f,0.49999994f),Time.time * .5f);
}
}
For some reason it is preforming the rotation before the GameObject even rotates, so when it instantiates, the GameObject snaps to where it would be if it instantiated when the scene started. I'm probably just missing something obvious, but I can't seem to find my solution anywhere online either.
Also, if there is an easier way to do this than with the code I've written, please tell me.
Well Time.time is the time in seconds since you started the application!
Sounds like you rather wanted to track the time since you instantiated the object so e.g. like
private float time;
void Update ()
{
time += Time.deltaTime * 0.5f;
WandPos.rotation = Quaternion.Lerp(new Quaternion(0,0,1,0), new Quaternion(0,0,0.866025448f,0.49999994f), time);
}
I followed this tutorial and done everything that was mentioned, everything looked and worked fine except the reticle point expands and contracts when I gaze upon the cube but the click event does not trigger. Any help would be greatly appreciated.
Code for random teleport is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ReticleTest : MonoBehaviour
{
public void RandomlyTeleport()
{
Debug.Log("reached here");
gameObject.transform.position = new Vector3(
GetRandomCoordinate(), Random.Range(0.5f, 2), GetRandomCoordinate());
}
private float GetRandomCoordinate()
{
var coordinate = Random.Range(-7,7);
while( coordinate > -1.5 && coordinate < 1.5)
{
coordinate = Random.Range(-5,5);
}
return coordinate;
}
}
These are screenshots
Update :
It turns out that the same thing is happening when I bring up the scene that comes premade with SDK HelloVR, although the hexagon(the only interactive thing in the scene) changes color when I gaze at it, nothing else happens when I click it. So, this is not a problem with what I made but the inherent problem of unity or the SDK
I'm making a small endless runner game about a fish swimming to school, and I want to include a particle system that replicates bubbles and indicates when it is speeding up. In order to make the fish swim through obstacles, he continually moves forward. However, when I try to move the particle generator forward through script, it doesn't work. Can someone tell me why?
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class motion : MonoBehaviour {
public float motionSpeed = 1f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(motionSpeed / 10, 0, 0);
motionSpeed += motionSpeed / 7200;
}
}
I recommend attaching your particle system onto a separate gameobject which is a child of your fish that is swimming.
Ok i know im so new to code its insane but i have made some progress in my books with my little game..yes it took me a month to figure out to move a character back and forth using arrow key but i did it nonetheless..So now im really stuck
how do i make my player change color (for 3 seconds) when hit from an object from above.
its 2D Csharp unity...object is falling from Y axis and my player is X axis...cause if i can do that ...then over time i can apply animation on the player when an object hits it.
Here's a script i quickly cooked up for you.
You should do more research, read tutorials, and watch more tutorials.
http://docs.unity3d.com/ScriptReference/Collider2D.OnCollisionEnter2D.html
http://docs.unity3d.com/ScriptReference/Color.Lerp.html
using UnityEngine;
using System.Collections;
public class changeColorOnHit : MonoBehaviour {
SpriteRenderer sr;
void Start(){
sr = GetComponent<SpriteRenderer>();
}
void OnCollisionEnter2D(Collision2D collision){//whenever we hit something
sr.color = new Color(2,0,0);//set this object's red color to 200 percent
}
void Update(){
//linear interpolation brings two values closer together proportional to a given third value(time)
sr.color = Color.Lerp (sr.color,Color.white,Time.deltaTime/1.5f);//slowly linear interpolate. takes about 3 seconds to return to white
}
}
I'm using the Unity 5 Car Asset coming with the Standard Assets. Controls are very hard. The car flips easily even if you are going at quite slow speed.
I have done some "tricks" I have found on the Internet like increasing the mass of the rigid body to 1500, adding the Stabilizer bars (A.K.A. anti-roll bars) script to the car, and setting the gravity center of the car in a fake perfect center. I have included the last versions of those scripts above.
I don't want to simulate perfect physics. I want a fun car easy to ride. Is it possible with Unity?
Script: gravity center of the car in a fake perfect center
using UnityEngine;
using System.Collections;
public class carflipfix : MonoBehaviour {
// Use this for initialization
void Start () {
GetComponent<Rigidbody>().centerOfMass = new Vector3(0, -1, 0);
}
}
Script: stabilizer bars (A.K.A. anti-roll bars).
using UnityEngine;
using System.Collections;
public class AntiRollBar : MonoBehaviour {
public WheelCollider wheelL;
public WheelCollider wheelR;
public float antiRollVal = 5000f;
// Update is called once per frame
void Update () {
WheelHit hit;
float travelL=1.0f;
float travelR=1.0f;
bool groundedL = wheelL.GetGroundHit(out hit);
if (groundedL){
travelL = (-wheelL.transform.InverseTransformPoint(hit.point).y - wheelL.radius) / wheelL.suspensionDistance;
}
bool groundedR = wheelR.GetGroundHit(out hit);
if (groundedR){
travelR = (-wheelR.transform.InverseTransformPoint(hit.point).y - wheelR.radius) / wheelR.suspensionDistance;
}
float antiRollForce = (travelL - travelR) * antiRollVal;
if (groundedL)
GetComponent<Rigidbody>().AddForceAtPosition(wheelL.transform.up * -antiRollForce,
wheelL.transform.position);
if (groundedR)
GetComponent<Rigidbody>().AddForceAtPosition(wheelR.transform.up * antiRollForce,
wheelR.transform.position);
}
}
If we remove the Rigidbody component, we won't be using the Unity physics. Then, we could move the car as we wish getting the input (from the keyboard, gamepad...) to multiply it by the current position of the car. It would be something like:
public class Car: MonoBehaviour
{
public void Update() {
// NOTICE THE FOLLOWING LINE OF CODE IS NOT CORRECT. THE CORRECT WAY SHOULD BE SOMETHING SIMILAR THOUGH:
gameObject.transform.position += input * Time.deltaTime * gameObject.transform.position;
}
}