Hi,
I've recently had a problem, I wanted to add the posibility to look on the sides with character camera in 3rd person, but it seems like after introducing it, the rest of the code seems to be ignored, like the beggining can be deleted and the rest is still working, although I think it shouldn't. It might be a bit messy, but does anyone know what's wrong?
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera : MonoBehaviour
{
public Transform ObjectToTrack;
public Vector3 delta;
void FixedUpdate()
{
transform.LookAt(ObjectToTrack);
var trackedRigidbody = ObjectToTrack.GetComponent<Rigidbody>();
var speed = trackedRigidbody.velocity.magnitude;
var targetPosition = ObjectToTrack.position + delta * (speed / 20f + 1f);
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.smoothDeltaTime * 3f);
}
float cameraAngle = 0f;
void Update()
{
if (Input.GetKey(KeyCode.Mouse0)) cameraAngle += Input.GetAxis("Mouse X");
var quaternionX = Quaternion.Euler(0, cameraAngle, 0);
var positionX = ObjectToTrack.position
+ quaternionX * delta;
transform.position = positionX;
transform.LookAt(ObjectToTrack);
transform.rotation *= Quaternion.Euler(-30f, 0, 0);
}
}
Update is called after fixed update(s) and so you're setting the position then overwriting it.
I would suggest changing this line
var positionX = ObjectToTrack.position + quaternionX * delta;
To
var positionX = transform.position + quaternionX * delta;
Or, more concisely just
transform.position += quaternionX * delta;
Related
My objective here is to have a smoothed out "follow camera", for my space-fighter proof of concept game. The camera should match roll off the target object in all axis.
To that end I've "stolen" and modified this code from the unity Answers-site, and it works beautifully for X and Y (pitch and yaw), but it refuses to roll.
Code:
public float Distance;
public float Height;
public float RotationDamping;
public GameObject Target;
void LateUpdate()
{
var wantedRotationAngleYaw = Target.transform.eulerAngles.y;
var currentRotationAngleYaw = transform.eulerAngles.y;
var wantedRotationAnglePitch = Target.transform.eulerAngles.x;
var currentRotationAnglePitch = transform.eulerAngles.x;
var wantedRotationAngleRoll = Target.transform.eulerAngles.z;
var currentRotationAngleRoll = transform.eulerAngles.z;
currentRotationAngleYaw = Mathf.LerpAngle(currentRotationAngleYaw, wantedRotationAngleYaw, RotationDamping * Time.deltaTime);
currentRotationAnglePitch = Mathf.LerpAngle(currentRotationAnglePitch, wantedRotationAnglePitch, RotationDamping * Time.deltaTime);
currentRotationAngleRoll = Mathf.LerpAngle(currentRotationAngleRoll, wantedRotationAngleRoll, RotationDamping * Time.deltaTime);
var currentRotation = Quaternion.Euler(currentRotationAnglePitch, currentRotationAngleYaw, currentRotationAngleRoll);
transform.position = Target.transform.position;
transform.position -= currentRotation * Vector3.forward * Distance;
transform.LookAt(Target.transform);
transform.position += transform.up * Height;
}
Image:
I would be more certain about this answer if you explained what you were trying to do, but you should consider moving by Height in the direction of currentRotation * Vector3.up instead of transform.up. Also, consider using currentRotation * Vector3.up to set the local up direction when calling LookAt:
transform.position = Target.transform.position;
transform.position -= currentRotation * Vector3.forward * Distance;
Vector3 currentUp = currentRotation * Vector3.up;
transform.LookAt(Target.transform, currentUp);
transform.position += currentUp * Height;
I can make the camera move the way I want, but I need it to be confined to within the 3D hollow sphere that I created. Currently the player can fly the camera out of the game area.
I've tried parenting the camera to a spherical game object, but the camera still leaves the game area but the sphere stays inside.
This was my attempt at using the BoundingSphere method to keep the camera inside the sphere.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SphericalBoundary : MonoBehaviour
{
public Vector3 pos;
public float r;
void Start()
{
BoundingSphere();
}
private void FixedUpdate()
{
BoundingSphere();
}
public void BoundingSphere()
{
pos = new Vector3(0, 0, 0);
r = (100);
}
}
============================================================================
============================================================================
This is the script that makes the camera fly around. Works perfectly.
using UnityEngine;
using System.Collections;
public class CameraFlight : MonoBehaviour
{
/*
EXTENDED FLYCAM
Desi Quintans (CowfaceGames.com), 17 August 2012.
Based on FlyThrough.js by Slin (http://wiki.unity3d.com/index.php/FlyThrough), 17 May 2011.
LICENSE
Free as in speech, and free as in beer.
FEATURES
WASD/Arrows: Movement
Q: Climb
E: Drop
Shift: Move faster
Control: Move slower
End: Toggle cursor locking to screen (you can also press Ctrl+P to toggle play mode on and off).
*/
public float cameraSensitivity = 90;
public float climbSpeed = 4;
public float normalMoveSpeed = 10;
public float slowMoveFactor = 0.25f;
public float fastMoveFactor = 3;
private float rotationX = 0.0f;
private float rotationY = 0.0f;
void Start()
{
// Screen.lockCursor = true;
}
void Update()
{
rotationX += Input.GetAxis("Mouse X") * cameraSensitivity * Time.deltaTime;
rotationY += Input.GetAxis("Mouse Y") * cameraSensitivity * Time.deltaTime;
rotationY = Mathf.Clamp(rotationY, -90, 90);
transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
transform.position += transform.forward * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
transform.position += transform.right * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
{
transform.position += transform.forward * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
transform.position += transform.right * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;
}
else
{
transform.position += transform.forward * normalMoveSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
transform.position += transform.right * normalMoveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
}
if (Input.GetKey(KeyCode.Q)) { transform.position += transform.up * climbSpeed * Time.deltaTime; }
if (Input.GetKey(KeyCode.E)) { transform.position -= transform.up * climbSpeed * Time.deltaTime; }
//if (Input.GetKeyDown(KeyCode.End))
//{
// Screen.lockCursor = (Screen.lockCursor == false) ? true :][1]
false;
//}
}
}
No errors are showing up. During play the player can fly the camera out of the sphere but I want them confined to the sphere.
I think you must look your camera position on every frame and also check if it's going out of bound of the sphere here is some code which might help you(it is not tested)
public class TestScriptForStackOverflow : MonoBehaviour {
public Transform CameraTrans, SphereTrans;
private float _sphereRadius;
void Start() {
//if we are talking about sphere its localscale.x,y,z values are always equal
_sphereRadius = SphereTrans.localScale.x / 2;
}
void Update() {
if (IsOutsideTheSphere(_sphereRadius, CameraTrans.position, SphereTrans.position)) {
//lets find direction of camera from sphere center
Vector3 direction = (CameraTrans.position - SphereTrans.position).normalized;
//this is bound point for specific direction which is point is on the end of the radius
Vector3 boundPos = SphereTrans.position + direction * _sphereRadius;
//finally assign bound position to camera to stop it to pierce the sphere bounds
CameraTrans.position = boundPos;
}
}
private bool IsOutsideTheSphere(float sphereRadius, Vector3 cameraPosition, Vector3 sphereCenterPosition) {
//distance betweeen cameraPosition and sphereCenterPosition
float distanceBetween = (cameraPosition - sphereCenterPosition).magnitude;
//returns true if distance between sphere center and camera position is longer then sphere radius
return distanceBetween > sphereRadius;
}
If there is something you don't understand ask me in comments
I currently have a script to move my "Player" around a surface but I find it very uneffective, the cube wiggles left to right and up and down. Does anyone have any good scrips on how to move a cube, flipping side to side, without wiggling or whatever. that also moves perfectly each time? my last one would move to a point like 2.000231 or something like that. it was really fusterating because I'm trying to have it perfect so it would fit in an hole. He's the script I am using. If you have a better one pls include ty
using System.Collections;
using UnityEngine;
public class TumblingCubes : MonoBehaviour
{
public float tumblingDuration = 0.2f;
void Update()
{
var dir = Vector3.zero;
if (Input.GetKey(KeyCode.UpArrow))
dir = Vector3.forward;
if (Input.GetKey(KeyCode.DownArrow))
dir = Vector3.back;
if (Input.GetKey(KeyCode.LeftArrow))
dir = Vector3.left;
if (Input.GetKey(KeyCode.RightArrow))
dir = Vector3.right;
if (dir != Vector3.zero && !isTumbling)
{
StartCoroutine(Tumble(dir));
}
}
bool isTumbling = false;
IEnumerator Tumble(Vector3 direction)
{
isTumbling = true;
var rotAxis = Vector3.Cross(Vector3.up, direction);
var pivot = (transform.position + Vector3.down * 0.5f) + direction * 0.5f;
var startRotation = transform.rotation;
var endRotation = Quaternion.AngleAxis(90.0f, rotAxis) * startRotation;
var startPosition = transform.position;
var endPosition = transform.position + direction;
var rotSpeed = 90.0f / tumblingDuration;
var t = 0.0f;
while (t < tumblingDuration)
{
t += Time.deltaTime;
transform.RotateAround(pivot, rotAxis, rotSpeed * Time.deltaTime);
yield return null;
}
transform.rotation = endRotation;
transform.position = endPosition;
isTumbling = false;
}
}
Your script should work just fine, you will always have small errors because of how float numbers are stored. But you shouldn't have errors as big as yours.
Are sure your cube is starting from (0,0,0)?
Anyway you can be sure if you put this in the end of the Tumble
var vec = transform.eulerAngles;
vec.x = Mathf.RoundToInt(vec.x / 90 * 90);
vec.y = Mathf.RoundToInt(vec.y / 90 * 90);
vec.z = Mathf.RoundToInt(vec.z / 90 * 90);
transform.eulerAngles = vec;
I'm tring to implement a simple (but realistic as possible) 2D flight simulator in Unity using its 2D engine.
I have taken some sources around and tried to compile my own (without success) ... i must admit i'm a math and c# newbie; forgive my ignorance ..
Ideally i would like to achieve something like this :
http://runway.countlessprojects.com/prototype/index.html
Any ideas / corrections / suggestions welcome
using UnityEngine;
using System.Collections;
public class FlightControl : MonoBehaviour {
public float energy;
public float roll;
public float tilt;
public float yaw;
public float airspeed;
public float fall;
public float tip;
Rigidbody2D rb;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
void Start () {
}
void Update () {
energy = transform.position.y + GetComponent<Rigidbody2D>().velocity.magnitude;
}
void FixedUpdate () {
tilt = Mathf.Clamp (energy / 100, -8f, 5f);
tilt /= Time.deltaTime * 10;
if (Input.GetButton("Jump")) {
GetComponent<Rigidbody2D>().AddForce((Vector2)transform.up * Time.deltaTime * 10000);
}
transform.rotation = Quaternion.Euler(0, 0, 270+Mathf.Rad2Deg * Mathf.Atan2(rb.velocity.y, rb.velocity.x));
if (((Vector2)transform.forward + GetComponent<Rigidbody2D>().velocity.normalized).magnitude < 1.4)
tilt += 1f;
if (tilt != 0)
transform.Rotate (new Vector3 (0f, 0f, tilt * Time.deltaTime));
GetComponent<Rigidbody2D>().velocity -= Vector2.up * Time.deltaTime;
// Velocity
Vector2 vertvel = GetComponent<Rigidbody2D>().velocity - (Vector2)Vector3.ProjectOnPlane (transform.up, GetComponent<Rigidbody2D>().velocity);
fall = vertvel.magnitude;
GetComponent<Rigidbody2D>().velocity -= vertvel * Time.deltaTime;
GetComponent<Rigidbody2D>().velocity += vertvel.magnitude * (Vector2)transform.right * Time.deltaTime / 10;
// Drag
Vector2 forwardDrag = GetComponent<Rigidbody2D>().velocity - (Vector2)Vector3.ProjectOnPlane ((Vector2)transform.right, GetComponent<Rigidbody2D>().velocity);
GetComponent<Rigidbody2D>().AddForce (forwardDrag * forwardDrag.magnitude * Time.deltaTime / 1000);
airspeed = GetComponent<Rigidbody2D>().velocity.magnitude;
}
}
Hi everybody I am having a problem with my game, the problem is based when I switch the materials so it can face the other direction (it is a 2d game). This affects the animation which is controlled by the renderer.material.mainTextureOffset but I have no idea why this is not working.
code in c#:
using UnityEngine;
using System.Collections;
public class character : MonoBehaviour {
public float forwardSpeed = 10.0f;
public Material newMaterialRefcs1;
public Material newMaterialRefcs2;
// Use this for initialization
void Start () {
}
//error is occurring here !!!!!!!! below Important. The 2 if statements below
// Update is called once per frame
void Update () {
if( Input.GetKey(KeyCode.D)){
renderer.material.mainTextureOffset = new Vector2(0.25f, 0);
transform.position += -transform.right * forwardSpeed * Time.deltaTime;
renderer.material = newMaterialRefcs1;
}if( Input.GetKey(KeyCode.A)){
renderer.material.mainTextureOffset = new Vector2(0.25f, 0);
transform.position += transform.right * forwardSpeed * Time.deltaTime;
renderer.material = newMaterialRefcs2;
}
}}
I'm not sure exactly what your problem is but it sounds like you're trying to adjust one setting in the first script then add a new material in the second script. If that is the case then, when you assign the new material in the second script you are replacing the material completely meaning the renderer.material.mainTextureOffset.x that you set in the first gets overwritten.
Try keeping the offset in a separate variable that you can assign to the new texture when it is added in the second script.
Try to use other shader. Diffuse, for example.
Also, check in inspector, is this value really changed. May You rewrite it in other script?
I have figured it out with some help from the unity forums took a while but here it is:
using UnityEngine;
using System.Collections;
public class character : MonoBehaviour {
public float forwardSpeed = 20.0f; public float rot = 0f;public float jumpSpeed = 100;public float gravity = 30f;
public Material newMaterialRefcs1;
public Material newMaterialRefcs2;
void Start () {
}
public float scrollSpeed = 0.25F;
void Update () {
if( Input.GetKey(KeyCode.RightArrow)){
scrollSpeed += 0.25f;
transform.position += -transform.right * forwardSpeed * Time.deltaTime;
renderer.material = newMaterialRefcs1;
float offset = scrollSpeed;
renderer.material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
}if( Input.GetKey(KeyCode.LeftArrow)){
scrollSpeed += 0.25f;
transform.position += transform.right * forwardSpeed * Time.deltaTime;
renderer.material = newMaterialRefcs2;
float offset = scrollSpeed;
renderer.material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
}
Vector3 isgrounded = transform.TransformDirection(Vector3.up);
if( Input.GetKeyDown(KeyCode.Space)&& Physics.Raycast(transform.position, isgrounded, 6)){
transform.position -= transform.up * jumpSpeed * Time.deltaTime*2;
}
Physics.gravity = new Vector3(0, gravity, 0);
transform.rotation = Quaternion.Euler(0, 0, transform.rotation.eulerAngles.z);
}
}